Skip to content

Commit 16c214b

Browse files
authored
feat: support customize the chai config via chaiConfig configuration. (#683)
1 parent e2b4afb commit 16c214b

File tree

15 files changed

+138
-7
lines changed

15 files changed

+138
-7
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { describe, expect, it } from '@rstest/core';
2+
3+
describe('Index', () => {
4+
it('test truncateThreshold', () => {
5+
expect([1, 2, [3, [4], { a: 1, length: 1 }]]).toStrictEqual([1, 2, [3]]);
6+
});
7+
});
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+
chaiConfig: {
5+
truncateThreshold: 100,
6+
},
7+
});

e2e/chaiConfigs/index.test.ts

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

examples/node/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"build": "rsbuild build",
77
"test": "rstest",
8+
"test:coverage": "rstest --coverage",
89
"test:watch": "rstest --watch"
910
},
1011
"devDependencies": {

examples/node/rstest.config.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
import { defineConfig } from '@rstest/core';
22

3-
export default defineConfig({
4-
coverage: {
5-
enabled: true,
6-
provider: 'istanbul',
7-
},
8-
});
3+
export default defineConfig({});

packages/core/src/pool/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const getRuntimeConfig = (context: ProjectContext): RuntimeConfig => {
5555
snapshotFormat,
5656
env,
5757
logHeapUsage,
58+
chaiConfig,
5859
} = context.normalizedConfig;
5960

6061
return {
@@ -78,6 +79,7 @@ const getRuntimeConfig = (context: ProjectContext): RuntimeConfig => {
7879
coverage,
7980
snapshotFormat,
8081
logHeapUsage,
82+
chaiConfig,
8183
};
8284
};
8385

packages/core/src/runtime/api/expect.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
import * as chai from 'chai';
3232
import type {
3333
Assertion,
34+
ChaiConfig,
3435
MatcherState,
3536
RstestExpect,
3637
TestCase,
@@ -45,6 +46,10 @@ chai.use(SnapshotPlugin);
4546
chai.use(JestAsymmetricMatchers);
4647
export { GLOBAL_EXPECT };
4748

49+
export function setupChaiConfig(config: ChaiConfig): void {
50+
Object.assign(chai.config, config);
51+
}
52+
4853
export function createExpect({
4954
getCurrentTest,
5055
workerState,

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
WorkerState,
99
} from '../../types';
1010
import { createRunner } from '../runner';
11-
import { assert, createExpect, GLOBAL_EXPECT } from './expect';
11+
import { assert, createExpect, GLOBAL_EXPECT, setupChaiConfig } from './expect';
1212
import { createRstestUtilities } from './utilities';
1313

1414
export const createRstestRuntime = (
@@ -27,6 +27,10 @@ export const createRstestRuntime = (
2727
} => {
2828
const { runner, api: runnerAPI } = createRunner({ workerState });
2929

30+
if (workerState.runtimeConfig.chaiConfig) {
31+
setupChaiConfig(workerState.runtimeConfig.chaiConfig);
32+
}
33+
3034
const expect: RstestExpect = createExpect({
3135
workerState,
3236
getCurrentTest: () => runner.getCurrentTest(),

packages/core/src/types/config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import type { RsbuildConfig } from '@rsbuild/core';
22
import type { SnapshotStateOptions } from '@vitest/snapshot';
3+
import type { config } from 'chai';
34
import type { CoverageOptions, NormalizedCoverageOptions } from './coverage';
45
import type {
56
BuiltInReporterNames,
67
Reporter,
78
ReporterWithOptions,
89
} from './reporter';
910

11+
export type ChaiConfig = Partial<
12+
Omit<typeof config, 'useProxy' | 'proxyExcludedKeys' | 'deepEqual'>
13+
>;
14+
1015
export type RstestPoolType = 'forks';
1116

1217
export type RstestPoolOptions = {
@@ -251,6 +256,11 @@ export interface RstestConfig {
251256
*/
252257
coverage?: CoverageOptions;
253258

259+
/**
260+
* chai configuration options
261+
*/
262+
chaiConfig?: ChaiConfig;
263+
254264
// Rsbuild configs
255265

256266
plugins?: RsbuildConfig['plugins'];
@@ -290,6 +300,7 @@ type OptionalKeys =
290300
| 'tools'
291301
| 'dev'
292302
| 'onConsoleLog'
303+
| 'chaiConfig'
293304
| 'resolveSnapshotPath';
294305

295306
export type NormalizedConfig = Required<

packages/core/src/types/worker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export type RuntimeConfig = Pick<
4949
| 'snapshotFormat'
5050
| 'env'
5151
| 'logHeapUsage'
52+
| 'chaiConfig'
5253
>;
5354

5455
export type WorkerContext = {

0 commit comments

Comments
 (0)