Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions e2e/snapshot/fixtures/__snapshots__/options.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Rstest Snapshot v1

exports[`test snapshot > test snapshot generate 1`] = `
Object {
"a": 1,
"b": Array [
1,
2,
3,
],
"c": Object {
"d": "hello",
},
}
`;
13 changes: 13 additions & 0 deletions e2e/snapshot/fixtures/options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { describe, expect, it } from '@rstest/core';

describe('test snapshot', () => {
it('test snapshot generate', () => {
expect({
a: 1,
b: [1, 2, 3],
c: {
d: 'hello',
},
}).toMatchSnapshot();
});
});
7 changes: 7 additions & 0 deletions e2e/snapshot/fixtures/rstest.options.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from '@rstest/core';

export default defineConfig({
snapshotFormat: {
printBasicPrototype: true,
},
});
26 changes: 26 additions & 0 deletions e2e/snapshot/options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { it } from '@rstest/core';
import { runRstestCli } from '../scripts';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

it('snapshotFormat', async () => {
const { expectExecSuccess } = await runRstestCli({
command: 'rstest',
args: [
'run',
'fixtures/options.test.ts',
'-c',
'fixtures/rstest.options.config.ts',
],
options: {
nodeOptions: {
cwd: __dirname,
},
},
});

await expectExecSuccess();
});
1 change: 1 addition & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ const createDefaultConfig = (): NormalizedConfig => ({
maxConcurrency: 5,
printConsoleTrace: false,
disableConsoleIntercept: false,
snapshotFormat: {},
coverage: {
exclude: [
'**/node_modules/**',
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/pool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const getRuntimeConfig = (context: ProjectContext): RuntimeConfig => {
hookTimeout,
isolate,
coverage,
snapshotFormat,
} = context.normalizedConfig;

return {
Expand All @@ -71,6 +72,7 @@ const getRuntimeConfig = (context: ProjectContext): RuntimeConfig => {
testEnvironment,
isolate,
coverage,
snapshotFormat,
};
};

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/runtime/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const preparePool = async ({
printConsoleTrace,
disableConsoleIntercept,
testEnvironment,
snapshotFormat,
},
} = context;

Expand All @@ -66,6 +67,7 @@ const preparePool = async ({
snapshotOptions: {
updateSnapshot,
snapshotEnvironment: new RstestSnapshotEnvironment(),
snapshotFormat,
},
distPath,
testPath,
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { RsbuildConfig } from '@rsbuild/core';
import type { SnapshotStateOptions } from '@vitest/snapshot';
import type { CoverageOptions, NormalizedCoverageOptions } from './coverage';
import type {
BuiltInReporterNames,
Expand All @@ -24,6 +25,11 @@ export type ProjectConfig = Omit<
'projects' | 'reporters' | 'pool' | 'isolate' | 'coverage'
>;

type SnapshotFormat = Omit<
NonNullable<SnapshotStateOptions['snapshotFormat']>,
'plugins' | 'compareKeys'
>;

/**
* A list of glob patterns or files that match your test projects.
*
Expand Down Expand Up @@ -209,6 +215,9 @@ export interface RstestConfig {
*/
onConsoleLog?: (content: string) => boolean | void;

/** Format snapshot output */
snapshotFormat?: SnapshotFormat;

/**
* Coverage options
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/types/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type RuntimeConfig = Pick<
| 'isolate'
| 'hookTimeout'
| 'coverage'
| 'snapshotFormat'
>;

export type WorkerContext = {
Expand Down Expand Up @@ -77,5 +78,6 @@ export type WorkerState = WorkerContext & {
snapshotOptions: {
updateSnapshot: SnapshotUpdateState;
snapshotEnvironment: SnapshotEnvironment;
snapshotFormat: RuntimeConfig['snapshotFormat'];
};
};
1 change: 1 addition & 0 deletions packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ exports[`mergeRstestConfig > should merge config correctly with default config 1
"./setup.ts",
],
"slowTestThreshold": 300,
"snapshotFormat": {},
"testEnvironment": "node",
"testTimeout": 5000,
"unstubEnvs": false,
Expand Down
3 changes: 3 additions & 0 deletions packages/core/tests/core/__snapshots__/rstest.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ exports[`rstest context > should generate rstest context correctly 1`] = `
"root": "<ROOT>/packages/core",
"setupFiles": [],
"slowTestThreshold": 300,
"snapshotFormat": {},
"testEnvironment": "node",
"testTimeout": 5000,
"unstubEnvs": false,
Expand Down Expand Up @@ -134,6 +135,7 @@ exports[`rstest context > should generate rstest context correctly with multiple
"<ROOT>/packages/core/test-project/scripts/rstest.setup.ts",
],
"slowTestThreshold": 300,
"snapshotFormat": {},
"source": {},
"testEnvironment": "node",
"testTimeout": 5000,
Expand Down Expand Up @@ -207,6 +209,7 @@ exports[`rstest context > should generate rstest context correctly with multiple
"<ROOT>/packages/core/test-project1/scripts/rstest.setup.ts",
],
"slowTestThreshold": 300,
"snapshotFormat": {},
"source": {},
"testEnvironment": "node",
"testTimeout": 5000,
Expand Down
1 change: 1 addition & 0 deletions website/docs/en/config/test/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"coverage",
"reporters",
"slowTestThreshold",
"snapshotFormat",
"printConsoleTrace",
"onConsoleLog",
"disableConsoleIntercept"
Expand Down
16 changes: 16 additions & 0 deletions website/docs/en/config/test/snapshotFormat.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# snapshotFormat

- **Type:** `PrettyFormatOptions`
- **Default:** `undefined`

Customize the snapshot format using options from the [pretty-format](https://www.npmjs.com/package/pretty-format).

```ts title="rstest.config.ts"
import { defineConfig } from '@rstest/core';

export default defineConfig({
snapshotFormat: {
printBasicPrototype: true,
},
});
```
1 change: 1 addition & 0 deletions website/docs/zh/config/test/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"coverage",
"reporters",
"slowTestThreshold",
"snapshotFormat",
"printConsoleTrace",
"onConsoleLog",
"disableConsoleIntercept"
Expand Down
16 changes: 16 additions & 0 deletions website/docs/zh/config/test/snapshotFormat.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# snapshotFormat

- **类型:** `PrettyFormatOptions`
- **默认值:** `undefined`

使用 [pretty-format](https://www.npmjs.com/package/pretty-format) 提供的选项自定义快照格式。

```ts title="rstest.config.ts"
import { defineConfig } from '@rstest/core';

export default defineConfig({
snapshotFormat: {
printBasicPrototype: true,
},
});
```
1 change: 1 addition & 0 deletions website/theme/components/ConfigOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const OVERVIEW_GROUPS: Group[] = [
'coverage',
'reporters',
'slowTestThreshold',
'snapshotFormat',
'onConsoleLog',
'printConsoleTrace',
'disableConsoleIntercept',
Expand Down
Loading