Skip to content

Commit 194b14b

Browse files
fix(snapshot): strip snapshot annotation when matching inline snapshots
1 parent d2ce708 commit 194b14b

7 files changed

Lines changed: 213 additions & 170 deletions

File tree

package-lock.json

Lines changed: 149 additions & 149 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"prepare": "husky install"
5959
},
6060
"dependencies": {
61-
"@vitest/snapshot": "^3.2.1",
61+
"@vitest/snapshot": "^3.2.4",
6262
"expect": "^30.0.0",
6363
"jest-matcher-utils": "^30.0.0",
6464
"lodash.isequal": "^4.5.0"
@@ -68,18 +68,18 @@
6868
"@types/jest": "^30.0.0",
6969
"@types/lodash.isequal": "^4.5.8",
7070
"@types/node": "^24.0.3",
71-
"@vitest/coverage-v8": "^3.2.1",
71+
"@vitest/coverage-v8": "^3.2.4",
7272
"@wdio/eslint": "^0.1.1",
7373
"@wdio/types": "^9.15.0",
7474
"c8": "^10.1.3",
75-
"eslint": "^9.28.0",
75+
"eslint": "^9.29.0",
7676
"husky": "^9.1.7",
7777
"npm-run-all2": "^8.0.4",
7878
"release-it": "^19.0.3",
7979
"rimraf": "^6.0.1",
8080
"shelljs": "^0.10.0",
8181
"typescript": "^5.8.3",
82-
"vitest": "^3.2.1",
82+
"vitest": "^3.2.4",
8383
"webdriverio": "^9.15.0"
8484
},
8585
"peerDependencies": {

src/matchers/snapshot.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'node:path'
22
import type { AssertionError } from 'node:assert'
33

44
import { expect } from 'expect'
5+
import { stripSnapshotIndentation } from '@vitest/snapshot'
56
import { SnapshotService } from '../snapshot.js'
67

78
interface InlineSnapshotOptions {
@@ -146,13 +147,27 @@ export function toMatchInlineSnapshot(received: unknown, inlineSnapshot: string,
146147
...stack.slice(3)
147148
].join('\n')
148149
}
149-
error.stack = error.stack?.split('\n').filter((line) => (
150+
const trace = error.stack?.split('\n').filter((line) => (
150151
line.includes('__INLINE_SNAPSHOT__') ||
151152
!(
152153
line.includes('__EXTERNAL_MATCHER_TRAP__') ||
153154
line.includes(`expect-webdriverio${path.sep}lib${path.sep}matchers${path.sep}snapshot.js:`)
154155
)
155-
)).join('\n')
156+
)) || []
157+
158+
/**
159+
* tweak the stack trace to enable inline snapshot testing within this projects
160+
* unit tests
161+
*/
162+
if (process.env.WDIO_INTERNAL_TEST) {
163+
trace.splice(2, 1)
164+
}
165+
166+
if (inlineSnapshot) {
167+
inlineSnapshot = stripSnapshotIndentation(inlineSnapshot)
168+
}
169+
170+
error.stack = trace.join('\n')
156171
return toMatchSnapshotHelper(received, message, {
157172
inlineSnapshot,
158173
error

src/snapshot.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import type { Services, Frameworks } from '@wdio/types'
99
*/
1010
let service: SnapshotService
1111

12+
export type SnapshotFormat = SnapshotStateOptions['snapshotFormat']
1213
type ResolveSnapshotPathFunction = (path: string, extension: string) => string
1314
interface SnapshotServiceArgs {
1415
updateState?: SnapshotUpdateState
1516
resolveSnapshotPath?: ResolveSnapshotPathFunction
17+
snapshotFormat?: SnapshotFormat
1618
}
1719

1820
class WebdriverIOSnapshotEnvironment extends NodeSnapshotEnvironment {
@@ -54,9 +56,18 @@ export class SnapshotService implements Services.ServiceInstance {
5456
: options?.updateState
5557
? options.updateState
5658
: 'new'
59+
60+
// Only set snapshotFormat if user provides explicit options
61+
const snapshotFormatConfig = options?.snapshotFormat ? {
62+
printBasicPrototype: false,
63+
escapeString: false,
64+
...options.snapshotFormat
65+
} : undefined
66+
5767
this.#options = {
5868
updateSnapshot,
59-
snapshotEnvironment: new WebdriverIOSnapshotEnvironment(options?.resolveSnapshotPath)
69+
snapshotEnvironment: new WebdriverIOSnapshotEnvironment(options?.resolveSnapshotPath),
70+
...(snapshotFormatConfig && { snapshotFormat: snapshotFormatConfig })
6071
} as const
6172
}
6273

test/file.snap

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/snapshot.test.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { Frameworks } from '@wdio/types'
77
import { expect as expectExport, SnapshotService } from '../src/index.js'
88

99
const __dirname = path.dirname(fileURLToPath(import.meta.url))
10+
const __filename = path.basename(fileURLToPath(import.meta.url))
1011

1112
const service = SnapshotService.initiate({
1213
resolveSnapshotPath: (path, extension) => path + extension
@@ -16,21 +17,28 @@ test('supports snapshot testing', async () => {
1617
await service.beforeTest({
1718
title: 'test',
1819
parent: 'parent',
19-
file: `${__dirname}/file`,
20+
file: path.join(__dirname, __filename),
2021
} as Frameworks.Test)
2122

23+
process.env.WDIO_INTERNAL_TEST = 'true'
24+
2225
const exp = expectExport
2326
expect(exp).toBeDefined()
2427
expect(exp({}).toMatchSnapshot).toBeDefined()
2528
expect(exp({}).toMatchInlineSnapshot).toBeDefined()
2629
await exp({ a: 'a' }).toMatchSnapshot()
27-
/**
28-
* doesn't work without running in WebdriverIO test runner context
29-
*/
30-
// await exp({ a: 'a' }).toMatchInlineSnapshot()
30+
await exp({ deep: { nested: { object: 'value' } } }).toMatchInlineSnapshot(`
31+
{
32+
"deep": {
33+
"nested": {
34+
"object": "value",
35+
},
36+
},
37+
}
38+
`)
3139
await service.after()
3240

33-
const expectedSnapfileExist = await fs.access(path.resolve(__dirname, 'file.snap'))
41+
const expectedSnapfileExist = await fs.access(path.resolve(__dirname, 'snapshot.test.ts.snap'))
3442
.then(() => true, () => false)
3543
expect(expectedSnapfileExist).toBe(true)
3644
})
@@ -54,4 +62,3 @@ test('supports cucumber snapshot testing', async () => {
5462
.then(() => true, () => false)
5563
expect(expectedSnapfileExist).toBe(true)
5664
})
57-

test/snapshot.test.ts.snap

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Snapshot v1
2+
3+
exports[`parent > test 1`] = `
4+
{
5+
"a": "a",
6+
}
7+
`;
8+
9+
exports[`parent > test 2`] = `
10+
{
11+
"deep": {
12+
"nested": {
13+
"object": "value",
14+
},
15+
},
16+
}
17+
`;

0 commit comments

Comments
 (0)