-
Notifications
You must be signed in to change notification settings - Fork 395
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
96e2681
fix(ssr): adjacent text in `<template>`s
nolanlawson 29c42fb
test: fix expected failures
nolanlawson 846235e
test(snapshots): add helpfuler message when error/expected swap
wjhsf 996e8ba
Merge branch 'master' into wjh/better-snapshot-errors
wjhsf dff95de
Merge branch 'master' into wjh/better-snapshot-errors
wjhsf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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); | ||
|
@@ -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' | ||
); | ||
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, | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL about |
||
Error.captureStackTrace(contentErr, testFixtureDir); | ||
throw contentErr; | ||
} | ||
} | ||
} | ||
|
||
throw err; | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.