Skip to content

Commit 1967be1

Browse files
authored
feat: support snapshotFormat option (#609)
1 parent f063dab commit 1967be1

File tree

16 files changed

+116
-0
lines changed

16 files changed

+116
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Rstest Snapshot v1
2+
3+
exports[`test snapshot > test snapshot generate 1`] = `
4+
Object {
5+
"a": 1,
6+
"b": Array [
7+
1,
8+
2,
9+
3,
10+
],
11+
"c": Object {
12+
"d": "hello",
13+
},
14+
}
15+
`;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, expect, it } from '@rstest/core';
2+
3+
describe('test snapshot', () => {
4+
it('test snapshot generate', () => {
5+
expect({
6+
a: 1,
7+
b: [1, 2, 3],
8+
c: {
9+
d: 'hello',
10+
},
11+
}).toMatchSnapshot();
12+
});
13+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from '@rstest/core';
2+
3+
export default defineConfig({
4+
snapshotFormat: {
5+
printBasicPrototype: true,
6+
},
7+
});

e2e/snapshot/options.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { dirname } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import { it } from '@rstest/core';
4+
import { runRstestCli } from '../scripts';
5+
6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = dirname(__filename);
8+
9+
it('snapshotFormat', async () => {
10+
const { expectExecSuccess } = await runRstestCli({
11+
command: 'rstest',
12+
args: [
13+
'run',
14+
'fixtures/options.test.ts',
15+
'-c',
16+
'fixtures/rstest.options.config.ts',
17+
],
18+
options: {
19+
nodeOptions: {
20+
cwd: __dirname,
21+
},
22+
},
23+
});
24+
25+
await expectExecSuccess();
26+
});

packages/core/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ const createDefaultConfig = (): NormalizedConfig => ({
144144
maxConcurrency: 5,
145145
printConsoleTrace: false,
146146
disableConsoleIntercept: false,
147+
snapshotFormat: {},
147148
coverage: {
148149
exclude: [
149150
'**/node_modules/**',

packages/core/src/pool/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const getRuntimeConfig = (context: ProjectContext): RuntimeConfig => {
5151
hookTimeout,
5252
isolate,
5353
coverage,
54+
snapshotFormat,
5455
} = context.normalizedConfig;
5556

5657
return {
@@ -71,6 +72,7 @@ const getRuntimeConfig = (context: ProjectContext): RuntimeConfig => {
7172
testEnvironment,
7273
isolate,
7374
coverage,
75+
snapshotFormat,
7476
};
7577
};
7678

packages/core/src/runtime/worker/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const preparePool = async ({
4646
printConsoleTrace,
4747
disableConsoleIntercept,
4848
testEnvironment,
49+
snapshotFormat,
4950
},
5051
} = context;
5152

@@ -66,6 +67,7 @@ const preparePool = async ({
6667
snapshotOptions: {
6768
updateSnapshot,
6869
snapshotEnvironment: new RstestSnapshotEnvironment(),
70+
snapshotFormat,
6971
},
7072
distPath,
7173
testPath,

packages/core/src/types/config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { RsbuildConfig } from '@rsbuild/core';
2+
import type { SnapshotStateOptions } from '@vitest/snapshot';
23
import type { CoverageOptions, NormalizedCoverageOptions } from './coverage';
34
import type {
45
BuiltInReporterNames,
@@ -24,6 +25,11 @@ export type ProjectConfig = Omit<
2425
'projects' | 'reporters' | 'pool' | 'isolate' | 'coverage'
2526
>;
2627

28+
type SnapshotFormat = Omit<
29+
NonNullable<SnapshotStateOptions['snapshotFormat']>,
30+
'plugins' | 'compareKeys'
31+
>;
32+
2733
/**
2834
* A list of glob patterns or files that match your test projects.
2935
*
@@ -209,6 +215,9 @@ export interface RstestConfig {
209215
*/
210216
onConsoleLog?: (content: string) => boolean | void;
211217

218+
/** Format snapshot output */
219+
snapshotFormat?: SnapshotFormat;
220+
212221
/**
213222
* Coverage options
214223
*/

packages/core/src/types/worker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export type RuntimeConfig = Pick<
4848
| 'isolate'
4949
| 'hookTimeout'
5050
| 'coverage'
51+
| 'snapshotFormat'
5152
>;
5253

5354
export type WorkerContext = {
@@ -77,5 +78,6 @@ export type WorkerState = WorkerContext & {
7778
snapshotOptions: {
7879
updateSnapshot: SnapshotUpdateState;
7980
snapshotEnvironment: SnapshotEnvironment;
81+
snapshotFormat: RuntimeConfig['snapshotFormat'];
8082
};
8183
};

packages/core/tests/__snapshots__/config.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ exports[`mergeRstestConfig > should merge config correctly with default config 1
6565
"./setup.ts",
6666
],
6767
"slowTestThreshold": 300,
68+
"snapshotFormat": {},
6869
"testEnvironment": "node",
6970
"testTimeout": 5000,
7071
"unstubEnvs": false,

0 commit comments

Comments
 (0)