Skip to content

Commit

Permalink
chore(ssr): use test options for expected failures (#4844)
Browse files Browse the repository at this point in the history
  • Loading branch information
cardoso authored Nov 13, 2024
1 parent 24fb303 commit 2efbadf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 35 deletions.
13 changes: 2 additions & 11 deletions packages/@lwc/ssr-compiler/src/__tests__/fixtures.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
34 changes: 10 additions & 24 deletions scripts/test-utils/test-fixture-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -119,10 +117,11 @@ export function testFixtureDir<T extends TestFixtureConfig>(
const src = fs.readFileSync(filename, 'utf-8');
const dirname = path.dirname(filename);
const fixtureConfig = getFixtureConfig<T>(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,
Expand All @@ -136,12 +135,6 @@ export function testFixtureDir<T extends TestFixtureConfig>(
);
}

// 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);

Expand All @@ -157,16 +150,9 @@ export function testFixtureDir<T extends TestFixtureConfig>(
// 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;
}
}
});
}
Expand Down

0 comments on commit 2efbadf

Please sign in to comment.