Skip to content

Commit

Permalink
set up auto-detection for eleventy.config.js tests
Browse files Browse the repository at this point in the history
  • Loading branch information
noelforte committed Oct 24, 2024
1 parent 3bb1140 commit e86b81f
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .vscode/tests.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {",
Expand Down
21 changes: 12 additions & 9 deletions tests/_utils/eleventy-test.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
}
}

Expand Down
10 changes: 4 additions & 6 deletions tests/autotrim.test.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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('/');
Expand All @@ -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('/');
Expand Down
6 changes: 2 additions & 4 deletions tests/basic-template.test.js
Original file line number Diff line number Diff line change
@@ -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/');
Expand Down
2 changes: 1 addition & 1 deletion tests/computed-data.test.js
Original file line number Diff line number Diff line change
@@ -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/');
Expand Down
6 changes: 2 additions & 4 deletions tests/engine-overrides.test.js
Original file line number Diff line number Diff line change
@@ -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/');
Expand Down
6 changes: 2 additions & 4 deletions tests/filters.test.js
Original file line number Diff line number Diff line change
@@ -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/');
Expand Down
2 changes: 1 addition & 1 deletion tests/ignore-tag.test.js
Original file line number Diff line number Diff line change
@@ -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/', {
Expand Down
2 changes: 1 addition & 1 deletion tests/includes.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EleventyTest } from './_eleventy-test.js';
import { EleventyTest } from '#11ty-test';
import { test } from 'vitest';

const testRun = new EleventyTest('./includes/');
Expand Down
2 changes: 1 addition & 1 deletion tests/layouts.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EleventyTest } from './_eleventy-test.js';
import { EleventyTest } from '#11ty-test';
import { test } from 'vitest';

const testRun = new EleventyTest('./layouts/');
Expand Down
2 changes: 1 addition & 1 deletion tests/permalinks.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EleventyTest } from './_eleventy-test.js';
import { EleventyTest } from '#11ty-test';
import { test } from 'vitest';

const testRun = new EleventyTest('./permalinks/');
Expand Down
6 changes: 2 additions & 4 deletions tests/shortcodes.test.js
Original file line number Diff line number Diff line change
@@ -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/');
Expand Down

0 comments on commit e86b81f

Please sign in to comment.