Skip to content

Commit 860dba6

Browse files
authored
chore: add type check for test files (#681)
1 parent bfb62e0 commit 860dba6

File tree

12 files changed

+90
-33
lines changed

12 files changed

+90
-33
lines changed

.github/workflows/test.yml

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ jobs:
7676
- name: Install Dependencies
7777
run: pnpm install
7878

79+
- name: Type Check
80+
run: pnpm run type-check
81+
7982
- name: Unit Test
8083
run: pnpm run test:unit
8184

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"test:unit": "vitest run --project unit*",
2323
"test:unit:watch": "vitest --project unit*",
2424
"testu": "pnpm run test:unit -u && pnpm run test:integration -u",
25+
"type-check": "pnpm -r run type-check",
2526
"update:rsbuild": "npx taze minor --include /rsbuild/ -w -r -l",
2627
"watch": "pnpm build --watch"
2728
},

packages/core/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"scripts": {
3535
"build": "rslib build",
3636
"dev": "rslib build --watch",
37-
"prebundle": "prebundle"
37+
"prebundle": "prebundle",
38+
"type-check": "tsc --noEmit && tsc --noEmit -p tests"
3839
},
3940
"dependencies": {
4041
"@rsbuild/core": "1.2.0-beta.0",

packages/core/src/utils/syntax.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const RSPACK_TARGET_UNLISTED_MODERN_ECMA_VERSIONS: EcmaScriptVersion[] = [
4848
*/
4949
export const ESX_TO_BROWSERSLIST: Record<
5050
FixedEcmaVersions,
51-
Record<string, string | string[]>
51+
Record<string, string>
5252
> &
5353
Record<LatestEcmaVersions, (target: RsbuildConfigOutputTarget) => string[]> =
5454
{

packages/core/tests/config.test.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ describe('syntax', () => {
222222
const composedRsbuildConfig = await composeCreateRsbuildConfig(rslibConfig);
223223

224224
expect(
225-
composedRsbuildConfig[0].config.output?.overrideBrowserslist,
225+
composedRsbuildConfig[0]!.config.output?.overrideBrowserslist,
226226
).toMatchInlineSnapshot(`
227227
[
228228
"last 1 node versions",
@@ -245,7 +245,7 @@ describe('syntax', () => {
245245
const composedRsbuildConfig = await composeCreateRsbuildConfig(rslibConfig);
246246

247247
expect(
248-
composedRsbuildConfig[0].config.output?.overrideBrowserslist,
248+
composedRsbuildConfig[0]!.config.output?.overrideBrowserslist,
249249
).toMatchInlineSnapshot(`
250250
[
251251
"last 1 Chrome versions",
@@ -273,7 +273,7 @@ describe('syntax', () => {
273273
const composedRsbuildConfig = await composeCreateRsbuildConfig(rslibConfig);
274274

275275
expect(
276-
composedRsbuildConfig[0].config.output?.overrideBrowserslist,
276+
composedRsbuildConfig[0]!.config.output?.overrideBrowserslist,
277277
).toMatchInlineSnapshot(`
278278
[
279279
"last 1 node versions",
@@ -294,7 +294,7 @@ describe('syntax', () => {
294294
const composedRsbuildConfig = await composeCreateRsbuildConfig(rslibConfig);
295295

296296
expect(
297-
composedRsbuildConfig[0].config.output?.overrideBrowserslist,
297+
composedRsbuildConfig[0]!.config.output?.overrideBrowserslist,
298298
).toMatchInlineSnapshot(`
299299
[
300300
"chrome >= 63.0.0",
@@ -322,7 +322,7 @@ describe('minify', () => {
322322
const composedRsbuildConfig = await composeCreateRsbuildConfig(rslibConfig);
323323

324324
expect(
325-
composedRsbuildConfig[0].config.output?.minify,
325+
composedRsbuildConfig[0]!.config.output?.minify,
326326
).toMatchInlineSnapshot(`
327327
{
328328
"css": false,
@@ -380,15 +380,15 @@ describe('minify', () => {
380380
const composedRsbuildConfig = await composeCreateRsbuildConfig(rslibConfig);
381381

382382
expect(
383-
composedRsbuildConfig[0].config.output?.minify,
383+
composedRsbuildConfig[0]!.config.output?.minify,
384384
).toMatchInlineSnapshot('false');
385385

386386
expect(
387-
composedRsbuildConfig[1].config.output?.minify,
387+
composedRsbuildConfig[1]!.config.output?.minify,
388388
).toMatchInlineSnapshot('true');
389389

390390
expect(
391-
composedRsbuildConfig[2].config.output?.minify,
391+
composedRsbuildConfig[2]!.config.output?.minify,
392392
).toMatchInlineSnapshot(`
393393
{
394394
"css": true,

packages/core/tests/extension.test.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ describe('should get extension correctly', () => {
1414
it('autoExtension is false', () => {
1515
const options: Options = {
1616
format: 'cjs',
17-
pkgJson: {},
17+
pkgJson: {
18+
name: 'foo',
19+
},
1820
autoExtension: false,
1921
};
2022

@@ -44,6 +46,7 @@ describe('should get extension correctly', () => {
4446
const options: Options = {
4547
format: 'cjs',
4648
pkgJson: {
49+
name: 'foo',
4750
type: 'module',
4851
},
4952
autoExtension: true,
@@ -62,6 +65,7 @@ describe('should get extension correctly', () => {
6265
const options: Options = {
6366
format: 'cjs',
6467
pkgJson: {
68+
name: 'foo',
6569
type: 'commonjs',
6670
},
6771
autoExtension: true,
@@ -80,6 +84,7 @@ describe('should get extension correctly', () => {
8084
const options: Options = {
8185
format: 'esm',
8286
pkgJson: {
87+
name: 'foo',
8388
type: 'commonjs',
8489
},
8590
autoExtension: true,
@@ -98,6 +103,7 @@ describe('should get extension correctly', () => {
98103
const options: Options = {
99104
format: 'esm',
100105
pkgJson: {
106+
name: 'foo',
101107
type: 'module',
102108
},
103109
autoExtension: true,

packages/core/tests/external.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ vi.mock('rslog');
66
describe('should composeAutoExternalConfig correctly', () => {
77
it('autoExternal default value', () => {
88
const esmResult = composeAutoExternalConfig({
9+
bundle: true,
910
format: 'esm',
1011
autoExternal: undefined,
1112
pkgJson: {
@@ -17,6 +18,7 @@ describe('should composeAutoExternalConfig correctly', () => {
1718
});
1819

1920
const cjsResult = composeAutoExternalConfig({
21+
bundle: true,
2022
format: 'cjs',
2123
autoExternal: undefined,
2224
pkgJson: {
@@ -28,6 +30,7 @@ describe('should composeAutoExternalConfig correctly', () => {
2830
});
2931

3032
const umdResult = composeAutoExternalConfig({
33+
bundle: true,
3134
format: 'umd',
3235
autoExternal: undefined,
3336
pkgJson: {
@@ -39,6 +42,7 @@ describe('should composeAutoExternalConfig correctly', () => {
3942
});
4043

4144
const mfResult = composeAutoExternalConfig({
45+
bundle: true,
4246
format: 'mf',
4347
autoExternal: undefined,
4448
pkgJson: {
@@ -77,6 +81,7 @@ describe('should composeAutoExternalConfig correctly', () => {
7781

7882
it('autoExternal is true', () => {
7983
const result = composeAutoExternalConfig({
84+
bundle: true,
8085
format: 'esm',
8186
autoExternal: true,
8287
pkgJson: {
@@ -110,6 +115,7 @@ describe('should composeAutoExternalConfig correctly', () => {
110115

111116
it('autoExternal is true when format is umd or mf', () => {
112117
const umdResult = composeAutoExternalConfig({
118+
bundle: true,
113119
format: 'umd',
114120
autoExternal: true,
115121
pkgJson: {
@@ -132,6 +138,7 @@ describe('should composeAutoExternalConfig correctly', () => {
132138
`);
133139

134140
const mfResult = composeAutoExternalConfig({
141+
bundle: true,
135142
format: 'mf',
136143
autoExternal: true,
137144
pkgJson: {
@@ -156,6 +163,7 @@ describe('should composeAutoExternalConfig correctly', () => {
156163

157164
it('autoExternal will deduplication ', () => {
158165
const result = composeAutoExternalConfig({
166+
bundle: true,
159167
format: 'esm',
160168
autoExternal: true,
161169
pkgJson: {
@@ -191,6 +199,7 @@ describe('should composeAutoExternalConfig correctly', () => {
191199

192200
it('autoExternal is object', () => {
193201
const result = composeAutoExternalConfig({
202+
bundle: true,
194203
format: 'esm',
195204
autoExternal: {
196205
peerDependencies: false,
@@ -219,6 +228,7 @@ describe('should composeAutoExternalConfig correctly', () => {
219228

220229
it('autoExternal is false', () => {
221230
const result = composeAutoExternalConfig({
231+
bundle: true,
222232
format: 'esm',
223233
autoExternal: false,
224234
pkgJson: {
@@ -234,6 +244,7 @@ describe('should composeAutoExternalConfig correctly', () => {
234244

235245
it('autoExternal with user externals object', () => {
236246
const result = composeAutoExternalConfig({
247+
bundle: true,
237248
format: 'esm',
238249
autoExternal: true,
239250
pkgJson: {
@@ -257,10 +268,21 @@ describe('should composeAutoExternalConfig correctly', () => {
257268

258269
it('read package.json failed', () => {
259270
const result = composeAutoExternalConfig({
271+
bundle: true,
260272
format: 'esm',
261273
autoExternal: true,
262274
});
263275

264276
expect(result).toEqual({});
265277
});
278+
279+
it('bundleless', () => {
280+
const result = composeAutoExternalConfig({
281+
bundle: false,
282+
format: 'esm',
283+
autoExternal: true,
284+
});
285+
286+
expect(result).toStrictEqual({});
287+
});
266288
});

packages/core/tests/syntax.test.ts

+31-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import { describe, expect, test } from 'vitest';
2+
import type { EcmaScriptVersion } from '../src/types';
23
import {
34
ESX_TO_BROWSERSLIST,
45
transformSyntaxToBrowserslist,
56
transformSyntaxToRspackTarget,
67
} from '../src/utils/syntax';
78

89
const compareSemver = (a: string, b: string) => {
9-
const [aMajor, aMinor, aPatch] = a.split('.').map(Number);
10-
const [bMajor, bMinor, bPatch] = b.split('.').map(Number);
10+
const [aMajor, aMinor, aPatch] = a.split('.').map(Number) as [
11+
number,
12+
number,
13+
number,
14+
];
15+
const [bMajor, bMinor, bPatch] = b.split('.').map(Number) as [
16+
number,
17+
number,
18+
number,
19+
];
1120

1221
if (aMajor !== bMajor) {
1322
return aMajor - bMajor;
@@ -27,7 +36,7 @@ describe('ESX_TO_BROWSERSLIST', () => {
2736
});
2837

2938
test('ECMA version mapped browserslist queries should increments', () => {
30-
const sortedVersions = [
39+
const sortedVersions: EcmaScriptVersion[] = [
3140
'es5',
3241
'es6',
3342
'es2015',
@@ -39,16 +48,18 @@ describe('ESX_TO_BROWSERSLIST', () => {
3948
'es2021',
4049
'es2022',
4150
'es2023',
42-
'es2024',
43-
'esnext',
4451
];
4552

4653
for (let i = 1; i < sortedVersions.length; i++) {
47-
const prev = sortedVersions[i - 1];
48-
const current = sortedVersions[i];
54+
const prev = sortedVersions[i - 1]!;
55+
const current = sortedVersions[i]!;
4956
for (const query of Object.keys(ESX_TO_BROWSERSLIST[current])) {
50-
const prevQuery = ESX_TO_BROWSERSLIST[prev][query];
51-
const currQuery = ESX_TO_BROWSERSLIST[current][query];
57+
const prevQuery = (ESX_TO_BROWSERSLIST[prev] as Record<string, string>)[
58+
query
59+
];
60+
const currQuery = (
61+
ESX_TO_BROWSERSLIST[current] as Record<string, string>
62+
)[query];
5263
if (prevQuery && currQuery) {
5364
expect(compareSemver(currQuery, prevQuery)).toBeGreaterThanOrEqual(0);
5465
}
@@ -59,7 +70,9 @@ describe('ESX_TO_BROWSERSLIST', () => {
5970

6071
describe('transformSyntaxToBrowserslist', () => {
6172
test('esX', () => {
62-
expect(transformSyntaxToBrowserslist('es2015')).toMatchInlineSnapshot(`
73+
expect(
74+
transformSyntaxToBrowserslist('es2015', 'web'),
75+
).toMatchInlineSnapshot(`
6376
[
6477
"chrome >= 63.0.0",
6578
"edge >= 79.0.0",
@@ -71,7 +84,9 @@ describe('transformSyntaxToBrowserslist', () => {
7184
]
7285
`);
7386

74-
expect(transformSyntaxToBrowserslist('es2018')).toMatchInlineSnapshot(`
87+
expect(
88+
transformSyntaxToBrowserslist('es2018', 'web'),
89+
).toMatchInlineSnapshot(`
7590
[
7691
"chrome >= 64.0.0",
7792
"edge >= 79.0.0",
@@ -111,15 +126,15 @@ describe('transformSyntaxToBrowserslist', () => {
111126

112127
test('browserslist', () => {
113128
expect(
114-
transformSyntaxToBrowserslist(['fully supports es6-module']),
129+
transformSyntaxToBrowserslist(['fully supports es6-module'], 'web'),
115130
).toMatchInlineSnapshot(`
116131
[
117132
"fully supports es6-module",
118133
]
119134
`);
120135

121136
expect(
122-
transformSyntaxToBrowserslist(['node 14', 'Chrome 103']),
137+
transformSyntaxToBrowserslist(['node 14', 'Chrome 103'], 'web'),
123138
).toMatchInlineSnapshot(`
124139
[
125140
"node 14",
@@ -130,7 +145,7 @@ describe('transformSyntaxToBrowserslist', () => {
130145

131146
test('combined', () => {
132147
expect(
133-
transformSyntaxToBrowserslist(['Chrome 123', 'es5']),
148+
transformSyntaxToBrowserslist(['Chrome 123', 'es5'], 'web'),
134149
).toMatchInlineSnapshot(`
135150
[
136151
"Chrome 123",
@@ -145,8 +160,8 @@ describe('transformSyntaxToBrowserslist', () => {
145160
]
146161
`);
147162

148-
expect(transformSyntaxToBrowserslist(['es5'])).toEqual(
149-
transformSyntaxToBrowserslist('es5'),
163+
expect(transformSyntaxToBrowserslist(['es5'], 'web')).toEqual(
164+
transformSyntaxToBrowserslist('es5', 'web'),
150165
);
151166
});
152167
});

packages/core/tests/tsconfig.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "@rslib/tsconfig/base",
3+
"include": ["."],
4+
"exclude": ["**/node_modules"],
5+
"references": []
6+
}

tests/integration/async-chunks/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ test('should get correct value from async chunks', async () => {
66
const fixturePath = join(__dirname, 'default');
77
const { entryFiles } = await buildAndGetResults({ fixturePath });
88

9-
for (const format of ['esm', 'cjs']) {
9+
for (const format of ['esm', 'cjs'] as const) {
1010
const { foo } = await import(entryFiles[format]);
1111
expect(await foo()).toBe('dynamic');
1212
}

tests/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"type": "module",
55
"scripts": {
66
"test:benchmark": "vitest bench",
7-
"test:e2e": "playwright test --pass-with-no-tests"
7+
"test:e2e": "playwright test --pass-with-no-tests",
8+
"type-check": "tsc --noEmit"
89
},
910
"dependencies": {
1011
"react": "^19.0.0",

0 commit comments

Comments
 (0)