diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4d21950e4..b04aa3b0b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/package.json b/package.json index 8f8bfc3bc..26b18e7e1 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/packages/core/package.json b/packages/core/package.json index ad09f0c38..a069f3423 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -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", diff --git a/packages/core/src/utils/syntax.ts b/packages/core/src/utils/syntax.ts index b07ab1236..d4cf76fab 100644 --- a/packages/core/src/utils/syntax.ts +++ b/packages/core/src/utils/syntax.ts @@ -48,7 +48,7 @@ const RSPACK_TARGET_UNLISTED_MODERN_ECMA_VERSIONS: EcmaScriptVersion[] = [ */ export const ESX_TO_BROWSERSLIST: Record< FixedEcmaVersions, - Record + Record > & Record string[]> = { diff --git a/packages/core/tests/config.test.ts b/packages/core/tests/config.test.ts index 27e851583..c8a6adf8a 100644 --- a/packages/core/tests/config.test.ts +++ b/packages/core/tests/config.test.ts @@ -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", @@ -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", @@ -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", @@ -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", @@ -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, @@ -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, diff --git a/packages/core/tests/extension.test.ts b/packages/core/tests/extension.test.ts index dcad0ce77..363789db2 100644 --- a/packages/core/tests/extension.test.ts +++ b/packages/core/tests/extension.test.ts @@ -14,7 +14,9 @@ describe('should get extension correctly', () => { it('autoExtension is false', () => { const options: Options = { format: 'cjs', - pkgJson: {}, + pkgJson: { + name: 'foo', + }, autoExtension: false, }; @@ -44,6 +46,7 @@ describe('should get extension correctly', () => { const options: Options = { format: 'cjs', pkgJson: { + name: 'foo', type: 'module', }, autoExtension: true, @@ -62,6 +65,7 @@ describe('should get extension correctly', () => { const options: Options = { format: 'cjs', pkgJson: { + name: 'foo', type: 'commonjs', }, autoExtension: true, @@ -80,6 +84,7 @@ describe('should get extension correctly', () => { const options: Options = { format: 'esm', pkgJson: { + name: 'foo', type: 'commonjs', }, autoExtension: true, @@ -98,6 +103,7 @@ describe('should get extension correctly', () => { const options: Options = { format: 'esm', pkgJson: { + name: 'foo', type: 'module', }, autoExtension: true, diff --git a/packages/core/tests/external.test.ts b/packages/core/tests/external.test.ts index 4dada5d99..7398cd5cc 100644 --- a/packages/core/tests/external.test.ts +++ b/packages/core/tests/external.test.ts @@ -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: { @@ -17,6 +18,7 @@ describe('should composeAutoExternalConfig correctly', () => { }); const cjsResult = composeAutoExternalConfig({ + bundle: true, format: 'cjs', autoExternal: undefined, pkgJson: { @@ -28,6 +30,7 @@ describe('should composeAutoExternalConfig correctly', () => { }); const umdResult = composeAutoExternalConfig({ + bundle: true, format: 'umd', autoExternal: undefined, pkgJson: { @@ -39,6 +42,7 @@ describe('should composeAutoExternalConfig correctly', () => { }); const mfResult = composeAutoExternalConfig({ + bundle: true, format: 'mf', autoExternal: undefined, pkgJson: { @@ -77,6 +81,7 @@ describe('should composeAutoExternalConfig correctly', () => { it('autoExternal is true', () => { const result = composeAutoExternalConfig({ + bundle: true, format: 'esm', autoExternal: true, pkgJson: { @@ -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: { @@ -132,6 +138,7 @@ describe('should composeAutoExternalConfig correctly', () => { `); const mfResult = composeAutoExternalConfig({ + bundle: true, format: 'mf', autoExternal: true, pkgJson: { @@ -156,6 +163,7 @@ describe('should composeAutoExternalConfig correctly', () => { it('autoExternal will deduplication ', () => { const result = composeAutoExternalConfig({ + bundle: true, format: 'esm', autoExternal: true, pkgJson: { @@ -191,6 +199,7 @@ describe('should composeAutoExternalConfig correctly', () => { it('autoExternal is object', () => { const result = composeAutoExternalConfig({ + bundle: true, format: 'esm', autoExternal: { peerDependencies: false, @@ -219,6 +228,7 @@ describe('should composeAutoExternalConfig correctly', () => { it('autoExternal is false', () => { const result = composeAutoExternalConfig({ + bundle: true, format: 'esm', autoExternal: false, pkgJson: { @@ -234,6 +244,7 @@ describe('should composeAutoExternalConfig correctly', () => { it('autoExternal with user externals object', () => { const result = composeAutoExternalConfig({ + bundle: true, format: 'esm', autoExternal: true, pkgJson: { @@ -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({}); + }); }); diff --git a/packages/core/tests/syntax.test.ts b/packages/core/tests/syntax.test.ts index 58269e122..99f38d297 100644 --- a/packages/core/tests/syntax.test.ts +++ b/packages/core/tests/syntax.test.ts @@ -1,4 +1,5 @@ import { describe, expect, test } from 'vitest'; +import type { EcmaScriptVersion } from '../src/types'; import { ESX_TO_BROWSERSLIST, transformSyntaxToBrowserslist, @@ -6,8 +7,16 @@ import { } 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; @@ -27,7 +36,7 @@ describe('ESX_TO_BROWSERSLIST', () => { }); test('ECMA version mapped browserslist queries should increments', () => { - const sortedVersions = [ + const sortedVersions: EcmaScriptVersion[] = [ 'es5', 'es6', 'es2015', @@ -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)[ + query + ]; + const currQuery = ( + ESX_TO_BROWSERSLIST[current] as Record + )[query]; if (prevQuery && currQuery) { expect(compareSemver(currQuery, prevQuery)).toBeGreaterThanOrEqual(0); } @@ -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", @@ -71,7 +84,9 @@ describe('transformSyntaxToBrowserslist', () => { ] `); - expect(transformSyntaxToBrowserslist('es2018')).toMatchInlineSnapshot(` + expect( + transformSyntaxToBrowserslist('es2018', 'web'), + ).toMatchInlineSnapshot(` [ "chrome >= 64.0.0", "edge >= 79.0.0", @@ -111,7 +126,7 @@ describe('transformSyntaxToBrowserslist', () => { test('browserslist', () => { expect( - transformSyntaxToBrowserslist(['fully supports es6-module']), + transformSyntaxToBrowserslist(['fully supports es6-module'], 'web'), ).toMatchInlineSnapshot(` [ "fully supports es6-module", @@ -119,7 +134,7 @@ describe('transformSyntaxToBrowserslist', () => { `); expect( - transformSyntaxToBrowserslist(['node 14', 'Chrome 103']), + transformSyntaxToBrowserslist(['node 14', 'Chrome 103'], 'web'), ).toMatchInlineSnapshot(` [ "node 14", @@ -130,7 +145,7 @@ describe('transformSyntaxToBrowserslist', () => { test('combined', () => { expect( - transformSyntaxToBrowserslist(['Chrome 123', 'es5']), + transformSyntaxToBrowserslist(['Chrome 123', 'es5'], 'web'), ).toMatchInlineSnapshot(` [ "Chrome 123", @@ -145,8 +160,8 @@ describe('transformSyntaxToBrowserslist', () => { ] `); - expect(transformSyntaxToBrowserslist(['es5'])).toEqual( - transformSyntaxToBrowserslist('es5'), + expect(transformSyntaxToBrowserslist(['es5'], 'web')).toEqual( + transformSyntaxToBrowserslist('es5', 'web'), ); }); }); diff --git a/packages/core/tests/tsconfig.json b/packages/core/tests/tsconfig.json new file mode 100644 index 000000000..947f4992f --- /dev/null +++ b/packages/core/tests/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "@rslib/tsconfig/base", + "include": ["."], + "exclude": ["**/node_modules"], + "references": [] +} diff --git a/tests/integration/async-chunks/index.test.ts b/tests/integration/async-chunks/index.test.ts index d1e1e4fd3..574180377 100644 --- a/tests/integration/async-chunks/index.test.ts +++ b/tests/integration/async-chunks/index.test.ts @@ -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'); } diff --git a/tests/package.json b/tests/package.json index 1f6bc2fe5..a3417aba3 100644 --- a/tests/package.json +++ b/tests/package.json @@ -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", diff --git a/tests/tsconfig.json b/tests/tsconfig.json index b4aba8d72..6e2a2e939 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -2,15 +2,17 @@ "extends": "@rslib/tsconfig/base", "compilerOptions": { "noEmit": true, - "composite": true + "composite": true, + "allowJs": true }, "include": [ "e2e/**/*.ts", - "integration/**/*.ts", + "integration/**/**.test.ts", + "integration/**/rslib.config.ts", + "integration/**/rslib.config.js", "benchmark/**/*.ts", "playwright.config.ts", - "scripts", - "integration/shims/cjs/src/ok.cjs" + "scripts" ], "exclude": ["**/node_modules", "**/.*/"], "references": [