Skip to content

Commit 1a3cbff

Browse files
committed
test(react): add codePath snapshot serializer
1 parent d520a3f commit 1a3cbff

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

packages/@livestore/react/src/__snapshots__/useClientDocument.test.tsx.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ exports[`useClientDocument > otel > should update the data based on component ke
3737
{
3838
"_name": "client-session-sync-processor:pull",
3939
"attributes": {
40-
"code.stacktrace": "at /Users/schickling/Code/overtone/submodules/livestore/packages/@livestore/common/src/sync/ClientSessionSyncProcessor.ts:297:14",
40+
"code.stacktrace": "at <REPO_DIR>/packages/@livestore/common/src/sync/ClientSessionSyncProcessor.ts:<LINE>:<COLUMN>",
4141
"span.label": "⚠︎ Interrupted",
4242
"status.interrupted": true,
4343
},
@@ -263,7 +263,7 @@ exports[`useClientDocument > otel > should update the data based on component ke
263263
{
264264
"_name": "client-session-sync-processor:pull",
265265
"attributes": {
266-
"code.stacktrace": "at /Users/schickling/Code/overtone/submodules/livestore/packages/@livestore/common/src/sync/ClientSessionSyncProcessor.ts:297:14",
266+
"code.stacktrace": "at <REPO_DIR>/packages/@livestore/common/src/sync/ClientSessionSyncProcessor.ts:<LINE>:<COLUMN>",
267267
"span.label": "⚠︎ Interrupted",
268268
"status.interrupted": true,
269269
},
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import fs from 'node:fs'
2+
import path from 'node:path'
3+
4+
/**
5+
* Find the root directory of a Git repo by walking up from a start directory.
6+
*
7+
* @param {string} [startDir=process.cwd()] – the directory from which to start the search
8+
* @returns {string|null} – the path to the repo root, or null if none is found
9+
*/
10+
const findRepoRootDir = (startDir = process.cwd()) => {
11+
let dir = path.resolve(startDir)
12+
13+
while (dir !== path.dirname(dir)) {
14+
const gitPath = path.join(dir, '.git')
15+
16+
if (fs.existsSync(gitPath) && fs.statSync(gitPath).isDirectory()) {
17+
return dir
18+
}
19+
20+
dir = path.dirname(dir)
21+
}
22+
23+
return null
24+
}
25+
26+
const repoRootDir = findRepoRootDir()
27+
if (!repoRootDir) throw new Error('Could not find the root directory of the Git repository.')
28+
const lineColumnRegex = /(?<fileExt>\.\w+):\d+:\d+/g
29+
30+
/**
31+
* Vitest {@link https://vitest.dev/guide/snapshot.html#custom-serializer | custom snapshot serializer} that replaces user-specific parts of a code path with placeholders.
32+
*/
33+
export default {
34+
test: (value) => typeof value === 'string' && value.includes(repoRootDir) && lineColumnRegex.test(value),
35+
36+
serialize: (value, config, indentation, depth, refs, printer) => {
37+
const sanitized = value
38+
.replaceAll(repoRootDir, '<REPO_DIR>')
39+
.replaceAll(lineColumnRegex, '$<fileExt>:<LINE>:<COLUMN>')
40+
return printer(sanitized, config, indentation, depth, refs)
41+
},
42+
}

packages/@livestore/react/vitest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default defineConfig({
44
test: {
55
// Needed for React hook tests
66
environment: 'jsdom',
7+
snapshotSerializers: ['./src/__tests__/serializers/codePath.js'],
78
},
89
esbuild: {
910
// TODO remove once `using` keyword supported OOTB with Vite https://github.com/vitejs/vite/issues/15464#issuecomment-1872485703

0 commit comments

Comments
 (0)