From e86b81f3cc8bf29bf963d72977a31cc3305d06d1 Mon Sep 17 00:00:00 2001 From: Noel Forte Date: Sat, 19 Oct 2024 02:48:00 -0400 Subject: [PATCH] set up auto-detection for eleventy.config.js tests --- .vscode/tests.code-snippets | 2 +- tests/_utils/eleventy-test.js | 21 ++++++++++++--------- tests/autotrim.test.js | 10 ++++------ tests/basic-template.test.js | 6 ++---- tests/computed-data.test.js | 2 +- tests/engine-overrides.test.js | 6 ++---- tests/filters.test.js | 6 ++---- tests/ignore-tag.test.js | 2 +- tests/includes.test.js | 2 +- tests/layouts.test.js | 2 +- tests/permalinks.test.js | 2 +- tests/shortcodes.test.js | 6 ++---- 12 files changed, 30 insertions(+), 37 deletions(-) diff --git a/.vscode/tests.code-snippets b/.vscode/tests.code-snippets index 2ee7eaa..b411dff 100644 --- a/.vscode/tests.code-snippets +++ b/.vscode/tests.code-snippets @@ -3,7 +3,7 @@ "scope": "javascript", "prefix": "#new-test-suite", "body": [ - "import { EleventyTest } from './_eleventy-test.js';", + "import { EleventyTest } from '#11ty-test';", "import { test } from 'vitest';\n", "const testRun = new EleventyTest('${1:./path-to-test/}');\n", "test('${2:test name}', async ({ expect }) => {", diff --git a/tests/_utils/eleventy-test.js b/tests/_utils/eleventy-test.js index 99303b3..eee9f65 100644 --- a/tests/_utils/eleventy-test.js +++ b/tests/_utils/eleventy-test.js @@ -1,12 +1,12 @@ /** * @typedef TestOptions * @property {import('eleventy-plugin-vento').VentoPluginOptions} pluginOptions - * @property {boolean} useEleventyConfig * * @typedef {{ url: string, inputPath: string, outputPath: string, rawInput: string, content: string }} PageObject */ import path from 'node:path'; +import fs from 'node:fs'; import Eleventy from '@11ty/eleventy'; import { VentoPlugin } from 'eleventy-plugin-vento'; @@ -19,33 +19,36 @@ class EleventyTest extends Eleventy { * @param {TestOptions} options */ constructor(inputPathname, options) { - const inputPath = path.resolve(import.meta.dirname, inputPathname); + const inputPath = path.resolve(import.meta.dirname, '..', inputPathname); const outputPath = path.resolve(inputPath, '_site'); + const maybeConfigPath = path.join(inputPath, 'eleventy.config.js'); // Initialize parent class super(inputPath, outputPath, { quietMode: true, - configPath: options?.useEleventyConfig && path.join(inputPath, 'eleventy.config.js'), + configPath: fs.existsSync(maybeConfigPath) && maybeConfigPath, /** @param {import('@11ty/eleventy').UserConfig} eleventyConfig */ config(eleventyConfig) { eleventyConfig.addPlugin(VentoPlugin, options?.pluginOptions); }, }); + } - // Perform a build - this.buildResults = this.toJSON(); + async rebuild() { + this.buildResults = await this.toJSON(); + return this.buildResults; } async getBuildResultForUrl(url) { - const results = await this.buildResults; - return results.find((page) => page.url === url); + this.buildResults ??= await this.rebuild(); + return this.buildResults.find((page) => page.url === url); } async countResultPages() { - const results = await this.buildResults; - return results.filter(({ outputPath }) => Boolean(outputPath)).length; + this.buildResults ??= await this.rebuild(); + return this.buildResults.filter(({ outputPath }) => Boolean(outputPath)).length; } } diff --git a/tests/autotrim.test.js b/tests/autotrim.test.js index 491a885..0a9fb42 100644 --- a/tests/autotrim.test.js +++ b/tests/autotrim.test.js @@ -1,11 +1,11 @@ -import { EleventyTest } from './_eleventy-test.js'; +import { EleventyTest } from '#11ty-test'; import { test } from 'vitest'; test('trim all tags', async ({ expect }) => { const testRun = new EleventyTest('./autotrim/', { pluginOptions: { autotrim: true }, - useEleventyConfig: true, }); + const { content } = await testRun.getBuildResultForUrl('/'); await expect(content).toMatchFileSnapshot('./_results/autotrim-all.html'); @@ -14,8 +14,8 @@ test('trim all tags', async ({ expect }) => { test('trim a single tag', async ({ expect }) => { const testRun = new EleventyTest('./autotrim/', { pluginOptions: { autotrim: ['set'] }, - useEleventyConfig: true, }); + const { content } = await testRun.getBuildResultForUrl('/'); await expect(content).toMatchFileSnapshot('./_results/autotrim-default-single.html'); @@ -24,8 +24,8 @@ test('trim a single tag', async ({ expect }) => { test('trim all default tags (extends)', async ({ expect }) => { const testRun = new EleventyTest('./autotrim/', { pluginOptions: { autotrim: ['@vento', 'tag1', 'tag2'] }, - useEleventyConfig: true, }); + const { content } = await testRun.getBuildResultForUrl('/'); await expect(content).toMatchFileSnapshot('./_results/autotrim-default-extends.html'); @@ -34,7 +34,6 @@ test('trim all default tags (extends)', async ({ expect }) => { test('trim a single custom tag', async ({ expect }) => { const testRun = new EleventyTest('./autotrim/', { pluginOptions: { autotrim: ['button'] }, - useEleventyConfig: true, }); const { content } = await testRun.getBuildResultForUrl('/'); @@ -44,7 +43,6 @@ test('trim a single custom tag', async ({ expect }) => { test('trim all custom tags (extends)', async ({ expect }) => { const testRun = new EleventyTest('./autotrim/', { pluginOptions: { autotrim: ['@11ty', 'tag1', 'tag2'] }, - useEleventyConfig: true, }); const { content } = await testRun.getBuildResultForUrl('/'); diff --git a/tests/basic-template.test.js b/tests/basic-template.test.js index aceffbd..650c182 100644 --- a/tests/basic-template.test.js +++ b/tests/basic-template.test.js @@ -1,9 +1,7 @@ -import { EleventyTest } from './_eleventy-test.js'; +import { EleventyTest } from '#11ty-test'; import { test } from 'vitest'; -const testRun = new EleventyTest('./basic/', { - useEleventyConfig: true, -}); +const testRun = new EleventyTest('./basic/'); test('basic template', async ({ expect }) => { const { content } = await testRun.getBuildResultForUrl('/pages/'); diff --git a/tests/computed-data.test.js b/tests/computed-data.test.js index 8c299e0..0404735 100644 --- a/tests/computed-data.test.js +++ b/tests/computed-data.test.js @@ -1,4 +1,4 @@ -import { EleventyTest } from './_eleventy-test.js'; +import { EleventyTest } from '#11ty-test'; import { test } from 'vitest'; const testRun = new EleventyTest('./computed-data/'); diff --git a/tests/engine-overrides.test.js b/tests/engine-overrides.test.js index 1859001..174f1a3 100644 --- a/tests/engine-overrides.test.js +++ b/tests/engine-overrides.test.js @@ -1,9 +1,7 @@ -import { EleventyTest } from './_eleventy-test.js'; +import { EleventyTest } from '#11ty-test'; import { test } from 'vitest'; -const testRun = new EleventyTest('./engine-overrides/', { - useEleventyConfig: true, -}); +const testRun = new EleventyTest('./engine-overrides/'); test('use vento as html engine', async ({ expect }) => { const { content } = await testRun.getBuildResultForUrl('/as-html-engine/'); diff --git a/tests/filters.test.js b/tests/filters.test.js index 5af9111..55c1b66 100644 --- a/tests/filters.test.js +++ b/tests/filters.test.js @@ -1,9 +1,7 @@ -import { EleventyTest } from './_eleventy-test.js'; +import { EleventyTest } from '#11ty-test'; import { test } from 'vitest'; -const testRun = new EleventyTest('./filters/', { - useEleventyConfig: true, -}); +const testRun = new EleventyTest('./filters/'); test('transform string with built-ins', async ({ expect }) => { const { content } = await testRun.getBuildResultForUrl('/basic/'); diff --git a/tests/ignore-tag.test.js b/tests/ignore-tag.test.js index 76a2d66..d4f5a12 100644 --- a/tests/ignore-tag.test.js +++ b/tests/ignore-tag.test.js @@ -1,4 +1,4 @@ -import { EleventyTest } from './_eleventy-test.js'; +import { EleventyTest } from '#11ty-test'; import { test } from 'vitest'; const testRun = new EleventyTest('./ignore-tag/', { diff --git a/tests/includes.test.js b/tests/includes.test.js index 76056f1..2cf7493 100644 --- a/tests/includes.test.js +++ b/tests/includes.test.js @@ -1,4 +1,4 @@ -import { EleventyTest } from './_eleventy-test.js'; +import { EleventyTest } from '#11ty-test'; import { test } from 'vitest'; const testRun = new EleventyTest('./includes/'); diff --git a/tests/layouts.test.js b/tests/layouts.test.js index 5e0a5cd..08864d8 100644 --- a/tests/layouts.test.js +++ b/tests/layouts.test.js @@ -1,4 +1,4 @@ -import { EleventyTest } from './_eleventy-test.js'; +import { EleventyTest } from '#11ty-test'; import { test } from 'vitest'; const testRun = new EleventyTest('./layouts/'); diff --git a/tests/permalinks.test.js b/tests/permalinks.test.js index 5c6dd27..a64d940 100644 --- a/tests/permalinks.test.js +++ b/tests/permalinks.test.js @@ -1,4 +1,4 @@ -import { EleventyTest } from './_eleventy-test.js'; +import { EleventyTest } from '#11ty-test'; import { test } from 'vitest'; const testRun = new EleventyTest('./permalinks/'); diff --git a/tests/shortcodes.test.js b/tests/shortcodes.test.js index 3632f9d..6362aa3 100644 --- a/tests/shortcodes.test.js +++ b/tests/shortcodes.test.js @@ -1,9 +1,7 @@ -import { EleventyTest } from './_eleventy-test.js'; +import { EleventyTest } from '#11ty-test'; import { test } from 'vitest'; -const testRun = new EleventyTest('./shortcodes/', { - useEleventyConfig: true, -}); +const testRun = new EleventyTest('./shortcodes/'); test('run single shortcodes', async ({ expect }) => { const { content } = await testRun.getBuildResultForUrl('/single/');