Skip to content

Commit f1c20c6

Browse files
chenjiahan9aoy
andauthored
test: migrate to Rstest (#55)
Co-authored-by: 9aoy <9aoyuao@gmail.com>
1 parent cc01766 commit f1c20c6

10 files changed

Lines changed: 353 additions & 1754 deletions

package.json

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"dev": "rslib build -w",
2727
"lint": "echo lint",
2828
"test": "pnpm build && pnpm test:unit && pnpm test:e2e",
29-
"test:unit": "cross-env jest --config=test/unit/jest.config.js",
29+
"test:unit": "rstest --config=test/unit/rstest.config.ts",
3030
"test:e2e": "cd test/e2e && pnpm test",
3131
"test:e2e:setup": "cd test/e2e && pnpm run setup",
3232
"precommit": "pnpm build && pnpm test:unit",
@@ -52,24 +52,18 @@
5252
}
5353
},
5454
"devDependencies": {
55-
"@jest/console": "^27.5.1",
5655
"@rslib/core": "^0.18.2",
5756
"@rspack/core": "^1.6.5",
57+
"@rstest/core": "^0.7.5",
5858
"@types/babel__code-frame": "^7.0.6",
5959
"@types/is-glob": "^4.0.4",
60-
"@types/jest": "^27.5.2",
6160
"@types/mock-fs": "^4.13.4",
6261
"@types/node": "^16.18.126",
6362
"cross-env": "^10.1.0",
6463
"husky": "^9.1.7",
65-
"jest": "^27.5.1",
66-
"jest-circus": "^27.5.1",
67-
"jest-environment-node": "^27.5.1",
68-
"jest-runner": "^27.5.1",
6964
"mock-fs": "^5.5.0",
7065
"prettier": "^3.7.3",
7166
"strip-ansi": "^6.0.1",
72-
"ts-jest": "^27.1.5",
7367
"typescript": "^5.9.3"
7468
},
7569
"publishConfig": {

pnpm-lock.yaml

Lines changed: 284 additions & 1712 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/tsconfig.json

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
{
2-
"compilerOptions": {
3-
"target": "es6",
4-
"module": "commonjs",
5-
"lib": ["ES6"],
6-
"moduleResolution": "node",
7-
"esModuleInterop": true,
8-
"skipLibCheck": true,
9-
"skipDefaultLibCheck": true,
10-
"resolveJsonModule": true,
11-
"baseUrl": "../"
12-
},
13-
"include": ["../src", "**/*.spec.ts"],
14-
"exclude": ["node_modules"]
2+
"compilerOptions": {
3+
"target": "es6",
4+
"module": "ESNext",
5+
"lib": ["ES6"],
6+
"moduleResolution": "Bundler",
7+
"esModuleInterop": true,
8+
"skipLibCheck": true,
9+
"skipDefaultLibCheck": true,
10+
"resolveJsonModule": true,
11+
"types": ["@rstest/core/globals"],
12+
"paths": {
13+
"src": ["../src"]
14+
},
15+
"baseUrl": "../"
16+
},
17+
"include": ["../src", "**/*.spec.ts"],
18+
"exclude": ["node_modules"]
1519
}

test/unit/rpc/wrap-rpc.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ describe('wrapRpc', () => {
1313
childProcessMock = {
1414
connected: true,
1515
pid: 1234,
16-
send: jest.fn((message, callback) => {
16+
send: rs.fn((message, callback) => {
1717
messageIds.push(message?.id);
1818
callback();
1919
}),
20-
on: jest.fn((name, handlerToAdd) => {
20+
on: rs.fn((name, handlerToAdd) => {
2121
if (!eventHandlers[name]) {
2222
eventHandlers[name] = [];
2323
}
2424
eventHandlers[name].push(handlerToAdd);
2525
}),
26-
off: jest.fn((name, handlerToRemove) => {
26+
off: rs.fn((name, handlerToRemove) => {
2727
if (!eventHandlers[name]) {
2828
return;
2929
}
@@ -105,7 +105,7 @@ describe('wrapRpc', () => {
105105
value: 41,
106106
});
107107

108-
expect(promise).resolves.toEqual(41);
108+
await expect(promise).resolves.toEqual(41);
109109
expect(eventHandlers).toEqual({
110110
message: [],
111111
close: [],
@@ -126,20 +126,20 @@ describe('wrapRpc', () => {
126126
error: 'sad error',
127127
});
128128

129-
expect(promise).rejects.toEqual('sad error');
129+
await expect(promise).rejects.toEqual('sad error');
130130
expect(eventHandlers).toEqual({
131131
message: [],
132132
close: [],
133133
});
134134
});
135135

136136
it('rejects on send error', async () => {
137-
(childProcessMock.send as jest.Mock).mockImplementation((message, callback) =>
137+
(childProcessMock.send as rs.Mock).mockImplementation((message, callback) =>
138138
callback(new Error('cannot send'))
139139
);
140140
const wrapped = wrapRpc<() => void>(childProcessMock);
141141

142-
expect(wrapped()).rejects.toEqual(new Error('cannot send'));
142+
await expect(wrapped()).rejects.toEqual(new Error('cannot send'));
143143
expect(eventHandlers).toEqual({
144144
message: [],
145145
close: [],
@@ -158,7 +158,7 @@ describe('wrapRpc', () => {
158158

159159
triggerClose(code, signal);
160160

161-
expect(promise).rejects.toEqual(new RpcExitError(message, code, signal));
161+
await expect(promise).rejects.toEqual(new RpcExitError(message, code, signal));
162162
expect(eventHandlers).toEqual({
163163
message: [],
164164
close: [],

test/unit/rstest.config.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { defineConfig } from '@rstest/core';
2+
3+
// Disable color in test
4+
process.env.NO_COLOR = '1';
5+
process.env.FORCE_COLOR = '0';
6+
7+
export default defineConfig({
8+
root: __dirname,
9+
globals: true,
10+
source: {
11+
tsconfigPath: '../tsconfig.json',
12+
},
13+
output: {
14+
module: true,
15+
}
16+
});

test/unit/typescript/type-script-support.spec.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import os from 'node:os';
2-
2+
import fs from 'node:fs';
33
import type { TypeScriptWorkerConfig } from 'src/typescript/type-script-worker-config';
44

55
describe('typescript/type-script-support', () => {
66
let configuration: TypeScriptWorkerConfig;
77

88
beforeEach(() => {
9-
jest.resetModules();
9+
rs.resetModules();
1010

1111
configuration = {
1212
configFile: './tsconfig.json',
@@ -28,18 +28,20 @@ describe('typescript/type-script-support', () => {
2828
});
2929

3030
it('throws error if typescript is not installed', async () => {
31-
jest.setMock('typescript', undefined);
32-
3331
const { assertTypeScriptSupport } = await import('src/typescript/type-script-support');
3432

35-
expect(() => assertTypeScriptSupport(configuration)).toThrowError(
33+
expect(() => assertTypeScriptSupport({
34+
...configuration,
35+
typescriptPath: 'typescript-404',
36+
})).toThrowError(
3637
'When you use TsCheckerRspackPlugin with typescript reporter enabled, you must install `typescript` package.'
3738
);
3839
});
3940

4041
it('throws error if there is no tsconfig.json file', async () => {
41-
jest.setMock('typescript', { version: '3.8.0' });
42-
jest.setMock('node:fs', { existsSync: () => false });
42+
rs.mock('typescript', () => ({ version: '3.8.0' }));
43+
44+
rs.spyOn(fs, 'existsSync').mockImplementation(() => false);
4345

4446
const { assertTypeScriptSupport } = await import('src/typescript/type-script-support');
4547

test/unit/typescript/type-script-worker-config.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('typescript/type-scripts-worker-config', () => {
3434
} as rspack.Compiler;
3535
});
3636
afterEach(() => {
37-
jest.resetModules();
37+
rs.resetModules();
3838
});
3939

4040
it.each([

test/unit/utils/async/pool.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ describe('createPool', () => {
1515

1616
it('limits concurrency', async () => {
1717
const pool = createPool(2);
18-
const shortTask = jest.fn(() => wait(10));
19-
const longTask = jest.fn(() => wait(500));
18+
const shortTask = rs.fn(() => wait(10));
19+
const longTask = rs.fn(() => wait(500));
2020

2121
pool.submit(shortTask);
2222
pool.submit(shortTask);
@@ -37,7 +37,7 @@ describe('createPool', () => {
3737

3838
it('works after draining', async () => {
3939
const pool = createPool(2);
40-
const shortTask = jest.fn(() => wait(10));
40+
const shortTask = rs.fn(() => wait(10));
4141

4242
pool.submit(shortTask);
4343
pool.submit(shortTask);

test/unit/utils/path/is-inside-another-path-unix.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { isInsideAnotherPath } from '../../../../src/utils/path/is-inside-another-path';
22

3-
jest.mock('node:path', () => jest.requireActual('node:path').posix);
3+
const mockedPath = rs.hoisted(() => {
4+
const path = rs.requireActual('node:path');
5+
return path.posix;
6+
});
7+
8+
rs.mock('node:path', () => mockedPath);
49

510
const unixTests: [string, string, boolean][] = [
611
// Identical

test/unit/utils/path/is-inside-another-path-windows.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { isInsideAnotherPath } from '../../../../src/utils/path/is-inside-another-path';
22

3-
jest.mock('node:path', () => jest.requireActual('node:path').win32);
3+
// fixed https://github.com/web-infra-dev/rstest/issues/785
4+
const mockedPath = rs.hoisted(() => {
5+
const path = rs.requireActual('node:path');
6+
return path.win32;
7+
});
8+
9+
rs.mock('node:path', () => mockedPath);
410

511
const windowsTests: [string, string, boolean][] = [
612
// subfolder

0 commit comments

Comments
 (0)