diff --git a/packages/@lwc/ssr-compiler/src/__tests__/fixtures.spec.ts b/packages/@lwc/ssr-compiler/src/__tests__/fixtures.spec.ts index b2ad2a3c39..712c400e94 100644 --- a/packages/@lwc/ssr-compiler/src/__tests__/fixtures.spec.ts +++ b/packages/@lwc/ssr-compiler/src/__tests__/fixtures.spec.ts @@ -89,14 +89,12 @@ describe.runIf(process.env.TEST_SSR_COMPILER).concurrent('fixtures', () => { { root: path.resolve(__dirname, '../../../engine-server/src/__tests__/fixtures'), pattern: '**/index.js', + // TODO [#4815]: enable all SSR v2 tests expectedFailures, }, async ({ filename, dirname, config }) => { const errorFile = config?.ssrFiles?.error ?? 'error.txt'; const expectedFile = config?.ssrFiles?.expected ?? 'expected.html'; - // TODO [#4815]: enable all SSR v2 tests - const shortFilename = filename.split('fixtures/')[1]; - const expectedFailure = expectedFailures.has(shortFilename); let compiledFixturePath; try { @@ -111,14 +109,7 @@ describe.runIf(process.env.TEST_SSR_COMPILER).concurrent('fixtures', () => { }; } - let module; - try { - module = (await import(compiledFixturePath)) as FixtureModule; - } catch (err: any) { - if (!expectedFailure) { - throw err; - } - } + const module = (await import(compiledFixturePath)) as FixtureModule; let result; try { diff --git a/scripts/test-utils/test-fixture-dir.ts b/scripts/test-utils/test-fixture-dir.ts index 7de3de9549..3b30330c86 100644 --- a/scripts/test-utils/test-fixture-dir.ts +++ b/scripts/test-utils/test-fixture-dir.ts @@ -17,23 +17,21 @@ type TestFixtureOutput = { [filename: string]: unknown }; /** * Facilitates the use of vitest's `test.only`/`test.skip` in fixture files. * @param dirname fixture directory to check for "directive" files - * @returns `test.only` if `.only` exists, `test.skip` if `.skip` exists, otherwise `test` + * @returns `{ only: true }` if `.only` exists, `{ skip: true }` if `.skip` exists, otherwise `{}` * @throws if you have both `.only` and `.skip` in the directory - * @example getTestFunc('/fixtures/some-test') + * @example getTestOptions('/fixtures/some-test') */ -function getTestFunc(dirname: string) { +function getTestOptions(dirname: string) { const isOnly = fs.existsSync(path.join(dirname, '.only')); const isSkip = fs.existsSync(path.join(dirname, '.skip')); if (isOnly && isSkip) { const relpath = path.relative(process.cwd(), dirname); throw new Error(`Cannot have both .only and .skip in ${relpath}`); } - return isOnly ? test.only : isSkip ? test.skip : test; + return isOnly ? { only: true } : isSkip ? { skip: true } : {}; } export interface TestFixtureConfig extends StyleCompilerConfig { - /** Human-readable test description. A proxy for `test(description, ...)`. */ - description?: string; /** Component name. */ name?: string; /** Component namespace. */ @@ -119,10 +117,11 @@ export function testFixtureDir( const src = fs.readFileSync(filename, 'utf-8'); const dirname = path.dirname(filename); const fixtureConfig = getFixtureConfig(dirname); - const description = fixtureConfig?.description ?? path.relative(root, filename); - const tester = getTestFunc(dirname); + const relpath = path.relative(root, filename); + const options = getTestOptions(dirname); + const fails = config.expectedFailures?.has(relpath); - tester(description, async ({ expect }) => { + test(relpath, { fails, ...options }, async ({ expect }) => { const outputs = await testFn({ src, filename, @@ -136,12 +135,6 @@ export function testFixtureDir( ); } - // TODO [#4815]: enable all SSR v2 tests - const shortFilename = filename.split('fixtures/')[1]; - const expectedFailure = config.expectedFailures?.has(shortFilename); - - let error: Error | undefined; - for (const [outputName, content] of Object.entries(outputs)) { const outputPath = path.resolve(dirname, outputName); @@ -157,16 +150,9 @@ export function testFixtureDir( // https://v8.dev/docs/stack-trace-api#stack-trace-collection-for-custom-exceptions Error.captureStackTrace(err, testFixtureDir); } - if (!error) { - error = err as Error; - } - } - } - if (expectedFailure && !error) { - throw new Error('Expected a failure in fixture: ' + shortFilename); - } else if (!expectedFailure && error) { - throw error; + throw err; + } } }); }