-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
67 lines (57 loc) · 1.51 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* @japa/snapshot
*
* (c) Japa
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { Test } from '@japa/runner/core'
import type { PluginFn } from '@japa/runner/types'
import { isModuleInstalled } from './src/utils.js'
import { PluginContext } from './src/plugin_context.js'
import type { SnapshotPluginOptions } from './src/types.js'
/**
* Extend the Assert interface to add the snapshot method
*/
declare module '@japa/assert' {
interface Assert {
snapshot(value: any): {
matchInline(inlineSnapshot?: string): void
match(): void
}
}
}
/**
* Extend the Expect interface to add the snapshot method
*/
declare module 'expect' {
interface Matchers<R> {
toMatchSnapshot(): R
toMatchInlineSnapshot(inlineSnapshot?: string): R
}
}
/**
* Snapshot plugin for Japa
*/
export function snapshot(options: SnapshotPluginOptions = {}) {
if (isModuleInstalled('@japa/assert')) {
import('./src/integrations/assert.js')
}
if (isModuleInstalled('@japa/expect')) {
import('./src/integrations/expect.js')
}
const snapshotPlugin: PluginFn = function ({ config, cliArgs }) {
PluginContext.init(options, cliArgs)
Test.executing((test) => {
PluginContext.setCurrentTestContext(test.context)
})
/**
* Save snapshots after all tests are done
*/
config.teardown.push(async () => {
await PluginContext.snapshotManager.saveSnapshots()
})
}
return snapshotPlugin
}