@@ -11,16 +11,11 @@ const REMOVED = k.red
11
11
function getFileName ( ) {
12
12
const err = new Error ( )
13
13
const stackArr = err . stack . split ( '\n' )
14
- let matched
15
- stackArr . forEach ( ( l , i ) => {
16
- if ( l . toLowerCase ( ) === 'error' ) {
17
- return false
18
- }
14
+ const snapshotLineIndex = stackArr . findIndex (
15
+ l => l . trim ( ) . indexOf ( 'at snapshot' ) > - 1
16
+ )
17
+ const matched = stackArr [ snapshotLineIndex + 1 ] . match ( / \( ( .+ ) \) $ / )
19
18
20
- if ( l . includes ( 'snapshot' ) ) {
21
- matched = stackArr [ i + 1 ] . match ( / \( ( .+ ) \) $ / )
22
- }
23
- } )
24
19
if ( ! matched ) {
25
20
return
26
21
}
@@ -49,14 +44,22 @@ function getFileName() {
49
44
50
45
const require = createRequire ( import . meta. url )
51
46
52
- export function snapsnot (
47
+ let fileTestCounter = new Map ( )
48
+
49
+ function getFileCounterKey ( filename , testName ) {
50
+ return `${ filename } :${ testName } `
51
+ }
52
+
53
+ export function snapshot (
53
54
test ,
54
55
currentValue ,
55
56
errorMsg = 'Snapshot does not match'
56
57
) {
57
58
const hasFileDetails = getFileName ( )
58
59
const shouldUpdate = ( ) => Number ( process . env . UPDATE_SNAPSHOTS ) === 1
60
+
59
61
if ( ! hasFileDetails ) return
62
+
60
63
const { filename } = hasFileDetails
61
64
const snapshotFileName = join (
62
65
'snapshots' ,
@@ -69,15 +72,22 @@ export function snapsnot(
69
72
snapshotName = test . name
70
73
}
71
74
75
+ const fileCounterKey = getFileCounterKey ( filename , test . fullName ?? test . name )
76
+ if ( ! fileTestCounter . has ( fileCounterKey ) ) {
77
+ fileTestCounter . set ( fileCounterKey , 0 )
78
+ }
79
+
80
+ const currentCount = fileTestCounter . get ( fileCounterKey )
81
+ snapshotName += ' ' + ( Number ( currentCount ) + 1 )
82
+ fileTestCounter . set ( fileCounterKey , Number ( currentCount ) + 1 )
83
+
72
84
if ( shouldUpdate ( ) ) {
73
- process . nextTick ( ( ) =>
74
- writeSnapshot ( currentValue , snapshotFileName , snapshotName )
75
- )
85
+ writeSnapshot ( currentValue , snapshotFileName , snapshotName )
76
86
return
77
87
}
78
88
79
89
if ( existsSync ( snapshotFileName ) ) {
80
- const module = require ( './' + snapshotFileName )
90
+ const module = require ( join ( process . cwd ( ) , snapshotFileName ) )
81
91
if ( module [ snapshotName ] ) {
82
92
const _diff = diffTrimmedLines ( format ( currentValue ) , module [ snapshotName ] )
83
93
const hasChanges = _diff . filter ( d => d . added || d . removed )
@@ -102,21 +112,20 @@ export function snapsnot(
102
112
)
103
113
}
104
114
}
105
- process . nextTick ( ( ) =>
106
- writeSnapshot ( currentValue , snapshotFileName , snapshotName )
107
- )
115
+ writeSnapshot ( currentValue , snapshotFileName , snapshotName )
108
116
}
109
117
110
118
function writeSnapshot ( value , file , name ) {
111
119
if ( ! existsSync ( file ) ) {
112
- let data = ( file , 'utf8' )
120
+ let data = ''
113
121
data += '\n\n'
114
122
data += `exports[\`${ name } \`]=\`${ format ( value ) } \``
115
123
mkdirSync ( dirname ( file ) , { recursive : true } )
116
124
writeFileSync ( file , data , 'utf8' )
117
125
return
118
126
}
119
- const module = require ( './' + file )
127
+ const modulePath = require . resolve ( join ( process . cwd ( ) , file ) )
128
+ const module = require ( modulePath )
120
129
module [ name ] = format ( value )
121
130
let newContent = ''
122
131
Object . keys ( module ) . forEach ( exp => {
0 commit comments