Skip to content

chore: add type check for test files #681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 14, 2025
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
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ jobs:
- name: Install Dependencies
run: pnpm install

- name: Type Check
run: pnpm run type-check

- name: Unit Test
run: pnpm run test:unit

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"test:unit": "vitest run --project unit*",
"test:unit:watch": "vitest --project unit*",
"testu": "pnpm run test:unit -u && pnpm run test:integration -u",
"type-check": "pnpm -r run type-check",
"update:rsbuild": "npx taze minor --include /rsbuild/ -w -r -l",
"watch": "pnpm build --watch"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"scripts": {
"build": "rslib build",
"dev": "rslib build --watch",
"prebundle": "prebundle"
"prebundle": "prebundle",
"type-check": "tsc --noEmit && tsc --noEmit -p tests"
},
"dependencies": {
"@rsbuild/core": "1.2.0-beta.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const RSPACK_TARGET_UNLISTED_MODERN_ECMA_VERSIONS: EcmaScriptVersion[] = [
*/
export const ESX_TO_BROWSERSLIST: Record<
FixedEcmaVersions,
Record<string, string | string[]>
Record<string, string>
> &
Record<LatestEcmaVersions, (target: RsbuildConfigOutputTarget) => string[]> =
{
Expand Down
16 changes: 8 additions & 8 deletions packages/core/tests/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe('syntax', () => {
const composedRsbuildConfig = await composeCreateRsbuildConfig(rslibConfig);

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

expect(
composedRsbuildConfig[0].config.output?.overrideBrowserslist,
composedRsbuildConfig[0]!.config.output?.overrideBrowserslist,
).toMatchInlineSnapshot(`
[
"last 1 Chrome versions",
Expand Down Expand Up @@ -273,7 +273,7 @@ describe('syntax', () => {
const composedRsbuildConfig = await composeCreateRsbuildConfig(rslibConfig);

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

expect(
composedRsbuildConfig[0].config.output?.overrideBrowserslist,
composedRsbuildConfig[0]!.config.output?.overrideBrowserslist,
).toMatchInlineSnapshot(`
[
"chrome >= 63.0.0",
Expand Down Expand Up @@ -322,7 +322,7 @@ describe('minify', () => {
const composedRsbuildConfig = await composeCreateRsbuildConfig(rslibConfig);

expect(
composedRsbuildConfig[0].config.output?.minify,
composedRsbuildConfig[0]!.config.output?.minify,
).toMatchInlineSnapshot(`
{
"css": false,
Expand Down Expand Up @@ -380,15 +380,15 @@ describe('minify', () => {
const composedRsbuildConfig = await composeCreateRsbuildConfig(rslibConfig);

expect(
composedRsbuildConfig[0].config.output?.minify,
composedRsbuildConfig[0]!.config.output?.minify,
).toMatchInlineSnapshot('false');

expect(
composedRsbuildConfig[1].config.output?.minify,
composedRsbuildConfig[1]!.config.output?.minify,
).toMatchInlineSnapshot('true');

expect(
composedRsbuildConfig[2].config.output?.minify,
composedRsbuildConfig[2]!.config.output?.minify,
).toMatchInlineSnapshot(`
{
"css": true,
Expand Down
8 changes: 7 additions & 1 deletion packages/core/tests/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ describe('should get extension correctly', () => {
it('autoExtension is false', () => {
const options: Options = {
format: 'cjs',
pkgJson: {},
pkgJson: {
name: 'foo',
},
autoExtension: false,
};

Expand Down Expand Up @@ -44,6 +46,7 @@ describe('should get extension correctly', () => {
const options: Options = {
format: 'cjs',
pkgJson: {
name: 'foo',
type: 'module',
},
autoExtension: true,
Expand All @@ -62,6 +65,7 @@ describe('should get extension correctly', () => {
const options: Options = {
format: 'cjs',
pkgJson: {
name: 'foo',
type: 'commonjs',
},
autoExtension: true,
Expand All @@ -80,6 +84,7 @@ describe('should get extension correctly', () => {
const options: Options = {
format: 'esm',
pkgJson: {
name: 'foo',
type: 'commonjs',
},
autoExtension: true,
Expand All @@ -98,6 +103,7 @@ describe('should get extension correctly', () => {
const options: Options = {
format: 'esm',
pkgJson: {
name: 'foo',
type: 'module',
},
autoExtension: true,
Expand Down
22 changes: 22 additions & 0 deletions packages/core/tests/external.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ vi.mock('rslog');
describe('should composeAutoExternalConfig correctly', () => {
it('autoExternal default value', () => {
const esmResult = composeAutoExternalConfig({
bundle: true,
format: 'esm',
autoExternal: undefined,
pkgJson: {
Expand All @@ -17,6 +18,7 @@ describe('should composeAutoExternalConfig correctly', () => {
});

const cjsResult = composeAutoExternalConfig({
bundle: true,
format: 'cjs',
autoExternal: undefined,
pkgJson: {
Expand All @@ -28,6 +30,7 @@ describe('should composeAutoExternalConfig correctly', () => {
});

const umdResult = composeAutoExternalConfig({
bundle: true,
format: 'umd',
autoExternal: undefined,
pkgJson: {
Expand All @@ -39,6 +42,7 @@ describe('should composeAutoExternalConfig correctly', () => {
});

const mfResult = composeAutoExternalConfig({
bundle: true,
format: 'mf',
autoExternal: undefined,
pkgJson: {
Expand Down Expand Up @@ -77,6 +81,7 @@ describe('should composeAutoExternalConfig correctly', () => {

it('autoExternal is true', () => {
const result = composeAutoExternalConfig({
bundle: true,
format: 'esm',
autoExternal: true,
pkgJson: {
Expand Down Expand Up @@ -110,6 +115,7 @@ describe('should composeAutoExternalConfig correctly', () => {

it('autoExternal is true when format is umd or mf', () => {
const umdResult = composeAutoExternalConfig({
bundle: true,
format: 'umd',
autoExternal: true,
pkgJson: {
Expand All @@ -132,6 +138,7 @@ describe('should composeAutoExternalConfig correctly', () => {
`);

const mfResult = composeAutoExternalConfig({
bundle: true,
format: 'mf',
autoExternal: true,
pkgJson: {
Expand All @@ -156,6 +163,7 @@ describe('should composeAutoExternalConfig correctly', () => {

it('autoExternal will deduplication ', () => {
const result = composeAutoExternalConfig({
bundle: true,
format: 'esm',
autoExternal: true,
pkgJson: {
Expand Down Expand Up @@ -191,6 +199,7 @@ describe('should composeAutoExternalConfig correctly', () => {

it('autoExternal is object', () => {
const result = composeAutoExternalConfig({
bundle: true,
format: 'esm',
autoExternal: {
peerDependencies: false,
Expand Down Expand Up @@ -219,6 +228,7 @@ describe('should composeAutoExternalConfig correctly', () => {

it('autoExternal is false', () => {
const result = composeAutoExternalConfig({
bundle: true,
format: 'esm',
autoExternal: false,
pkgJson: {
Expand All @@ -234,6 +244,7 @@ describe('should composeAutoExternalConfig correctly', () => {

it('autoExternal with user externals object', () => {
const result = composeAutoExternalConfig({
bundle: true,
format: 'esm',
autoExternal: true,
pkgJson: {
Expand All @@ -257,10 +268,21 @@ describe('should composeAutoExternalConfig correctly', () => {

it('read package.json failed', () => {
const result = composeAutoExternalConfig({
bundle: true,
format: 'esm',
autoExternal: true,
});

expect(result).toEqual({});
});

it('bundleless', () => {
const result = composeAutoExternalConfig({
bundle: false,
format: 'esm',
autoExternal: true,
});

expect(result).toStrictEqual({});
});
});
47 changes: 31 additions & 16 deletions packages/core/tests/syntax.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { describe, expect, test } from 'vitest';
import type { EcmaScriptVersion } from '../src/types';
import {
ESX_TO_BROWSERSLIST,
transformSyntaxToBrowserslist,
transformSyntaxToRspackTarget,
} from '../src/utils/syntax';

const compareSemver = (a: string, b: string) => {
const [aMajor, aMinor, aPatch] = a.split('.').map(Number);
const [bMajor, bMinor, bPatch] = b.split('.').map(Number);
const [aMajor, aMinor, aPatch] = a.split('.').map(Number) as [
number,
number,
number,
];
const [bMajor, bMinor, bPatch] = b.split('.').map(Number) as [
number,
number,
number,
];

if (aMajor !== bMajor) {
return aMajor - bMajor;
Expand All @@ -27,7 +36,7 @@ describe('ESX_TO_BROWSERSLIST', () => {
});

test('ECMA version mapped browserslist queries should increments', () => {
const sortedVersions = [
const sortedVersions: EcmaScriptVersion[] = [
'es5',
'es6',
'es2015',
Expand All @@ -39,16 +48,18 @@ describe('ESX_TO_BROWSERSLIST', () => {
'es2021',
'es2022',
'es2023',
'es2024',
'esnext',
];

for (let i = 1; i < sortedVersions.length; i++) {
const prev = sortedVersions[i - 1];
const current = sortedVersions[i];
const prev = sortedVersions[i - 1]!;
const current = sortedVersions[i]!;
for (const query of Object.keys(ESX_TO_BROWSERSLIST[current])) {
const prevQuery = ESX_TO_BROWSERSLIST[prev][query];
const currQuery = ESX_TO_BROWSERSLIST[current][query];
const prevQuery = (ESX_TO_BROWSERSLIST[prev] as Record<string, string>)[
query
];
const currQuery = (
ESX_TO_BROWSERSLIST[current] as Record<string, string>
)[query];
if (prevQuery && currQuery) {
expect(compareSemver(currQuery, prevQuery)).toBeGreaterThanOrEqual(0);
}
Expand All @@ -59,7 +70,9 @@ describe('ESX_TO_BROWSERSLIST', () => {

describe('transformSyntaxToBrowserslist', () => {
test('esX', () => {
expect(transformSyntaxToBrowserslist('es2015')).toMatchInlineSnapshot(`
expect(
transformSyntaxToBrowserslist('es2015', 'web'),
).toMatchInlineSnapshot(`
[
"chrome >= 63.0.0",
"edge >= 79.0.0",
Expand All @@ -71,7 +84,9 @@ describe('transformSyntaxToBrowserslist', () => {
]
`);

expect(transformSyntaxToBrowserslist('es2018')).toMatchInlineSnapshot(`
expect(
transformSyntaxToBrowserslist('es2018', 'web'),
).toMatchInlineSnapshot(`
[
"chrome >= 64.0.0",
"edge >= 79.0.0",
Expand Down Expand Up @@ -111,15 +126,15 @@ describe('transformSyntaxToBrowserslist', () => {

test('browserslist', () => {
expect(
transformSyntaxToBrowserslist(['fully supports es6-module']),
transformSyntaxToBrowserslist(['fully supports es6-module'], 'web'),
).toMatchInlineSnapshot(`
[
"fully supports es6-module",
]
`);

expect(
transformSyntaxToBrowserslist(['node 14', 'Chrome 103']),
transformSyntaxToBrowserslist(['node 14', 'Chrome 103'], 'web'),
).toMatchInlineSnapshot(`
[
"node 14",
Expand All @@ -130,7 +145,7 @@ describe('transformSyntaxToBrowserslist', () => {

test('combined', () => {
expect(
transformSyntaxToBrowserslist(['Chrome 123', 'es5']),
transformSyntaxToBrowserslist(['Chrome 123', 'es5'], 'web'),
).toMatchInlineSnapshot(`
[
"Chrome 123",
Expand All @@ -145,8 +160,8 @@ describe('transformSyntaxToBrowserslist', () => {
]
`);

expect(transformSyntaxToBrowserslist(['es5'])).toEqual(
transformSyntaxToBrowserslist('es5'),
expect(transformSyntaxToBrowserslist(['es5'], 'web')).toEqual(
transformSyntaxToBrowserslist('es5', 'web'),
);
});
});
Expand Down
6 changes: 6 additions & 0 deletions packages/core/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "@rslib/tsconfig/base",
"include": ["."],
"exclude": ["**/node_modules"],
"references": []
}
2 changes: 1 addition & 1 deletion tests/integration/async-chunks/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test('should get correct value from async chunks', async () => {
const fixturePath = join(__dirname, 'default');
const { entryFiles } = await buildAndGetResults({ fixturePath });

for (const format of ['esm', 'cjs']) {
for (const format of ['esm', 'cjs'] as const) {
const { foo } = await import(entryFiles[format]);
expect(await foo()).toBe('dynamic');
}
Expand Down
3 changes: 2 additions & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"type": "module",
"scripts": {
"test:benchmark": "vitest bench",
"test:e2e": "playwright test --pass-with-no-tests"
"test:e2e": "playwright test --pass-with-no-tests",
"type-check": "tsc --noEmit"
},
"dependencies": {
"react": "^19.0.0",
Expand Down
Loading
Loading