From 5d09f4a98f4f381934575426d19b8b38079d26b4 Mon Sep 17 00:00:00 2001 From: 10Derozan Date: Mon, 4 Dec 2023 14:04:50 +0800 Subject: [PATCH] test(module-tools): adjust dts test and add case for declarationMap (#5033) --- pnpm-lock.yaml | 6 +- .../dts/{distPath-bundle.ts => dts-bundle.ts} | 4 +- ...stPath-bundleless.ts => dts-bundleless.ts} | 3 +- .../module/fixtures/build/dts/dts.test.ts | 131 ++++-------------- .../module/fixtures/build/dts/only-bundle.ts | 11 -- .../fixtures/build/dts/only-bundleless.ts | 11 -- .../module/fixtures/build/dts/package.json | 5 +- .../module/fixtures/build/dts/src/index.ts | 1 + .../module/fixtures/build/dts/src1/b.ts | 2 - .../module/fixtures/build/dts/src1/index.ts | 5 - .../module/fixtures/build/dts/src2/b.ts | 2 - .../module/fixtures/build/dts/src2/index.ts | 5 - .../fixtures/build/dts/tsconfig-bundle.json | 10 -- .../build/dts/tsconfig-bundleless.json | 10 -- .../module/fixtures/build/dts/tsconfig.json | 4 +- .../fixtures/build/dts/tsconfigPath-bundle.ts | 15 -- .../build/dts/tsconfigPath-bundleless.ts | 13 -- 17 files changed, 45 insertions(+), 193 deletions(-) rename tests/integration/module/fixtures/build/dts/{distPath-bundle.ts => dts-bundle.ts} (74%) rename tests/integration/module/fixtures/build/dts/{distPath-bundleless.ts => dts-bundleless.ts} (79%) delete mode 100644 tests/integration/module/fixtures/build/dts/only-bundle.ts delete mode 100644 tests/integration/module/fixtures/build/dts/only-bundleless.ts delete mode 100644 tests/integration/module/fixtures/build/dts/src1/b.ts delete mode 100644 tests/integration/module/fixtures/build/dts/src1/index.ts delete mode 100644 tests/integration/module/fixtures/build/dts/src2/b.ts delete mode 100644 tests/integration/module/fixtures/build/dts/src2/index.ts delete mode 100644 tests/integration/module/fixtures/build/dts/tsconfig-bundle.json delete mode 100644 tests/integration/module/fixtures/build/dts/tsconfig-bundleless.json delete mode 100644 tests/integration/module/fixtures/build/dts/tsconfigPath-bundle.ts delete mode 100644 tests/integration/module/fixtures/build/dts/tsconfigPath-bundleless.ts diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c131223bb67c..bb3fa0db8d03 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -7132,7 +7132,11 @@ importers: tests/integration/module/fixtures/build/define/ts: {} - tests/integration/module/fixtures/build/dts: {} + tests/integration/module/fixtures/build/dts: + devDependencies: + react: + specifier: ^18.2.0 + version: 18.2.0 tests/integration/module/fixtures/build/esbuildOptions: {} diff --git a/tests/integration/module/fixtures/build/dts/distPath-bundle.ts b/tests/integration/module/fixtures/build/dts/dts-bundle.ts similarity index 74% rename from tests/integration/module/fixtures/build/dts/distPath-bundle.ts rename to tests/integration/module/fixtures/build/dts/dts-bundle.ts index 9e8add5bdb59..2b28492dd487 100644 --- a/tests/integration/module/fixtures/build/dts/distPath-bundle.ts +++ b/tests/integration/module/fixtures/build/dts/dts-bundle.ts @@ -6,7 +6,9 @@ export default defineConfig({ buildType: 'bundle', dts: { distPath: './types', + only: true, + respectExternal: false, }, - outDir: './dist/bundle-dist-path', + outDir: './dist/bundle', }, }); diff --git a/tests/integration/module/fixtures/build/dts/distPath-bundleless.ts b/tests/integration/module/fixtures/build/dts/dts-bundleless.ts similarity index 79% rename from tests/integration/module/fixtures/build/dts/distPath-bundleless.ts rename to tests/integration/module/fixtures/build/dts/dts-bundleless.ts index b6fe29135fd2..59c44174efb1 100644 --- a/tests/integration/module/fixtures/build/dts/distPath-bundleless.ts +++ b/tests/integration/module/fixtures/build/dts/dts-bundleless.ts @@ -5,7 +5,8 @@ export default defineConfig({ buildType: 'bundleless', dts: { distPath: './types', + only: true, }, - outDir: './dist/bundleless-dist-path', + outDir: './dist/bundleless', }, }); diff --git a/tests/integration/module/fixtures/build/dts/dts.test.ts b/tests/integration/module/fixtures/build/dts/dts.test.ts index f0c262e77bdd..92a1ed0924f1 100644 --- a/tests/integration/module/fixtures/build/dts/dts.test.ts +++ b/tests/integration/module/fixtures/build/dts/dts.test.ts @@ -16,142 +16,68 @@ afterAll(() => { const fixtureDir = __dirname; const enableDts = true; -describe('dts is false', () => { +describe('dts build', () => { it('buildType is bundle', async () => { - const configFile = path.join(fixtureDir, './false-bundle.ts'); - const { success } = await runCli({ + const configFile = path.join(fixtureDir, './dts-bundle.ts'); + await runCli({ argv: ['build'], configFile, appDirectory: fixtureDir, enableDts, }); - expect(success).toBeTruthy(); + // dts.distPath const distDtsFilePath = path.join( fixtureDir, - './dist/false-bundle/index.d.ts', + './dist/bundle/types/index.d.ts', ); - expect(await fs.pathExists(distDtsFilePath)).toBeFalsy(); + const distJsFilePath = path.join(fixtureDir, './dist/bundle/index.js'); + // dts.only + expect(fs.existsSync(distJsFilePath)).toBeFalsy(); + const content = await fs.readFile(distDtsFilePath, 'utf8'); + // dts.respectExternal + expect(content.includes('export { Ref } from')).toBeTruthy(); }); it('buildType is bundleless', async () => { - const configFile = path.join(fixtureDir, './false-bundleless.ts'); - const { success } = await runCli({ + const configFile = path.join(fixtureDir, './dts-bundleless.ts'); + await runCli({ argv: ['build'], configFile, appDirectory: fixtureDir, enableDts, }); - expect(success).toBeTruthy(); - const distDtsFilePath = path.join( + const distMapPath = path.join( fixtureDir, - './dist/false-bundleless/index.d.ts', + './dist/bundleless/types/index.d.ts.map', ); - expect(await fs.pathExists(distDtsFilePath)).toBeFalsy(); + const content = JSON.parse(await fs.readFile(distMapPath, 'utf8')); + // source map + expect( + path.resolve(path.dirname(distMapPath), content.sources[0]) === + path.resolve(fixtureDir, 'src/index.ts'), + ).toBeTruthy(); }); }); -describe('dts.distPath', () => { - it('buildType is bundle', async () => { - const configFile = path.join(fixtureDir, './distPath-bundle.ts'); - const { success } = await runCli({ - argv: ['build'], - configFile, - appDirectory: fixtureDir, - enableDts, - }); - expect(success).toBeTruthy(); - const distDtsFilePath = path.join( - fixtureDir, - './dist/bundle-dist-path/types/index.d.ts', - ); - expect(await fs.pathExists(distDtsFilePath)).toBeTruthy(); - }); - - it('buildType is bundleless', async () => { - const configFile = path.join(fixtureDir, './distPath-bundleless.ts'); - const { success } = await runCli({ - argv: ['build'], - configFile, - appDirectory: fixtureDir, - enableDts, - }); - expect(success).toBeTruthy(); - - let distDtsFilePath = path.join( - fixtureDir, - './dist/bundleless-dist-path/types/index.d.ts', - ); - expect(await fs.pathExists(distDtsFilePath)).toBeTruthy(); - distDtsFilePath = path.join( - fixtureDir, - './dist/bundleless-dist-path/types/b.d.ts', - ); - expect(await fs.pathExists(distDtsFilePath)).toBeTruthy(); - }); -}); - -describe('dts.tsconfigPath', () => { +describe('dts is false', () => { it('buildType is bundle', async () => { - const configFile = path.join(fixtureDir, './tsconfigPath-bundle.ts'); + const configFile = path.join(fixtureDir, './false-bundle.ts'); const { success } = await runCli({ argv: ['build'], configFile, appDirectory: fixtureDir, enableDts, }); - expect(success).toBeTruthy(); const distDtsFilePath = path.join( fixtureDir, - './dist/tsconfig-path/bundle/index.d.ts', - ); - expect(await fs.pathExists(distDtsFilePath)).toBeTruthy(); - }); - - it('buildType is bundleless', async () => { - const configFile = path.join(fixtureDir, './tsconfigPath-bundleless.ts'); - const { success } = await runCli({ - argv: ['build'], - configFile, - appDirectory: fixtureDir, - enableDts, - }); - expect(success).toBeTruthy(); - let distDtsFilePath = path.join( - fixtureDir, - './dist/tsconfig-path/bundleless/index.d.ts', - ); - expect(await fs.pathExists(distDtsFilePath)).toBeTruthy(); - - distDtsFilePath = path.join( - fixtureDir, - './dist/tsconfig-path/bundleless/b.d.ts', - ); - expect(await fs.pathExists(distDtsFilePath)).toBeTruthy(); - }); -}); - -describe('dts.only is true', () => { - it('buildType is bundle', async () => { - const configFile = path.join(fixtureDir, './only-bundle.ts'); - const { success } = await runCli({ - argv: ['build'], - configFile, - appDirectory: fixtureDir, - enableDts, - }); - expect(success).toBeTruthy(); - let distDtsFilePath = path.join( - fixtureDir, - './dist/only-bundle/index.d.ts', + './dist/false-bundle/index.d.ts', ); - expect(await fs.pathExists(distDtsFilePath)).toBeTruthy(); - distDtsFilePath = path.join(fixtureDir, './dist/only-bundle/index.js'); expect(await fs.pathExists(distDtsFilePath)).toBeFalsy(); }); it('buildType is bundleless', async () => { - const configFile = path.join(fixtureDir, './only-bundleless.ts'); + const configFile = path.join(fixtureDir, './false-bundleless.ts'); const { success } = await runCli({ argv: ['build'], configFile, @@ -159,13 +85,10 @@ describe('dts.only is true', () => { enableDts, }); expect(success).toBeTruthy(); - let distDtsFilePath = path.join( + const distDtsFilePath = path.join( fixtureDir, - './dist/only-bundleless/index.d.ts', + './dist/false-bundleless/index.d.ts', ); - expect(await fs.pathExists(distDtsFilePath)).toBeTruthy(); - - distDtsFilePath = path.join(fixtureDir, './dist/only-bundleless/index.js'); expect(await fs.pathExists(distDtsFilePath)).toBeFalsy(); }); }); diff --git a/tests/integration/module/fixtures/build/dts/only-bundle.ts b/tests/integration/module/fixtures/build/dts/only-bundle.ts deleted file mode 100644 index 06e4b5d31024..000000000000 --- a/tests/integration/module/fixtures/build/dts/only-bundle.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { defineConfig } from '@modern-js/module-tools/defineConfig'; - -export default defineConfig({ - buildConfig: { - buildType: 'bundle', - dts: { - only: true, - }, - outDir: './dist/only-bundle', - }, -}); diff --git a/tests/integration/module/fixtures/build/dts/only-bundleless.ts b/tests/integration/module/fixtures/build/dts/only-bundleless.ts deleted file mode 100644 index be1c17a0aa64..000000000000 --- a/tests/integration/module/fixtures/build/dts/only-bundleless.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { defineConfig } from '@modern-js/module-tools/defineConfig'; - -export default defineConfig({ - buildConfig: { - buildType: 'bundleless', - dts: { - only: true, - }, - outDir: './dist/only-bundleless', - }, -}); diff --git a/tests/integration/module/fixtures/build/dts/package.json b/tests/integration/module/fixtures/build/dts/package.json index 4d5b82640d6a..999a4ff4d008 100644 --- a/tests/integration/module/fixtures/build/dts/package.json +++ b/tests/integration/module/fixtures/build/dts/package.json @@ -1,4 +1,7 @@ { "name": "dts-test", - "version": "1.0.0" + "version": "1.0.0", + "devDependencies": { + "react": "^18.2.0" + } } diff --git a/tests/integration/module/fixtures/build/dts/src/index.ts b/tests/integration/module/fixtures/build/dts/src/index.ts index 7d6e6f9384e6..82354cc593c7 100644 --- a/tests/integration/module/fixtures/build/dts/src/index.ts +++ b/tests/integration/module/fixtures/build/dts/src/index.ts @@ -2,4 +2,5 @@ import { b } from '@src/b'; console.info(b); +export type { Ref } from 'react'; export type { A } from './b'; diff --git a/tests/integration/module/fixtures/build/dts/src1/b.ts b/tests/integration/module/fixtures/build/dts/src1/b.ts deleted file mode 100644 index 2670ea29ceae..000000000000 --- a/tests/integration/module/fixtures/build/dts/src1/b.ts +++ /dev/null @@ -1,2 +0,0 @@ -export const b = 'hello world'; -export type A = string; diff --git a/tests/integration/module/fixtures/build/dts/src1/index.ts b/tests/integration/module/fixtures/build/dts/src1/index.ts deleted file mode 100644 index 466333abd362..000000000000 --- a/tests/integration/module/fixtures/build/dts/src1/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { b } from '@src1/b'; - -console.info(b); - -export type { A } from '@src1/b'; diff --git a/tests/integration/module/fixtures/build/dts/src2/b.ts b/tests/integration/module/fixtures/build/dts/src2/b.ts deleted file mode 100644 index 2670ea29ceae..000000000000 --- a/tests/integration/module/fixtures/build/dts/src2/b.ts +++ /dev/null @@ -1,2 +0,0 @@ -export const b = 'hello world'; -export type A = string; diff --git a/tests/integration/module/fixtures/build/dts/src2/index.ts b/tests/integration/module/fixtures/build/dts/src2/index.ts deleted file mode 100644 index f17fada910ad..000000000000 --- a/tests/integration/module/fixtures/build/dts/src2/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { b } from '@src2/b'; - -console.info(b); - -export type { A } from '@src2/b'; diff --git a/tests/integration/module/fixtures/build/dts/tsconfig-bundle.json b/tests/integration/module/fixtures/build/dts/tsconfig-bundle.json deleted file mode 100644 index cc0f134db5d5..000000000000 --- a/tests/integration/module/fixtures/build/dts/tsconfig-bundle.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "extends": "@modern-js/tsconfig/base", - "compilerOptions": { - "baseUrl": "./", - "paths": { - "@src1/*": ["./src1/*"] - } - }, - "include": ["src1"] -} diff --git a/tests/integration/module/fixtures/build/dts/tsconfig-bundleless.json b/tests/integration/module/fixtures/build/dts/tsconfig-bundleless.json deleted file mode 100644 index 49c7df388438..000000000000 --- a/tests/integration/module/fixtures/build/dts/tsconfig-bundleless.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "extends": "@modern-js/tsconfig/base", - "compilerOptions": { - "baseUrl": "./", - "paths": { - "@src2/*": ["./src2/*"] - } - }, - "include": ["src2"] -} diff --git a/tests/integration/module/fixtures/build/dts/tsconfig.json b/tests/integration/module/fixtures/build/dts/tsconfig.json index a7949e4c0bf7..153a9c581c72 100644 --- a/tests/integration/module/fixtures/build/dts/tsconfig.json +++ b/tests/integration/module/fixtures/build/dts/tsconfig.json @@ -4,7 +4,9 @@ "baseUrl": "./", "paths": { "@src/*": ["./src/*"] - } + }, + "declaration": true, + "declarationMap": true }, "include": ["src"] } diff --git a/tests/integration/module/fixtures/build/dts/tsconfigPath-bundle.ts b/tests/integration/module/fixtures/build/dts/tsconfigPath-bundle.ts deleted file mode 100644 index b55a825e784c..000000000000 --- a/tests/integration/module/fixtures/build/dts/tsconfigPath-bundle.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { defineConfig } from '@modern-js/module-tools/defineConfig'; - -export default defineConfig({ - buildConfig: { - alias: { - '@src1': './src1', - }, - buildType: 'bundle', - input: ['./src1/index.ts'], - dts: { - tsconfigPath: './tsconfig-bundle.json', - }, - outDir: './dist/tsconfig-path/bundle', - }, -}); diff --git a/tests/integration/module/fixtures/build/dts/tsconfigPath-bundleless.ts b/tests/integration/module/fixtures/build/dts/tsconfigPath-bundleless.ts deleted file mode 100644 index df6c0d104ae0..000000000000 --- a/tests/integration/module/fixtures/build/dts/tsconfigPath-bundleless.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { defineConfig } from '@modern-js/module-tools/defineConfig'; - -export default defineConfig({ - buildConfig: { - alias: { - '@src2': './src2', - }, - buildType: 'bundleless', - sourceDir: './src2', - dts: { tsconfigPath: './tsconfig-bundleless.json' }, - outDir: './dist/tsconfig-path/bundleless', - }, -});