Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: better snapshot errors #4985

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions scripts/test-utils/test-fixture-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

import fs from 'node:fs';
import fs, { readFileSync } from 'node:fs';
import path from 'node:path';
import { AssertionError } from 'node:assert';
import { test } from 'vitest';
import * as glob from 'glob';
import type { Config as StyleCompilerConfig } from '@lwc/style-compiler';
Expand Down Expand Up @@ -145,7 +146,9 @@ export function testFixtureDir<T extends TestFixtureConfig>(
);
}

for (const [outputName, content] of Object.entries(outputs)) {
const outputsList = Object.entries(outputs);

for (const [outputName, content] of outputsList) {
const outputPath = path.resolve(dirname, outputName);
try {
await expect(content ?? '').toMatchFileSnapshot(outputPath);
Expand All @@ -156,6 +159,53 @@ export function testFixtureDir<T extends TestFixtureConfig>(
Error.captureStackTrace(err, testFixtureDir);
}

const isErrorSnapshot = outputName.startsWith('error');
const isSuccessSnapshot = outputName.startsWith('expected');

// If we change from a successful result to an error (or vice versa),
// then either the snapshot or content for the failing file is empty,
// and the diff printed in the error message isn't helpful. Here, we check for
// that case, and then check if the other file has flipped the other way.
// If it has, we throw a more helpful error message.
if (isErrorSnapshot || isSuccessSnapshot) {
const brokenResult = outputs[outputName];
const brokenResultHasContent = Boolean(brokenResult);
const brokenSnapshot = readFileSync(outputPath, 'utf8');
const brokenSnapshotHasContent = Boolean(brokenSnapshot);
if (brokenResultHasContent !== brokenSnapshotHasContent) {
// This file flipped from content to empty, or vice versa
const otherType = isErrorSnapshot ? 'expected' : 'error';
const otherName = outputsList.find(([name]) =>
name.startsWith(otherType)
)![0];
const otherResult = outputs[otherName];
const otherResultHasContent = Boolean(otherResult);
const otherSnapshot = readFileSync(
path.resolve(dirname, otherName),
'utf8'
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a shame we have to keep reading in snapshot files when presumably Vitest is already doing this elsewhere.

const otherSnapshotHasContent = Boolean(otherSnapshot);
if (otherResultHasContent !== otherSnapshotHasContent) {
// The other file has flipped from content to empty, or vice versa
const expectedContentName = brokenSnapshotHasContent
? outputName
: otherName;
const actualContentName = brokenResultHasContent
? outputName
: otherName;
const contentErr = new AssertionError({
message: `Expected content in ${expectedContentName}, but found content in ${actualContentName}.`,
expected: brokenSnapshotHasContent
? brokenSnapshot
: otherSnapshot,
actual: brokenResultHasContent ? brokenResult : otherResult,
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL about AssertionError!

Error.captureStackTrace(contentErr, testFixtureDir);
throw contentErr;
}
}
}

throw err;
}
}
Expand Down