Skip to content

Commit c23f03d

Browse files
committed
fix: add test level counters
1 parent 6f30056 commit c23f03d

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
exports[`yo`] = `"make it rain"`
1+
exports[`yo 1`]=`"make it rain"`
22

3-
exports[`yo 2`] = `Object {
4-
"name": 2,
3+
exports[`yo 2 1`]=`Object {
4+
"name": 3,
55
}`
66

7-
exports[`lols`] = `Array [
7+
exports[`yo 2 2`]=`Object {
8+
"name": 1,
9+
}`
10+
11+
exports[`lols 1`]=`Array [
812
Object {
913
"name": 1,
1014
},
1115
Object {
1216
"name": 2,
1317
},
1418
]`
19+

src/snapshot.js

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,11 @@ const REMOVED = k.red
1111
function getFileName() {
1212
const err = new Error()
1313
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(/\((.+)\)$/)
1918

20-
if (l.includes('snapshot')) {
21-
matched = stackArr[i + 1].match(/\((.+)\)$/)
22-
}
23-
})
2419
if (!matched) {
2520
return
2621
}
@@ -49,14 +44,22 @@ function getFileName() {
4944

5045
const require = createRequire(import.meta.url)
5146

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(
5354
test,
5455
currentValue,
5556
errorMsg = 'Snapshot does not match'
5657
) {
5758
const hasFileDetails = getFileName()
5859
const shouldUpdate = () => Number(process.env.UPDATE_SNAPSHOTS) === 1
60+
5961
if (!hasFileDetails) return
62+
6063
const { filename } = hasFileDetails
6164
const snapshotFileName = join(
6265
'snapshots',
@@ -69,15 +72,22 @@ export function snapsnot(
6972
snapshotName = test.name
7073
}
7174

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+
7284
if (shouldUpdate()) {
73-
process.nextTick(() =>
74-
writeSnapshot(currentValue, snapshotFileName, snapshotName)
75-
)
85+
writeSnapshot(currentValue, snapshotFileName, snapshotName)
7686
return
7787
}
7888

7989
if (existsSync(snapshotFileName)) {
80-
const module = require('./' + snapshotFileName)
90+
const module = require(join(process.cwd(), snapshotFileName))
8191
if (module[snapshotName]) {
8292
const _diff = diffTrimmedLines(format(currentValue), module[snapshotName])
8393
const hasChanges = _diff.filter(d => d.added || d.removed)
@@ -102,21 +112,20 @@ export function snapsnot(
102112
)
103113
}
104114
}
105-
process.nextTick(() =>
106-
writeSnapshot(currentValue, snapshotFileName, snapshotName)
107-
)
115+
writeSnapshot(currentValue, snapshotFileName, snapshotName)
108116
}
109117

110118
function writeSnapshot(value, file, name) {
111119
if (!existsSync(file)) {
112-
let data = (file, 'utf8')
120+
let data = ''
113121
data += '\n\n'
114122
data += `exports[\`${name}\`]=\`${format(value)}\``
115123
mkdirSync(dirname(file), { recursive: true })
116124
writeFileSync(file, data, 'utf8')
117125
return
118126
}
119-
const module = require('./' + file)
127+
const modulePath = require.resolve(join(process.cwd(), file))
128+
const module = require(modulePath)
120129
module[name] = format(value)
121130
let newContent = ''
122131
Object.keys(module).forEach(exp => {

tests/index.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { test } from 'node:test'
2-
import { snapsnot } from '../src/snapshot.js'
2+
import { snapshot } from '../src/snapshot.js'
33

44
test('yo', t => {
5-
snapsnot(t, 'make it rain')
5+
snapshot(t, 'make it rain')
66
})
77

88
test('yo 2', t => {
9-
snapsnot(t, { name: 2 })
9+
snapshot(t, { name: 3 })
10+
snapshot(t, { name: 1 })
1011
})
1112

1213
test('for', t => {
1314
t.test('lols', t => {
14-
snapsnot(t, [{ name: 1 }, { name: 2 }])
15+
snapshot(t, [{ name: 1 }, { name: 2 }])
1516
})
1617
})

0 commit comments

Comments
 (0)