Skip to content

Commit

Permalink
feat!(snapshot): make snapshots more visually pleasing (vitest-dev#3961)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored and LorenzoBloedow committed Dec 19, 2023
1 parent 6b21ecc commit 37c79b2
Show file tree
Hide file tree
Showing 27 changed files with 578 additions and 541 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// prefer global Buffer to not initialize the whole module
"n/prefer-global/buffer": "off",
"@typescript-eslint/no-invalid-this": "off",
"@typescript-eslint/quotes": ["error", "single", {
"allowTemplateLiterals": true
}],
"no-restricted-imports": [
"error",
{
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/test/__snapshots__/suite.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Vitest Snapshot v1
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`suite name > snapshot 1`] = `
{
Expand Down
2 changes: 1 addition & 1 deletion examples/mocks/test/error-mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ vi.mock('../src/default', () => {

test('when using top level variable, gives helpful message', async () => {
await expect(() => import('../src/default').then(m => m.default)).rejects
.toThrowErrorMatchingInlineSnapshot('"[vitest] There was an error when mocking a module. If you are using \\"vi.mock\\" factory, make sure there are no top level variables inside, since this call is hoisted to top of the file. Read more: https://vitest.dev/api/vi.html#vi-mock"')
.toThrowErrorMatchingInlineSnapshot('"[vitest] There was an error when mocking a module. If you are using "vi.mock" factory, make sure there are no top level variables inside, since this call is hoisted to top of the file. Read more: https://vitest.dev/api/vi.html#vi-mock"')
})
2 changes: 1 addition & 1 deletion examples/react/test/__snapshots__/basic.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Vitest Snapshot v1
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Link changes the class when hovered 1`] = `
<a
Expand Down
2 changes: 1 addition & 1 deletion examples/vitesse/test/__snapshots__/basic.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Vitest Snapshot v1
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`mount component 1`] = `
<div
Expand Down
4 changes: 2 additions & 2 deletions examples/vue-jsx/test/__snapshots__/case.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Vitest Snapshot v1
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`mount component 1`] = `"<a-select-stub value=\\"test\\"></a-select-stub>"`;
exports[`mount component 1`] = `"<a-select-stub value="test"></a-select-stub>"`;
1 change: 0 additions & 1 deletion examples/vue/test/imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ describe('import vue components', () => {
})

test('template string imports as expected', async () => {
// eslint-disable-next-line @typescript-eslint/quotes
const cmp = await import(`../components/Hello.vue`)
expect(cmp).toBeDefined()
})
Expand Down
2 changes: 1 addition & 1 deletion examples/vue2/test/__snapshots__/basic.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Vitest Snapshot v1
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`mount component 1`] = `
"<div>
Expand Down
2 changes: 1 addition & 1 deletion examples/vue2/test/__snapshots__/script-setup.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Vitest Snapshot v1
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`mount component 1`] = `
"<div>
Expand Down
7 changes: 3 additions & 4 deletions packages/snapshot/src/port/inlineSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,10 @@ function prepareSnapString(snap: string, source: string, index: number) {
.split(/\n/g)

const isOneline = lines.length <= 1
const quote = isOneline ? '\'' : '`'
const quote = '`'
if (isOneline)
return `'${lines.join('\n').replace(/'/g, '\\\'')}'`
else
return `${quote}\n${lines.map(i => i ? indentNext + i : '').join('\n').replace(/`/g, '\\`').replace(/\${/g, '\\${')}\n${indent}${quote}`
return `${quote}${lines.join('\n').replace(/`/g, '\\`').replace(/\${/g, '\\${')}${quote}`
return `${quote}\n${lines.map(i => i ? indentNext + i : '').join('\n').replace(/`/g, '\\`').replace(/\${/g, '\\${')}\n${indent}${quote}`
}

const startRegex = /(?:toMatchInlineSnapshot|toThrowErrorMatchingInlineSnapshot)\s*\(\s*(?:\/\*[\S\s]*\*\/\s*|\/\/.*\s+)*\s*[\w_$]*(['"`\)])/m
Expand Down
1 change: 1 addition & 0 deletions packages/snapshot/src/port/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default class SnapshotState {
this.updated = 0
this._snapshotFormat = {
printBasicPrototype: false,
escapeString: false,
...options.snapshotFormat,
}
this._environment = options.snapshotEnvironment
Expand Down
1 change: 0 additions & 1 deletion test/browser/test/mocked.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ test('imports are still the same', async () => {
// @ts-expect-error typescript resolution
await expect(import('../src/calculator')).resolves.toBe(calculatorModule)
// @ts-expect-error typescript resolution
// eslint-disable-next-line @typescript-eslint/quotes
await expect(import(`../src/calculator`)).resolves.toBe(calculatorModule)
})
20 changes: 10 additions & 10 deletions test/core/test/__snapshots__/mocked.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`mocked function which fails on toReturnWith > just one call 1`] = `
"expected \\"spy\\" to return with: 2 at least once
"expected "spy" to return with: 2 at least once
Received:
Expand All @@ -16,7 +16,7 @@ Number of calls: 1
`;

exports[`mocked function which fails on toReturnWith > multi calls 1`] = `
"expected \\"spy\\" to return with: 2 at least once
"expected "spy" to return with: 2 at least once
Received:
Expand All @@ -41,29 +41,29 @@ Number of calls: 3
`;

exports[`mocked function which fails on toReturnWith > oject type 1`] = `
"expected \\"spy\\" to return with: { a: '4' } at least once
"expected "spy" to return with: { a: '4' } at least once
Received:
1st spy call return:
Object {
- \\"a\\": \\"4\\",
+ \\"a\\": \\"1\\",
- "a": "4",
+ "a": "1",
}
2nd spy call return:
Object {
- \\"a\\": \\"4\\",
+ \\"a\\": \\"1\\",
- "a": "4",
+ "a": "1",
}
3rd spy call return:
Object {
- \\"a\\": \\"4\\",
+ \\"a\\": \\"1\\",
- "a": "4",
+ "a": "1",
}
Expand All @@ -72,7 +72,7 @@ Number of calls: 3
`;

exports[`mocked function which fails on toReturnWith > zero call 1`] = `
"expected \\"spy\\" to return with: 2 at least once
"expected "spy" to return with: 2 at least once
Received:
Expand Down
14 changes: 14 additions & 0 deletions test/core/test/__snapshots__/snapshot.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ exports[`renders mock snapshot 2`] = `
}
`;
exports[`single line snapshot 1`] = `"some string"`;
exports[`single line snapshot 2`] = `"some "string""`;
exports[`single line snapshot 3`] = `"some "string"`;
exports[`single line snapshot 4`] = `"som\`e\` string"`;
exports[`single line snapshot 5`] = `"some string\`"`;
exports[`single line snapshot 6`] = `"some string'"`;
exports[`single line snapshot 7`] = `"some 'string'"`;
exports[`throwing 1`] = `"omega"`;
exports[`throwing 2`] = `"omega"`;
Expand Down
6 changes: 3 additions & 3 deletions test/core/test/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ test('displays object diff', () => {
+ Received
Object {
\\"a\\": 1,
- \\"b\\": 2,
+ \\"b\\": 3,
"a": 1,
- "b": 2,
+ "b": 3,
}
"
`)
Expand Down
Loading

0 comments on commit 37c79b2

Please sign in to comment.