diff --git a/test/cli/basic/index.test.ts b/test/cli/basic/index.test.ts index e8d5f94c..96d20cd6 100644 --- a/test/cli/basic/index.test.ts +++ b/test/cli/basic/index.test.ts @@ -1,9 +1,9 @@ import fs from 'fs' -import { createCliJob } from '../../testing-utils' +import { runCli } from '../../testing-utils' describe('cli', () => { it(`cli basic should work properly`, async () => { - const { code, distFile } = await createCliJob({ + const { code, distFile } = await runCli({ directory: __dirname, args: ['hello.js', '-o', 'dist/hello.bundle.js'], }) @@ -12,7 +12,7 @@ describe('cli', () => { }) it(`cli format should work properly`, async () => { - const { code, distFile } = await createCliJob({ + const { code, distFile } = await runCli({ directory: __dirname, args: ['hello.js', '-f', 'cjs', '-o', 'dist/hello.cjs'], }) @@ -24,7 +24,7 @@ describe('cli', () => { }) it(`cli compress should work properly`, async () => { - const { code, distFile } = await createCliJob({ + const { code, distFile } = await runCli({ directory: __dirname, args: ['hello.js', '-m', '-o', 'dist/hello.bundle.min.js'], }) @@ -36,7 +36,7 @@ describe('cli', () => { }) it(`cli sourcemap should work properly`, async () => { - const { code, distFile } = await createCliJob({ + const { code, distFile } = await runCli({ directory: __dirname, args: ['hello.js', '--sourcemap', '-o', 'dist/hello.js'], }) @@ -51,7 +51,7 @@ describe('cli', () => { }) it(`cli minified with sourcemap should work properly`, async () => { - const { code, distFile } = await createCliJob({ + const { code, distFile } = await runCli({ directory: __dirname, args: ['hello.js', '-m', '--sourcemap', '-o', 'dist/hello.js'], }) diff --git a/test/cli/cjs-relative-imports/index.test.ts b/test/cli/cjs-relative-imports/index.test.ts index ed1eacb0..5d057e3f 100644 --- a/test/cli/cjs-relative-imports/index.test.ts +++ b/test/cli/cjs-relative-imports/index.test.ts @@ -1,8 +1,8 @@ import fs from 'fs' -import { createCliJob } from '../../testing-utils' +import { runCli } from '../../testing-utils' test(`cli cjs-relative-imports should work properly`, async () => { - const { code, distFile } = await createCliJob({ + const { code, distFile } = await runCli({ directory: __dirname, args: ['index.js', '-o', 'dist/index.js'], }) diff --git a/test/cli/dts-bundle/index.test.ts b/test/cli/dts-bundle/index.test.ts index fd909df7..9e5c5889 100644 --- a/test/cli/dts-bundle/index.test.ts +++ b/test/cli/dts-bundle/index.test.ts @@ -1,7 +1,7 @@ import fs from 'fs' import fsp from 'fs/promises' import { join } from 'path' -import { createCliJob, deleteFile } from '../../testing-utils' +import { runCli, deleteFile } from '../../testing-utils' describe('cli', () => { const dir = __dirname @@ -16,7 +16,7 @@ describe('cli', () => { }) it(`cli dts-bundle option should work properly`, async () => { - const { code, distFile } = await createCliJob({ + const { code, distFile } = await runCli({ directory: __dirname, args: ['./base.ts', '-o', 'dist/base.js', '--dts-bundle'], }) @@ -27,7 +27,7 @@ describe('cli', () => { }) it(`cli dts-bundle option should not bundle dts without dts-bundle`, async () => { - const { code, distFile } = await createCliJob({ + const { code, distFile } = await runCli({ directory: __dirname, args: ['./base.ts', '-o', 'dist/base.js'], }) @@ -42,7 +42,7 @@ describe('cli', () => { join(dir, './package.json'), '{ "name": "prepare-ts-with-pkg-json", "dependencies": { "@swc/types": "*" } }', ) - const { code, distFile } = await createCliJob({ + const { code, distFile } = await runCli({ directory: __dirname, args: ['./base.ts', '-o', 'dist/base.js'], }) diff --git a/test/cli/dts/index.test.ts b/test/cli/dts/index.test.ts index 0fefec09..fb82a305 100644 --- a/test/cli/dts/index.test.ts +++ b/test/cli/dts/index.test.ts @@ -1,10 +1,10 @@ import fs from 'fs' import path from 'path' -import { createCliJob } from '../../testing-utils' +import { runCli } from '../../testing-utils' describe('cli', () => { it(`cli dts should work properly`, async () => { - const { code, distFile } = await createCliJob({ + const { code, distFile } = await runCli({ directory: __dirname, args: ['./input.ts', '-o', 'dist/output.js'], }) diff --git a/test/cli/env-var/index.test.ts b/test/cli/env-var/index.test.ts index ee65c844..8fd7bb58 100644 --- a/test/cli/env-var/index.test.ts +++ b/test/cli/env-var/index.test.ts @@ -1,8 +1,8 @@ import fs from 'fs' -import { createCliJob } from '../../testing-utils' +import { runCli } from '../../testing-utils' test(`cli env-var should work properly`, async () => { - const { code, distFile } = await createCliJob({ + const { code, distFile } = await runCli({ directory: __dirname, args: ['index.js', '--env', 'MY_TEST_ENV', '-o', 'dist/index.js'], options: { diff --git a/test/cli/externals/index.test.ts b/test/cli/externals/index.test.ts index abcb9ad4..4c0aa348 100644 --- a/test/cli/externals/index.test.ts +++ b/test/cli/externals/index.test.ts @@ -1,9 +1,9 @@ import fs from 'fs' -import { createCliJob } from '../../testing-utils' +import { runCli } from '../../testing-utils' describe('cli', () => { it(`cli external should work properly`, async () => { - const { code, distFile } = await createCliJob({ + const { code, distFile } = await runCli({ directory: __dirname, args: [ 'with-externals.js', @@ -21,7 +21,7 @@ describe('cli', () => { }) it(`cli no-externals should work properly`, async () => { - const { code, distFile } = await createCliJob({ + const { code, distFile } = await runCli({ directory: __dirname, args: [ 'with-externals.js', diff --git a/test/cli/no-dts/index.test.ts b/test/cli/no-dts/index.test.ts index 83a0b05f..b7a77b34 100644 --- a/test/cli/no-dts/index.test.ts +++ b/test/cli/no-dts/index.test.ts @@ -1,9 +1,9 @@ import fs from 'fs' import path from 'path' -import { createCliJob } from '../../testing-utils' +import { runCli } from '../../testing-utils' test(`cli no-dts option should work properly`, async () => { - const { code, distFile } = await createCliJob({ + const { code, distFile } = await runCli({ directory: __dirname, args: ['./base.ts', '-o', 'dist/base.js', '--no-dts'], }) diff --git a/test/cli/no-entry/index.test.ts b/test/cli/no-entry/index.test.ts index a46e7635..e9e5ede5 100644 --- a/test/cli/no-entry/index.test.ts +++ b/test/cli/no-entry/index.test.ts @@ -1,7 +1,7 @@ -import { createCliJob } from '../../testing-utils' +import { runCli } from '../../testing-utils' test(`cli no-entry should work properly`, async () => { - const { code, stderr } = await createCliJob({ + const { code, stderr } = await runCli({ directory: __dirname, }) expect( diff --git a/test/cli/output-in-watch/index.test.ts b/test/cli/output-in-watch/index.test.ts index e53148ec..43f07bc5 100644 --- a/test/cli/output-in-watch/index.test.ts +++ b/test/cli/output-in-watch/index.test.ts @@ -1,7 +1,7 @@ -import { createCliJob } from '../../testing-utils' +import { runCli } from '../../testing-utils' test('cli - output-in-watch', async () => { - const { stdout, signal } = await createCliJob({ + const { stdout, signal } = await runCli({ directory: __dirname, args: ['hello.js', '-w', '-o', 'dist/hello.bundle.js'], abortTimeout: 3000, diff --git a/test/cli/target/index.test.ts b/test/cli/target/index.test.ts index 363272b5..eb8aecef 100644 --- a/test/cli/target/index.test.ts +++ b/test/cli/target/index.test.ts @@ -1,8 +1,8 @@ import fs from 'fs' -import { createCliJob } from '../../testing-utils' +import { runCli } from '../../testing-utils' test(`cli es2020-target should work properly`, async () => { - const { code, distFile } = await createCliJob({ + const { code, distFile } = await runCli({ directory: __dirname, args: ['es2020.ts', '--target', 'es2020', '-o', 'dist/es2020.js'], }) diff --git a/test/cli/workspace/index.test.ts b/test/cli/workspace/index.test.ts index cab78b17..7950e785 100644 --- a/test/cli/workspace/index.test.ts +++ b/test/cli/workspace/index.test.ts @@ -1,10 +1,10 @@ import fs from 'fs' -import { createCliJob } from '../../testing-utils' +import { runCli } from '../../testing-utils' import path from 'path' describe('cli', () => { it(`cli workspace should work properly`, async () => { - const { code, distDir } = await createCliJob({ + const { code, distDir } = await runCli({ directory: __dirname, args: ['./index.ts'], }) diff --git a/test/integration/bin/cts/index.test.ts b/test/integration/bin/cts/index.test.ts index 4cd5d595..63daea0d 100644 --- a/test/integration/bin/cts/index.test.ts +++ b/test/integration/bin/cts/index.test.ts @@ -1,19 +1,12 @@ -import { - assertFilesContent, - createIntegrationTest, -} from '../../../testing-utils' +import { assertFilesContent, createJob } from '../../../testing-utils' describe('integration bin/cts', () => { + const { distDir } = createJob({ + directory: __dirname, + }) it('should work with bin as .cts extension', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - await assertFilesContent(distDir, { - 'bin/index.cjs': '#!/usr/bin/env node', - }) - }, - ) + await assertFilesContent(distDir, { + 'bin/index.cjs': '#!/usr/bin/env node', + }) }) }) diff --git a/test/integration/bin/multi-path/index.test.ts b/test/integration/bin/multi-path/index.test.ts index be441212..adacec85 100644 --- a/test/integration/bin/multi-path/index.test.ts +++ b/test/integration/bin/multi-path/index.test.ts @@ -1,6 +1,6 @@ import { assertFilesContent, - createIntegrationTest, + createJob, isWindows, } from '../../../testing-utils' @@ -10,17 +10,13 @@ describe('integration bin/multi-path', () => { it('skip test on windows', () => {}) return } + const { distDir } = createJob({ + directory: __dirname, + }) it('should work with bin as multi path', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - await assertFilesContent(distDir, { - 'bin/a.js': '#!/usr/bin/env node', - 'bin/b.js': '#!/usr/bin/env node', - }) - }, - ) + await assertFilesContent(distDir, { + 'bin/a.js': '#!/usr/bin/env node', + 'bin/b.js': '#!/usr/bin/env node', + }) }) }) diff --git a/test/integration/bin/patch-binary/index.test.ts b/test/integration/bin/patch-binary/index.test.ts index 9e8ff8ee..5a359c4e 100644 --- a/test/integration/bin/patch-binary/index.test.ts +++ b/test/integration/bin/patch-binary/index.test.ts @@ -1,22 +1,16 @@ import { - createIntegrationTest, assertContainFiles, assertFilesContent, + createJob, } from '../../../testing-utils' describe('integration bin/patch-binary', () => { + const { distDir } = createJob({ directory: __dirname }) it('should patch binary directive', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const distFiles = ['bin.js'] - await assertContainFiles(distDir, distFiles) - await assertFilesContent(distDir, { - 'bin.js': "#!/usr/bin/env node\nconsole.log('Hello, world!');", - }) - }, - ) + const distFiles = ['bin.js'] + await assertContainFiles(distDir, distFiles) + await assertFilesContent(distDir, { + 'bin.js': "#!/usr/bin/env node\nconsole.log('Hello, world!');", + }) }) }) diff --git a/test/integration/bin/single-path/index.test.ts b/test/integration/bin/single-path/index.test.ts index facb1ba8..9edd4c7d 100644 --- a/test/integration/bin/single-path/index.test.ts +++ b/test/integration/bin/single-path/index.test.ts @@ -1,8 +1,7 @@ import { - createIntegrationTest, + createJob, assertContainFiles, assertFilesContent, - getFileNamesFromDirectory, isWindows, } from '../../../testing-utils' @@ -12,22 +11,12 @@ describe('integration bin/single-path', () => { it('skip test on windows', () => {}) return } + const { distDir } = createJob({ directory: __dirname }) it('should work with bin as single path', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - console.log( - 'getFileNamesFromDirectory', - getFileNamesFromDirectory(distDir), - ) - const distFiles = ['bin.js'] - await assertContainFiles(distDir, distFiles) - await assertFilesContent(distDir, { - 'bin.js': /console.log\('Hello, world!'\)/, - }) - }, - ) + const distFiles = ['bin.js'] + await assertContainFiles(distDir, distFiles) + await assertFilesContent(distDir, { + 'bin.js': /console.log\('Hello, world!'\)/, + }) }) }) diff --git a/test/integration/browserslist/index.test.ts b/test/integration/browserslist/index.test.ts index 4aca0a14..5a9c31c5 100644 --- a/test/integration/browserslist/index.test.ts +++ b/test/integration/browserslist/index.test.ts @@ -1,16 +1,10 @@ -import { createIntegrationTest, assertFilesContent } from '../../testing-utils' +import { assertFilesContent, createJob } from '../../testing-utils' describe('browserslist', () => { + const { distDir } = createJob({ directory: __dirname }) it('should work with basic JSX format', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - ({ distDir }) => { - assertFilesContent(distDir, { - 'index.js': `_class_private_field_loose_key`, - }) - }, - ) + await assertFilesContent(distDir, { + 'index.js': `_class_private_field_loose_key`, + }) }) }) diff --git a/test/integration/conflicted-entry/conflicted-entry.test.ts b/test/integration/conflicted-entry/conflicted-entry.test.ts index 4295e60c..903fa3f4 100644 --- a/test/integration/conflicted-entry/conflicted-entry.test.ts +++ b/test/integration/conflicted-entry/conflicted-entry.test.ts @@ -1,17 +1,10 @@ -import { createIntegrationTest } from '../../testing-utils' +import { createJob } from '../../testing-utils' describe('integration - conflicted-entry', () => { + const { job } = createJob({ directory: __dirname }) it('should error on conflicted entries', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ code, stderr }) => { - expect(code).toBe(1) - expect(stderr).toContain( - 'Conflicted entry files found for entries: ./foo', - ) - }, - ) + const { code, stderr } = job + expect(code).toBe(1) + expect(stderr).toContain('Conflicted entry files found for entries: ./foo') }) }) diff --git a/test/integration/default-node-mjs/index.test.ts b/test/integration/default-node-mjs/index.test.ts index 9fd7b2dd..28c48e6e 100644 --- a/test/integration/default-node-mjs/index.test.ts +++ b/test/integration/default-node-mjs/index.test.ts @@ -1,22 +1,17 @@ import { readFile } from 'fs/promises' -import { createIntegrationTest, existsFile } from '../../testing-utils' +import { createJob, existsFile } from '../../testing-utils' describe('integration default-node-mjs', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should work with .mjs extension', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const distFiles = [`${distDir}/index.node.mjs`] + const distFiles = [`${distDir}/index.node.mjs`] - for (const f of distFiles) { - expect(await existsFile(f)).toBe(true) - } + for (const f of distFiles) { + expect(await existsFile(f)).toBe(true) + } - expect(await readFile(distFiles[0], 'utf-8')).toContain('export {') - expect(await readFile(distFiles[0], 'utf-8')).not.toContain('exports') - }, - ) + expect(await readFile(distFiles[0], 'utf-8')).toContain('export {') + expect(await readFile(distFiles[0], 'utf-8')).not.toContain('exports') }) }) diff --git a/test/integration/dev-prod-convention-reexport/index.test.ts b/test/integration/dev-prod-convention-reexport/index.test.ts index b4b65e1f..2bdf543f 100644 --- a/test/integration/dev-prod-convention-reexport/index.test.ts +++ b/test/integration/dev-prod-convention-reexport/index.test.ts @@ -1,22 +1,17 @@ -import { createIntegrationTest, assertFilesContent } from '../../testing-utils' +import { createJob, assertFilesContent } from '../../testing-utils' describe('integration dev-prod-convention-reexport', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should work with dev and prod optimize conditions', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - await assertFilesContent(distDir, { - // index export - 'index.dev.js': /core.dev.js/, - 'index.dev.mjs': /core.dev.mjs/, - 'index.prod.js': /core.prod.js/, - 'index.prod.mjs': /core.prod.mjs/, - 'index.js': /core.js/, - 'index.mjs': /core.mjs/, - }) - }, - ) + await assertFilesContent(distDir, { + // index export + 'index.dev.js': /core.dev.js/, + 'index.dev.mjs': /core.dev.mjs/, + 'index.prod.js': /core.prod.js/, + 'index.prod.mjs': /core.prod.mjs/, + 'index.js': /core.js/, + 'index.mjs': /core.mjs/, + }) }) }) diff --git a/test/integration/dev-prod-convention/index.test.ts b/test/integration/dev-prod-convention/index.test.ts index e70ff04b..b7063c8a 100644 --- a/test/integration/dev-prod-convention/index.test.ts +++ b/test/integration/dev-prod-convention/index.test.ts @@ -1,22 +1,17 @@ -import { createIntegrationTest, assertFilesContent } from '../../testing-utils' +import { createJob, assertFilesContent } from '../../testing-utils' describe('integration dev-prod-convention', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should work with dev and prod optimize conditions', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - await assertFilesContent(distDir, { - 'index.development.js': /= "development"/, - 'index.development.mjs': /= "development"/, - 'index.production.js': /= "production"/, - 'index.production.mjs': /= "production"/, - // Do not replace NODE_ENV by default - 'index.js': /= process.env.NODE_ENV/, - 'index.mjs': /= process.env.NODE_ENV/, - }) - }, - ) + await assertFilesContent(distDir, { + 'index.development.js': /= "development"/, + 'index.development.mjs': /= "development"/, + 'index.production.js': /= "production"/, + 'index.production.mjs': /= "production"/, + // Do not replace NODE_ENV by default + 'index.js': /= process.env.NODE_ENV/, + 'index.mjs': /= process.env.NODE_ENV/, + }) }) }) diff --git a/test/integration/dev-prod-nested-convention/index.test.ts b/test/integration/dev-prod-nested-convention/index.test.ts index 3173645c..2a1d6133 100644 --- a/test/integration/dev-prod-nested-convention/index.test.ts +++ b/test/integration/dev-prod-nested-convention/index.test.ts @@ -1,30 +1,25 @@ -import { createIntegrationTest, assertFilesContent } from '../../testing-utils' +import { createJob, assertFilesContent } from '../../testing-utils' describe('integration dev-prod-nested-convention', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should work with dev and prod optimize conditions', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - await assertFilesContent(distDir, { - // index export - 'index.development.js': /= "development"/, - 'index.development.mjs': /= "development"/, - 'index.production.js': /= "production"/, - 'index.production.mjs': /= "production"/, - 'index.js': /= 'index'/, - 'index.mjs': /= 'index'/, + await assertFilesContent(distDir, { + // index export + 'index.development.js': /= "development"/, + 'index.development.mjs': /= "development"/, + 'index.production.js': /= "production"/, + 'index.production.mjs': /= "production"/, + 'index.js': /= 'index'/, + 'index.mjs': /= 'index'/, - // core export - 'core.development.js': `= 'core' + "development"`, - 'core.development.mjs': `= 'core' + "development"`, - 'core.production.js': `= 'core' + "production"`, - 'core.production.mjs': `= 'core' + "production"`, - 'core.js': /= 'core'/, - 'core.mjs': /= 'core'/, - }) - }, - ) + // core export + 'core.development.js': `= 'core' + "development"`, + 'core.development.mjs': `= 'core' + "development"`, + 'core.production.js': `= 'core' + "production"`, + 'core.production.mjs': `= 'core' + "production"`, + 'core.js': /= 'core'/, + 'core.mjs': /= 'core'/, + }) }) }) diff --git a/test/integration/dev-prod-special-convention/index.test.ts b/test/integration/dev-prod-special-convention/index.test.ts index e9e2497b..de2dac1a 100644 --- a/test/integration/dev-prod-special-convention/index.test.ts +++ b/test/integration/dev-prod-special-convention/index.test.ts @@ -1,23 +1,18 @@ -import { createIntegrationTest, assertFilesContent } from '../../testing-utils' +import { createJob, assertFilesContent } from '../../testing-utils' describe('integration dev-prod-special-convention', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should work with dev prod and special optimize conditions', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - await assertFilesContent(distDir, { - 'index.react-server.mjs': 'index.react-server', - 'index.development.js': /'index.default'/, - 'index.development.mjs': /'index.default'/, - 'index.production.js': /'index.default'/, - 'index.production.mjs': /'index.default'/, - // In jest the NODE_ENV is set to test - 'index.js': /'index.default'/, - 'index.mjs': /'index.default'/, - }) - }, - ) + await assertFilesContent(distDir, { + 'index.react-server.mjs': 'index.react-server', + 'index.development.js': /'index.default'/, + 'index.development.mjs': /'index.default'/, + 'index.production.js': /'index.default'/, + 'index.production.mjs': /'index.default'/, + // In jest the NODE_ENV is set to test + 'index.js': /'index.default'/, + 'index.mjs': /'index.default'/, + }) }) }) diff --git a/test/integration/duplicate-entries-partial-specified/index.test.ts b/test/integration/duplicate-entries-partial-specified/index.test.ts index 383c5a23..5e67c86e 100644 --- a/test/integration/duplicate-entries-partial-specified/index.test.ts +++ b/test/integration/duplicate-entries-partial-specified/index.test.ts @@ -1,20 +1,12 @@ -import { - createIntegrationTest, - getFileNamesFromDirectory, -} from '../../testing-utils' +import { createJob, getFileNamesFromDirectory } from '../../testing-utils' describe('integration duplicate-entries-partial-specified', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should not generate js types paths if not specified', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - expect(await getFileNamesFromDirectory(distDir)).toEqual([ - 'a.js', - 'a.workerd.js', - ]) - }, - ) + expect(await getFileNamesFromDirectory(distDir)).toEqual([ + 'a.js', + 'a.workerd.js', + ]) }) }) diff --git a/test/integration/dynamic-require/dynamic-require.test.ts b/test/integration/dynamic-require/dynamic-require.test.ts index 03eca612..736dffee 100644 --- a/test/integration/dynamic-require/dynamic-require.test.ts +++ b/test/integration/dynamic-require/dynamic-require.test.ts @@ -1,39 +1,31 @@ import fs from 'fs' -import { - createIntegrationTest, - getFileNamesFromDirectory, -} from '../../testing-utils' +import { createJob, getFileNamesFromDirectory } from '../../testing-utils' describe('integration - dynamic-require', () => { - it('should work', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - expect(await getFileNamesFromDirectory(distDir)).toEqual([ - 'foo.cjs', - 'foo.js', - 'index.cjs', - 'index.js', - ]) - // TODO: add helper of read content of dist file + const { distDir } = createJob({ directory: __dirname }) - // For require calls to the relative path, the value should be bundled - expect(fs.readFileSync(`${distDir}/index.cjs`, 'utf-8')).toContain( - `return 'being-required'`, - ) - expect(fs.readFileSync(`${distDir}/index.js`, 'utf-8')).toContain( - `return 'being-required'`, - ) - // For require calls to the external library, the value should not be bundled - expect(fs.readFileSync(`${distDir}/foo.cjs`, 'utf-8')).not.toContain( - `external-lib-value`, - ) - expect(fs.readFileSync(`${distDir}/foo.js`, 'utf-8')).not.toContain( - `external-lib-value`, - ) - }, + it('should work to include the relative dynamic module', async () => { + expect(await getFileNamesFromDirectory(distDir)).toEqual([ + 'foo.cjs', + 'foo.js', + 'index.cjs', + 'index.js', + ]) + // TODO: add helper of read content of dist file + + // For require calls to the relative path, the value should be bundled + expect(fs.readFileSync(`${distDir}/index.cjs`, 'utf-8')).toContain( + `return 'being-required'`, + ) + expect(fs.readFileSync(`${distDir}/index.js`, 'utf-8')).toContain( + `return 'being-required'`, + ) + // For require calls to the external library, the value should not be bundled + expect(fs.readFileSync(`${distDir}/foo.cjs`, 'utf-8')).not.toContain( + `external-lib-value`, + ) + expect(fs.readFileSync(`${distDir}/foo.js`, 'utf-8')).not.toContain( + `external-lib-value`, ) }) }) diff --git a/test/integration/edge-variable/index.test.ts b/test/integration/edge-variable/index.test.ts index 8a50f2fd..cfb37ec9 100644 --- a/test/integration/edge-variable/index.test.ts +++ b/test/integration/edge-variable/index.test.ts @@ -1,17 +1,13 @@ -import { createIntegrationTest, assertFilesContent } from '../../testing-utils' +import { createJob, assertFilesContent } from '../../testing-utils' describe('integration edge-variable', () => { + const { distDir } = createJob({ + directory: __dirname, + }) it('should work with edge export condition', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - assertFilesContent(distDir, { - 'index.js': /typeof EdgeRuntime/, - 'index.edge.js': /const variable = \"string\"/, // typeof "edge-runtime" - }) - }, - ) + await assertFilesContent(distDir, { + 'index.js': /typeof EdgeRuntime/, + 'index.edge.js': /const variable = \"string\"/, // typeof "edge-runtime" + }) }) }) diff --git a/test/integration/esm-pkg-cjs-main-field/index.test.ts b/test/integration/esm-pkg-cjs-main-field/index.test.ts index bcf9b1f4..450ae9c4 100644 --- a/test/integration/esm-pkg-cjs-main-field/index.test.ts +++ b/test/integration/esm-pkg-cjs-main-field/index.test.ts @@ -1,15 +1,10 @@ -import { createIntegrationTest, assertContainFiles } from '../../testing-utils' +import { createJob, assertContainFiles } from '../../testing-utils' describe('integration esm-pkg-cjs-main-field', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should work with ESM package with CJS main field ', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const distFiles = ['index.cjs', 'index.mjs'] - assertContainFiles(distDir, distFiles) - }, - ) + const distFiles = ['index.cjs', 'index.mjs'] + assertContainFiles(distDir, distFiles) }) }) diff --git a/test/integration/esm-shims/index.test.ts b/test/integration/esm-shims/index.test.ts index 67c7b7ed..b3f1491d 100644 --- a/test/integration/esm-shims/index.test.ts +++ b/test/integration/esm-shims/index.test.ts @@ -1,38 +1,33 @@ import { - createIntegrationTest, assertFilesContent, getFileContents, + createJob, } from '../../testing-utils' describe('integration esm-shims', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should work with ESM shims', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const requirePolyfill = - 'const require = __node_cjsModule.createRequire(import.meta.url)' - const filenamePolyfill = - 'const __filename = __node_cjsUrl.fileURLToPath(import.meta.url)' - const dirnamePolyfill = - 'const __dirname = __node_cjsPath.dirname(__filename)' + const requirePolyfill = + 'const require = __node_cjsModule.createRequire(import.meta.url)' + const filenamePolyfill = + 'const __filename = __node_cjsUrl.fileURLToPath(import.meta.url)' + const dirnamePolyfill = + 'const __dirname = __node_cjsPath.dirname(__filename)' - await assertFilesContent(distDir, { - 'filename.mjs': filenamePolyfill, - 'dirname.mjs': dirnamePolyfill, - 'custom-require.mjs': (code) => !code.includes(requirePolyfill), - 'require.js': /require\(/, - 'filename.js': /__filename/, - 'dirname.js': /__dirname/, - 'custom-require.js': (code) => !code.includes(requirePolyfill), - }) + await assertFilesContent(distDir, { + 'filename.mjs': filenamePolyfill, + 'dirname.mjs': dirnamePolyfill, + 'custom-require.mjs': (code) => !code.includes(requirePolyfill), + 'require.js': /require\(/, + 'filename.js': /__filename/, + 'dirname.js': /__dirname/, + 'custom-require.js': (code) => !code.includes(requirePolyfill), + }) - const contents = await getFileContents(distDir, ['require.mjs']) - expect(contents['require.mjs']).not.toContain(requirePolyfill) - expect(contents['require.mjs']).toContain('function getRequireModule') - expect(contents['require.mjs']).toContain('import.meta.url') - }, - ) + const contents = await getFileContents(distDir, ['require.mjs']) + expect(contents['require.mjs']).not.toContain(requirePolyfill) + expect(contents['require.mjs']).toContain('function getRequireModule') + expect(contents['require.mjs']).toContain('import.meta.url') }) }) diff --git a/test/integration/externals/index.test.ts b/test/integration/externals/index.test.ts index 7ed524d9..75e35aa8 100644 --- a/test/integration/externals/index.test.ts +++ b/test/integration/externals/index.test.ts @@ -1,18 +1,13 @@ import { readFile } from 'fs/promises' -import { createIntegrationTest } from '../../testing-utils' +import { createJob } from '../../testing-utils' describe('integration externals', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should handle externals', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const distFile = `${distDir}/index.js` - const content = await readFile(distFile, { encoding: 'utf-8' }) - expect(content).toMatch(/['"]peer-dep['"]/) - expect(content).toMatch(/['"]peer-dep-meta['"]/) - }, - ) + const distFile = `${distDir}/index.js` + const content = await readFile(distFile, { encoding: 'utf-8' }) + expect(content).toMatch(/['"]peer-dep['"]/) + expect(content).toMatch(/['"]peer-dep-meta['"]/) }) }) diff --git a/test/integration/import-fallback/index.test.ts b/test/integration/import-fallback/index.test.ts index b965ef13..6f5fa0f5 100644 --- a/test/integration/import-fallback/index.test.ts +++ b/test/integration/import-fallback/index.test.ts @@ -1,17 +1,12 @@ import { readFile } from 'fs/promises' -import { createIntegrationTest } from '../../testing-utils' +import { createJob } from '../../testing-utils' describe('integration import fallback', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should handle import fallback', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const aEdgeLightFile = `${distDir}/a.edge-light.js` - const content = await readFile(aEdgeLightFile, { encoding: 'utf-8' }) - expect(content).toMatch(/import '.\/b.js'/) - }, - ) + const aEdgeLightFile = `${distDir}/a.edge-light.js` + const content = await readFile(aEdgeLightFile, { encoding: 'utf-8' }) + expect(content).toMatch(/import '.\/b.js'/) }) }) diff --git a/test/integration/js-bad-configured-types/index.test.ts b/test/integration/js-bad-configured-types/index.test.ts index 3f5ca3e3..3eb50a5f 100644 --- a/test/integration/js-bad-configured-types/index.test.ts +++ b/test/integration/js-bad-configured-types/index.test.ts @@ -1,21 +1,17 @@ -import { createIntegrationTest } from '../../testing-utils' +import { createJob } from '../../testing-utils' describe('integration js-bad-configured-types', () => { + const { job } = createJob({ directory: __dirname }) + it('should error when types is not correctly configured', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ stderr, code }) => { - expect(code).toBe(0) + const { code, stderr } = job + expect(code).toBe(0) - expect(stderr).toContain( - `Bad export types field with import.require in ./dist/index.d.mts, use "types" export condition for it`, - ) - expect(stderr).toContain( - `Bad export types field with require in ./dist/index.d.ts, use "types" export condition for it`, - ) - }, + expect(stderr).toContain( + `Bad export types field with import.require in ./dist/index.d.mts, use "types" export condition for it`, + ) + expect(stderr).toContain( + `Bad export types field with require in ./dist/index.d.ts, use "types" export condition for it`, ) }) }) diff --git a/test/integration/js-only/index.test.ts b/test/integration/js-only/index.test.ts index 3539d7c1..d72222dc 100644 --- a/test/integration/js-only/index.test.ts +++ b/test/integration/js-only/index.test.ts @@ -1,6 +1,6 @@ -import { createIntegrationTest, existsFile } from '../../testing-utils' +import { createJob, existsFile } from '../../testing-utils' -describe('integration js-only', () => { +describe('integration - js-only', () => { beforeEach(() => { // Mock the 'typescript' module to throw an error on import jest.mock('typescript', () => { @@ -13,15 +13,11 @@ describe('integration js-only', () => { jest.resetModules() // Reset the module registry to ensure clean state jest.clearAllMocks() // Clear mocks after each test }) + + const { distDir } = createJob({ directory: __dirname }) + it('should work with js only project', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const distFile = `${distDir}/index.js` - expect(await existsFile(distFile)).toBe(true) - }, - ) + const distFile = `${distDir}/index.js` + expect(await existsFile(distFile)).toBe(true) }) }) diff --git a/test/integration/lint/cjs-pkg-esm-main-field/index.test.ts b/test/integration/lint/cjs-pkg-esm-main-field/index.test.ts index 18240f87..9a7cb09b 100644 --- a/test/integration/lint/cjs-pkg-esm-main-field/index.test.ts +++ b/test/integration/lint/cjs-pkg-esm-main-field/index.test.ts @@ -1,17 +1,14 @@ -import { createIntegrationTest } from '../../../testing-utils' +import { createJob } from '../../../testing-utils' describe('integration cjs-pkg-esm-main-field', () => { + const { job } = createJob({ + directory: __dirname, + args: ['lint'], + }) it('should warn if main field with .mjs extension in CJS package', async () => { - await createIntegrationTest( - { - args: ['lint'], - directory: __dirname, - }, - ({ stderr }) => { - expect(stderr).toContain( - 'Cannot export `main` field with .mjs extension in CJS package, only .js extension is allowed', - ) - }, + const { stderr } = job + expect(stderr).toContain( + 'Cannot export `main` field with .mjs extension in CJS package, only .js extension is allowed', ) }) }) diff --git a/test/integration/lint/invalid-exports-cjs/index.test.ts b/test/integration/lint/invalid-exports-cjs/index.test.ts index 33a161cf..f4d16cb8 100644 --- a/test/integration/lint/invalid-exports-cjs/index.test.ts +++ b/test/integration/lint/invalid-exports-cjs/index.test.ts @@ -1,24 +1,22 @@ -import { createIntegrationTest } from '../../../testing-utils' +import { createJob } from '../../../testing-utils' describe('integration invalid-exports-cjs', () => { - it('should warn on invalid exports as CJS', async () => { - await createIntegrationTest( - { - args: ['lint'], - directory: __dirname, - }, - async ({ stderr }) => { - expect(stderr).toContain('Missing package name') - expect(stderr).toContain( - 'Cannot export `require` field with .mjs extension in CJS package, only .cjs and .js extensions are allowed', - ) - expect(stderr).toContain('./dist/index.mjs') - expect(stderr).toContain( - 'Cannot export `import` field with .js or .cjs extension in CJS package, only .mjs extensions are allowed', - ) - expect(stderr).toContain('./dist/foo.js') - expect(stderr).not.toContain('./dist/index.esm.js') - }, + const { job } = createJob({ + directory: __dirname, + args: ['lint'], + }) + + it('should warn on invalid exports as CJS', () => { + const { stderr } = job + expect(stderr).toContain('Missing package name') + expect(stderr).toContain( + 'Cannot export `require` field with .mjs extension in CJS package, only .cjs and .js extensions are allowed', + ) + expect(stderr).toContain('./dist/index.mjs') + expect(stderr).toContain( + 'Cannot export `import` field with .js or .cjs extension in CJS package, only .mjs extensions are allowed', ) + expect(stderr).toContain('./dist/foo.js') + expect(stderr).not.toContain('./dist/index.esm.js') }) }) diff --git a/test/integration/lint/invalid-exports-esm/index.test.ts b/test/integration/lint/invalid-exports-esm/index.test.ts index a4d10285..a5cf6dc2 100644 --- a/test/integration/lint/invalid-exports-esm/index.test.ts +++ b/test/integration/lint/invalid-exports-esm/index.test.ts @@ -1,23 +1,21 @@ -import { createIntegrationTest } from '../../../testing-utils' +import { createJob } from '../../../testing-utils' describe('integration invalid-exports-esm', () => { + const { job } = createJob({ + directory: __dirname, + args: ['lint'], + }) + it('should warn on invalid exports as ESM', async () => { - await createIntegrationTest( - { - args: ['lint'], - directory: __dirname, - }, - async ({ stderr }) => { - expect(stderr).not.toContain('Missing package name') - expect(stderr).toContain( - 'Cannot export `require` field with .js or .mjs extension in ESM package, only .cjs extensions are allowed', - ) - expect(stderr).toContain('./dist/index.js') - expect(stderr).toContain( - 'Cannot export `import` field with .cjs extension in ESM package, only .js and .mjs extensions are allowed', - ) - expect(stderr).toContain('./dist/foo.cjs') - }, + const { stderr } = job + expect(stderr).not.toContain('Missing package name') + expect(stderr).toContain( + 'Cannot export `require` field with .js or .mjs extension in ESM package, only .cjs extensions are allowed', + ) + expect(stderr).toContain('./dist/index.js') + expect(stderr).toContain( + 'Cannot export `import` field with .cjs extension in ESM package, only .js and .mjs extensions are allowed', ) + expect(stderr).toContain('./dist/foo.cjs') }) }) diff --git a/test/integration/lint/missing-files-exports/index.test.ts b/test/integration/lint/missing-files-exports/index.test.ts index 146a04c1..45a07fb0 100644 --- a/test/integration/lint/missing-files-exports/index.test.ts +++ b/test/integration/lint/missing-files-exports/index.test.ts @@ -1,16 +1,14 @@ -import { createIntegrationTest } from '../../../testing-utils' +import { createJob } from '../../../testing-utils' describe('integration - lint - missing-files-exports', () => { + const { job } = createJob({ + directory: __dirname, + args: ['lint'], + }) + it('should warn on missing files', async () => { - await createIntegrationTest( - { - args: ['lint'], - directory: __dirname, - }, - async ({ stderr }) => { - expect(stderr).toContain('Missing files in package.json') - expect(stderr).toContain('foo/index.js') - }, - ) + const { stderr } = job + expect(stderr).toContain('Missing files in package.json') + expect(stderr).toContain('foo/index.js') }) }) diff --git a/test/integration/lint/missing-files-main/index.test.ts b/test/integration/lint/missing-files-main/index.test.ts index 6d271c17..b62326cb 100644 --- a/test/integration/lint/missing-files-main/index.test.ts +++ b/test/integration/lint/missing-files-main/index.test.ts @@ -1,16 +1,14 @@ -import { createIntegrationTest } from '../../../testing-utils' +import { createJob } from '../../../testing-utils' describe('integration - lint - missing-files-main', () => { - it('should warn on missing files', async () => { - await createIntegrationTest( - { - args: ['lint'], - directory: __dirname, - }, - async ({ stderr }) => { - expect(stderr).toContain('Missing files in package.json') - expect(stderr).toContain('index.js') - }, - ) + const { job } = createJob({ + directory: __dirname, + args: ['lint'], + }) + + it('should warn on missing files', () => { + const { stderr } = job + expect(stderr).toContain('Missing files in package.json') + expect(stderr).toContain('index.js') }) }) diff --git a/test/integration/lint/single-entry/index.test.ts b/test/integration/lint/single-entry/index.test.ts index 13e19821..9bbab60c 100644 --- a/test/integration/lint/single-entry/index.test.ts +++ b/test/integration/lint/single-entry/index.test.ts @@ -1,36 +1,33 @@ import { assertContainFiles, assertFilesContent, + createJob, stripANSIColor, } from '../../../testing-utils' -import { createIntegrationTest } from '../../../testing-utils' -describe('integration single-entry', () => { +describe('integration - single-entry', () => { + const { job, distDir } = createJob({ + directory: __dirname, + }) it('should warn on invalid exports as CJS', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir, stdout, stderr }) => { - const distFiles = ['index.js', 'index.d.ts'] + const { stdout, stderr } = job + const distFiles = ['index.js', 'index.d.ts'] - await assertContainFiles(distDir, distFiles) - await assertFilesContent(distDir, { - 'index.js': `Object.defineProperty(exports, '__esModule', { value: true });`, - 'index.d.ts': 'declare const _default: () => string;', - }) + await assertContainFiles(distDir, distFiles) + await assertFilesContent(distDir, { + 'index.js': `Object.defineProperty(exports, '__esModule', { value: true });`, + 'index.d.ts': 'declare const _default: () => string;', + }) - const log = `\ - dist/index.d.ts - dist/index.js` + const log = `\ + dist/index.d.ts + dist/index.js` - expect(stderr).not.toContain('Cannot export `exports` field with') + expect(stderr).not.toContain('Cannot export `exports` field with') - const rawStdout = stripANSIColor(stdout) - log.split('\n').forEach((line: string) => { - expect(rawStdout).toContain(line.trim()) - }) - }, - ) + const rawStdout = stripANSIColor(stdout) + log.split('\n').forEach((line: string) => { + expect(rawStdout).toContain(line.trim()) + }) }) }) diff --git a/test/integration/monorepo-composite/index.test.ts b/test/integration/monorepo-composite/index.test.ts index 10a318b2..f5e978da 100644 --- a/test/integration/monorepo-composite/index.test.ts +++ b/test/integration/monorepo-composite/index.test.ts @@ -1,15 +1,12 @@ import { join } from 'path' -import { assertContainFiles, createIntegrationTest } from '../../testing-utils' +import { assertContainFiles, createJob } from '../../testing-utils' describe('integration monorepo-composite', () => { + const { distDir } = createJob({ + directory: join(__dirname, 'packages', 'a'), + }) + it('should succeed the build', async () => { - await createIntegrationTest( - { - directory: join(__dirname, 'packages', 'a'), - }, - async ({ distDir }) => { - await assertContainFiles(distDir, ['index.js', 'index.d.ts']) - }, - ) + await assertContainFiles(distDir, ['index.js', 'index.d.ts']) }) }) diff --git a/test/integration/monorepo-composite/tsconfig.json b/test/integration/monorepo-composite/tsconfig.json index cac11b63..b1b3e2e3 100644 --- a/test/integration/monorepo-composite/tsconfig.json +++ b/test/integration/monorepo-composite/tsconfig.json @@ -7,5 +7,6 @@ { "path": "./packages/a" } - ] + ], + "exclude": ["*.test.ts"] } diff --git a/test/integration/multi-exports-ts/index.test.ts b/test/integration/multi-exports-ts/index.test.ts index 60907588..565fd0cb 100644 --- a/test/integration/multi-exports-ts/index.test.ts +++ b/test/integration/multi-exports-ts/index.test.ts @@ -1,7 +1,7 @@ -import { createIntegration, assertFilesContent } from '../../testing-utils' +import { createJob, assertFilesContent } from '../../testing-utils' describe('integration multi-exports', () => { - const { dir } = createIntegration({ + const { dir } = createJob({ directory: __dirname, }) it('should work with multi exports condition', async () => { diff --git a/test/integration/multi-types/index.test.ts b/test/integration/multi-types/index.test.ts index 910afd8f..13975216 100644 --- a/test/integration/multi-types/index.test.ts +++ b/test/integration/multi-types/index.test.ts @@ -1,40 +1,36 @@ import { + createJob, assertFilesContent, - createIntegrationTest, getChunkFileNamesFromLog, stripANSIColor, } from '../../testing-utils' describe('integration multi-types', () => { + const { distDir, job } = createJob({ directory: __dirname }) + it('should contain files', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir, stdout }) => { - const contentsRegex = { - 'index.js': /'index'/, - // types - 'index.d.ts': `declare const index`, - 'index.d.mts': `declare const index`, - 'index.d.cts': `declare const index`, - } + const { stdout } = job + const contentsRegex = { + 'index.js': /'index'/, + // types + 'index.d.ts': `declare const index`, + 'index.d.mts': `declare const index`, + 'index.d.cts': `declare const index`, + } - await assertFilesContent(distDir, contentsRegex) + await assertFilesContent(distDir, contentsRegex) - const log = `\ - dist/index.cjs - dist/index.js - dist/index.mjs - dist/index.d.mts - dist/index.d.cts - ` + const log = `\ + dist/index.cjs + dist/index.js + dist/index.mjs + dist/index.d.mts + dist/index.d.cts + ` - const rawStdout = stripANSIColor(stdout) - getChunkFileNamesFromLog(log).forEach((chunk: string) => { - expect(rawStdout).toContain(chunk) - }) - }, - ) + const rawStdout = stripANSIColor(stdout) + getChunkFileNamesFromLog(log).forEach((chunk: string) => { + expect(rawStdout).toContain(chunk) + }) }) }) diff --git a/test/integration/nested-exports/index.test.ts b/test/integration/nested-exports/index.test.ts index e5b2d49c..61e8fb06 100644 --- a/test/integration/nested-exports/index.test.ts +++ b/test/integration/nested-exports/index.test.ts @@ -1,16 +1,11 @@ -import { assertFilesContent, createIntegrationTest } from '../../testing-utils' +import { assertFilesContent, createJob } from '../../testing-utils' describe('integration nested-exports', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should work with nested path in exports', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - await assertFilesContent(distDir, { - 'foo/bar.js': "'foo.bar'", - }) - }, - ) + await assertFilesContent(distDir, { + 'foo/bar.js': "'foo.bar'", + }) }) }) diff --git a/test/integration/no-clean/index.test.ts b/test/integration/no-clean/index.test.ts index 80222425..71ad9e82 100644 --- a/test/integration/no-clean/index.test.ts +++ b/test/integration/no-clean/index.test.ts @@ -4,7 +4,7 @@ import { execSync } from 'child_process' import { assertContainFiles, assertFilesContent, - createIntegration, + createJob, } from '../../testing-utils' import { existsSync } from 'fs' @@ -16,7 +16,7 @@ describe('integration - no-clean flag', () => { await fsp.writeFile(join(distDir, 'no-clean.json'), '{}') }) - createIntegration({ + createJob({ directory: __dirname, args: ['--no-clean'], }) @@ -41,7 +41,7 @@ describe('integration - no-clean default', () => { await fsp.writeFile(join(distDir, 'no-clean.json'), '{}') }) - createIntegration({ + createJob({ directory: __dirname, }) diff --git a/test/integration/node-mixed-legacy-modern-entries/index.test.ts b/test/integration/node-mixed-legacy-modern-entries/index.test.ts index 30fd3321..76257041 100644 --- a/test/integration/node-mixed-legacy-modern-entries/index.test.ts +++ b/test/integration/node-mixed-legacy-modern-entries/index.test.ts @@ -1,19 +1,15 @@ -import { createIntegrationTest, assertContainFiles } from '../../testing-utils' +import { createJob, assertContainFiles } from '../../testing-utils' describe('node-mixed-legacy-modern-entries', () => { + const { distDir, job } = createJob({ directory: __dirname }) + it('should deduplicate entries', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir, stdout }) => { - const distFiles = ['index.js', 'index.mjs', 'index.d.ts', 'index.d.mts'] - assertContainFiles(distDir, distFiles) - for (const filename of distFiles) { - // only contain file name once - expect(stdout.split(filename).length).toBe(2) - } - }, - ) + const { stdout } = job + const distFiles = ['index.js', 'index.mjs', 'index.d.ts', 'index.d.mts'] + assertContainFiles(distDir, distFiles) + for (const filename of distFiles) { + // only contain file name once + expect(stdout.split(filename).length).toBe(2) + } }) }) diff --git a/test/integration/output-short/index.test.ts b/test/integration/output-short/index.test.ts index df7b68c2..ac834fa3 100644 --- a/test/integration/output-short/index.test.ts +++ b/test/integration/output-short/index.test.ts @@ -1,4 +1,4 @@ -import { createIntegrationTest, stripANSIColor } from '../../testing-utils' +import { createJob, stripANSIColor } from '../../testing-utils' const getOutputSizeColumnIndex = (line: string): number => { let match @@ -9,36 +9,32 @@ const getOutputSizeColumnIndex = (line: string): number => { } describe('integration output-short', () => { + const { job } = createJob({ directory: __dirname }) + it('should match output with exports', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ stdout }) => { - /* - output: - - Exports File Size - . dist/index.js 30 B - */ - const [tableHeads, indexLine] = stripANSIColor(stdout).split('\n') - expect(tableHeads).toContain('Exports') - expect(tableHeads).toContain('File') - expect(tableHeads).toContain('Size') + const { stdout } = job + /* + output: + + Exports File Size + . dist/index.js 30 B + */ + const [tableHeads, indexLine] = stripANSIColor(stdout).split('\n') + expect(tableHeads).toContain('Exports') + expect(tableHeads).toContain('File') + expect(tableHeads).toContain('Size') - expect(indexLine).toContain('.') - expect(indexLine).toContain('dist/index.js') + expect(indexLine).toContain('.') + expect(indexLine).toContain('dist/index.js') - const [exportsIndex, fileIndex, sizeIndex] = [ - tableHeads.indexOf('Exports'), - tableHeads.indexOf('File'), - tableHeads.indexOf('Size'), - ] + const [exportsIndex, fileIndex, sizeIndex] = [ + tableHeads.indexOf('Exports'), + tableHeads.indexOf('File'), + tableHeads.indexOf('Size'), + ] - expect(indexLine.indexOf('.')).toEqual(exportsIndex) - expect(indexLine.indexOf('dist/index.js')).toEqual(fileIndex) - expect(getOutputSizeColumnIndex(indexLine)).toEqual(sizeIndex) - }, - ) + expect(indexLine.indexOf('.')).toEqual(exportsIndex) + expect(indexLine.indexOf('dist/index.js')).toEqual(fileIndex) + expect(getOutputSizeColumnIndex(indexLine)).toEqual(sizeIndex) }) }) diff --git a/test/integration/output/index.test.ts b/test/integration/output/index.test.ts index 495f7488..36d64bd0 100644 --- a/test/integration/output/index.test.ts +++ b/test/integration/output/index.test.ts @@ -1,4 +1,4 @@ -import { createIntegrationTest, stripANSIColor } from '../../testing-utils' +import { createJob, stripANSIColor } from '../../testing-utils' const getOutputSizeColumnIndex = (line: string): number => { let match @@ -9,77 +9,71 @@ const getOutputSizeColumnIndex = (line: string): number => { } describe('integration output', () => { + const { job } = createJob({ directory: __dirname }) + it('should match output with exports', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ stdout }) => { - /* - output: - - Exports File Size - cli (bin) dist/cli.js 103 B - . dist/index.js 42 B - . (react-server) dist/index.react-server.js 55 B - ./foo dist/foo.js 103 B - */ + /* + output: - const lines = stripANSIColor(stdout).split('\n') - const [tableHeads, ...restLines] = lines - const cliLine = restLines.find((line) => line.includes('cli'))! - const indexLine = restLines.find( - (line) => line.includes('. ') && !line.includes('react-server'), - )! - const indexReactServerLine = restLines.find((line) => - line.includes('. (react-server)'), - )! - const fooLine = restLines.find((line) => line.includes('./foo'))! + Exports File Size + cli (bin) dist/cli.js 103 B + . dist/index.js 42 B + . (react-server) dist/index.react-server.js 55 B + ./foo dist/foo.js 103 B + */ - expect(tableHeads).toContain('Exports') - expect(tableHeads).toContain('File') - expect(tableHeads).toContain('Size') + const { stdout } = job + const lines = stripANSIColor(stdout).split('\n') + const [tableHeads, ...restLines] = lines + const cliLine = restLines.find((line) => line.includes('cli'))! + const indexLine = restLines.find( + (line) => line.includes('. ') && !line.includes('react-server'), + )! + const indexReactServerLine = restLines.find((line) => + line.includes('. (react-server)'), + )! + const fooLine = restLines.find((line) => line.includes('./foo'))! - expect(cliLine).toContain('cli (bin)') - expect(cliLine).toContain('dist/cli.js') + expect(tableHeads).toContain('Exports') + expect(tableHeads).toContain('File') + expect(tableHeads).toContain('Size') - expect(indexLine).toContain('.') - expect(indexLine).toContain('dist/index.js') + expect(cliLine).toContain('cli (bin)') + expect(cliLine).toContain('dist/cli.js') - expect(indexReactServerLine).toContain('. (react-server)') - expect(indexReactServerLine).toContain('dist/index.react-server.js') + expect(indexLine).toContain('.') + expect(indexLine).toContain('dist/index.js') - expect(fooLine).toContain('./foo') - expect(fooLine).toContain('dist/foo.js') + expect(indexReactServerLine).toContain('. (react-server)') + expect(indexReactServerLine).toContain('dist/index.react-server.js') - const [exportsIndex, fileIndex, sizeIndex] = [ - tableHeads.indexOf('Exports'), - tableHeads.indexOf('File'), - tableHeads.indexOf('Size'), - ] + expect(fooLine).toContain('./foo') + expect(fooLine).toContain('dist/foo.js') - expect(cliLine.indexOf('cli (bin)')).toEqual(exportsIndex) - expect(cliLine.indexOf('dist/cli.js')).toEqual(fileIndex) - expect(getOutputSizeColumnIndex(cliLine)).toEqual(sizeIndex) + const [exportsIndex, fileIndex, sizeIndex] = [ + tableHeads.indexOf('Exports'), + tableHeads.indexOf('File'), + tableHeads.indexOf('Size'), + ] - expect(indexLine.indexOf('.')).toEqual(exportsIndex) - expect(indexLine.indexOf('dist/index.js')).toEqual(fileIndex) - expect(getOutputSizeColumnIndex(indexLine)).toEqual(sizeIndex) + expect(cliLine.indexOf('cli (bin)')).toEqual(exportsIndex) + expect(cliLine.indexOf('dist/cli.js')).toEqual(fileIndex) + expect(getOutputSizeColumnIndex(cliLine)).toEqual(sizeIndex) - expect(indexReactServerLine.indexOf('. (react-server)')).toEqual( - exportsIndex, - ) - expect( - indexReactServerLine.indexOf('dist/index.react-server.js'), - ).toEqual(fileIndex) - expect(getOutputSizeColumnIndex(indexReactServerLine)).toEqual( - sizeIndex, - ) + expect(indexLine.indexOf('.')).toEqual(exportsIndex) + expect(indexLine.indexOf('dist/index.js')).toEqual(fileIndex) + expect(getOutputSizeColumnIndex(indexLine)).toEqual(sizeIndex) - expect(fooLine.indexOf('./foo')).toEqual(exportsIndex) - expect(fooLine.indexOf('dist/foo.js')).toEqual(fileIndex) - expect(getOutputSizeColumnIndex(fooLine)).toEqual(sizeIndex) - }, + expect(indexReactServerLine.indexOf('. (react-server)')).toEqual( + exportsIndex, + ) + expect(indexReactServerLine.indexOf('dist/index.react-server.js')).toEqual( + fileIndex, ) + expect(getOutputSizeColumnIndex(indexReactServerLine)).toEqual(sizeIndex) + + expect(fooLine.indexOf('./foo')).toEqual(exportsIndex) + expect(fooLine.indexOf('dist/foo.js')).toEqual(fileIndex) + expect(getOutputSizeColumnIndex(fooLine)).toEqual(sizeIndex) }) }) diff --git a/test/integration/pkg-exports-default/index.test.ts b/test/integration/pkg-exports-default/index.test.ts index 85f0be1a..e0bfcb89 100644 --- a/test/integration/pkg-exports-default/index.test.ts +++ b/test/integration/pkg-exports-default/index.test.ts @@ -1,26 +1,21 @@ import { promises as fsp } from 'fs' import { join } from 'path' -import { assertContainFiles, createIntegrationTest } from '../../testing-utils' +import { assertContainFiles, createJob } from '../../testing-utils' describe('integration pkg-exports-default', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should generate proper assets with js', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const distFiles = ['index.cjs', 'index.mjs'] - await assertContainFiles(distDir, distFiles) - const cjsFile = await fsp.readFile(join(distDir, 'index.cjs'), { - encoding: 'utf-8', - }) - expect(cjsFile).toContain( - `function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }`, - ) - expect(cjsFile).toContain( - `Object.defineProperty(exports, '__esModule', { value: true });`, - ) - }, + const distFiles = ['index.cjs', 'index.mjs'] + await assertContainFiles(distDir, distFiles) + const cjsFile = await fsp.readFile(join(distDir, 'index.cjs'), { + encoding: 'utf-8', + }) + expect(cjsFile).toContain( + `function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }`, + ) + expect(cjsFile).toContain( + `Object.defineProperty(exports, '__esModule', { value: true });`, ) }) }) diff --git a/test/integration/pkg-exports-js/index.test.ts b/test/integration/pkg-exports-js/index.test.ts index 078e4148..b4c81c73 100644 --- a/test/integration/pkg-exports-js/index.test.ts +++ b/test/integration/pkg-exports-js/index.test.ts @@ -1,15 +1,10 @@ -import { assertContainFiles, createIntegrationTest } from '../../testing-utils' +import { assertContainFiles, createJob } from '../../testing-utils' describe('integration pkg-exports-js', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should generate proper assets with js', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const distFiles = ['index.cjs', 'index.mjs', 'index.esm.js'] - assertContainFiles(distDir, distFiles) - }, - ) + const distFiles = ['index.cjs', 'index.mjs', 'index.esm.js'] + assertContainFiles(distDir, distFiles) }) }) diff --git a/test/integration/pkg-exports-ts-rsc/index.test.ts b/test/integration/pkg-exports-ts-rsc/index.test.ts index 3d3e3484..25f58361 100644 --- a/test/integration/pkg-exports-ts-rsc/index.test.ts +++ b/test/integration/pkg-exports-ts-rsc/index.test.ts @@ -1,26 +1,21 @@ -import { assertFilesContent, createIntegrationTest } from '../../testing-utils' +import { assertFilesContent, createJob } from '../../testing-utils' describe('integration pkg-exports-ts-rsc', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should generate proper assets for rsc condition with ts', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - await assertFilesContent(distDir, { - './index.mjs': /const shared = true/, - './react-server.mjs': /'react-server'/, - './react-native.js': /'react-native'/, - './index.d.ts': /declare const shared = true/, - './api.mjs': `'./index.mjs'`, - './api.react-server.mjs': (content) => { - return ( - content.includes('api:react-server') && - content.includes('./index.mjs') - ) - }, - }) + await assertFilesContent(distDir, { + './index.mjs': /const shared = true/, + './react-server.mjs': /'react-server'/, + './react-native.js': /'react-native'/, + './index.d.ts': /declare const shared = true/, + './api.mjs': `'./index.mjs'`, + './api.react-server.mjs': (content) => { + return ( + content.includes('api:react-server') && + content.includes('./index.mjs') + ) }, - ) + }) }) }) diff --git a/test/integration/prepare-ts-with-pkg-json/index.test.ts b/test/integration/prepare-ts-with-pkg-json/index.test.ts index 885bf98e..f828f1c3 100644 --- a/test/integration/prepare-ts-with-pkg-json/index.test.ts +++ b/test/integration/prepare-ts-with-pkg-json/index.test.ts @@ -1,10 +1,6 @@ import fsp from 'fs/promises' import { join } from 'path' -import { - assertContainFiles, - createIntegrationTest, - deleteFile, -} from '../../testing-utils' +import { assertContainFiles, createJob, deleteFile } from '../../testing-utils' describe('integration prepare-ts-with-pkg-json', () => { const dir = __dirname @@ -15,22 +11,20 @@ describe('integration prepare-ts-with-pkg-json', () => { ) await deleteFile(join(dir, './tsconfig.json')) }) + + createJob({ + directory: __dirname, + args: ['prepare'], + }) + it('should contain files', async () => { - await createIntegrationTest( - { - args: ['prepare'], - directory: __dirname, - }, - async ({ dir }) => { - assertContainFiles(dir, ['package.json']) - const pkgJson = JSON.parse( - await fsp.readFile(join(dir, './package.json'), 'utf-8'), - ) - expect(pkgJson.files).toContain('dist') - expect(pkgJson.type).toBeUndefined() - expect(pkgJson.main).toBe('./dist/cjs/index.js') - expect(pkgJson.module).toBe('./dist/es/index.mjs') - }, + assertContainFiles(dir, ['package.json']) + const pkgJson = JSON.parse( + await fsp.readFile(join(dir, './package.json'), 'utf-8'), ) + expect(pkgJson.files).toContain('dist') + expect(pkgJson.type).toBeUndefined() + expect(pkgJson.main).toBe('./dist/cjs/index.js') + expect(pkgJson.module).toBe('./dist/es/index.mjs') }) }) diff --git a/test/integration/prepare-ts-with-test-file/index.test.ts b/test/integration/prepare-ts-with-test-file/index.test.ts index 19841c46..80810fa0 100644 --- a/test/integration/prepare-ts-with-test-file/index.test.ts +++ b/test/integration/prepare-ts-with-test-file/index.test.ts @@ -1,10 +1,6 @@ import fsp from 'fs/promises' import { join } from 'path' -import { - assertContainFiles, - createIntegrationTest, - deleteFile, -} from '../../testing-utils' +import { assertContainFiles, createJob, deleteFile } from '../../testing-utils' describe('integration prepare-ts-with-test-file', () => { const dir = __dirname @@ -12,23 +8,19 @@ describe('integration prepare-ts-with-test-file', () => { await deleteFile(join(dir, './package.json')) await deleteFile(join(dir, './tsconfig.json')) }) + createJob({ + args: ['prepare'], + directory: __dirname, + }) it('should contain files', async () => { - await createIntegrationTest( - { - args: ['prepare'], - directory: __dirname, - }, - async ({ dir }) => { - assertContainFiles(dir, ['package.json']) - const pkgJson = JSON.parse( - await fsp.readFile(join(dir, './package.json'), 'utf-8'), - ) - expect(pkgJson.files).toContain('dist') - expect(pkgJson.main).toBe('./dist/es/index.js') - expect(pkgJson.module).toBe('./dist/es/index.js') - expect(Object.keys(pkgJson.exports)).toEqual(['.']) - expect(Object.keys(pkgJson.exports['.'])).not.toContain('./test') - }, + assertContainFiles(dir, ['package.json']) + const pkgJson = JSON.parse( + await fsp.readFile(join(dir, './package.json'), 'utf-8'), ) + expect(pkgJson.files).toContain('dist') + expect(pkgJson.main).toBe('./dist/es/index.js') + expect(pkgJson.module).toBe('./dist/es/index.js') + expect(Object.keys(pkgJson.exports)).toEqual(['.']) + expect(Object.keys(pkgJson.exports['.'])).not.toContain('./test') }) }) diff --git a/test/integration/raw-data/index.test.ts b/test/integration/raw-data/index.test.ts index 79f83efa..0e82e75b 100644 --- a/test/integration/raw-data/index.test.ts +++ b/test/integration/raw-data/index.test.ts @@ -1,20 +1,15 @@ import { existsSync } from 'fs' -import { assertFilesContent, createIntegrationTest } from '../../testing-utils' +import { assertFilesContent, createJob } from '../../testing-utils' import { join } from 'path' describe('integration - raw-data', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should generate proper assets', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const distFile = join(distDir, 'index.js') - expect(existsSync(distFile)).toBe(true) - await assertFilesContent(distDir, { - 'index.js': '"thisismydata"', - }) - }, - ) + const distFile = join(distDir, 'index.js') + expect(existsSync(distFile)).toBe(true) + await assertFilesContent(distDir, { + 'index.js': '"thisismydata"', + }) }) }) diff --git a/test/integration/relative-entry/relative-entry.test.ts b/test/integration/relative-entry/relative-entry.test.ts index 79980aea..085c1210 100644 --- a/test/integration/relative-entry/relative-entry.test.ts +++ b/test/integration/relative-entry/relative-entry.test.ts @@ -1,16 +1,11 @@ -import { assertFilesContent, createIntegrationTest } from '../../testing-utils' +import { assertFilesContent, createJob } from '../../testing-utils' describe('integration relative-entry', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should generate proper assets for each exports', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - await assertFilesContent(distDir, { - 'index.js': `from './relative.js'`, - }) - }, - ) + await assertFilesContent(distDir, { + 'index.js': `from './relative.js'`, + }) }) }) diff --git a/test/integration/server-components-same-layer/index.test.ts b/test/integration/server-components-same-layer/index.test.ts index 8574785a..bdc20cc9 100644 --- a/test/integration/server-components-same-layer/index.test.ts +++ b/test/integration/server-components-same-layer/index.test.ts @@ -1,29 +1,21 @@ import { promises as fsp } from 'fs' import { join } from 'path' -import { createIntegrationTest } from '../../testing-utils' +import { createJob } from '../../testing-utils' describe('integration server-components-same-layer', () => { - it('should generate proper assets for each exports', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ dir }) => { - const distFiles = await fsp.readdir(join(dir, 'dist')) - const clientChunkFiles = distFiles.filter((f) => - f.includes('client-client-'), - ) - expect(clientChunkFiles.length).toBe(0) + const { distDir } = createJob({ directory: __dirname }) - // index doesn't have "use client" directive - const indexCjs = await fsp.readFile( - join(dir, 'dist/index.cjs'), - 'utf-8', - ) - const indexEsm = await fsp.readFile(join(dir, 'dist/index.js'), 'utf-8') - expect(indexCjs).toContain('use client') - expect(indexEsm).toContain('use client') - }, + it('should generate proper assets for each exports', async () => { + const distFiles = await fsp.readdir(distDir) + const clientChunkFiles = distFiles.filter((f) => + f.includes('client-client-'), ) + expect(clientChunkFiles.length).toBe(0) + + // index doesn't have "use client" directive + const indexCjs = await fsp.readFile(join(distDir, 'index.cjs'), 'utf-8') + const indexEsm = await fsp.readFile(join(distDir, 'index.js'), 'utf-8') + expect(indexCjs).toContain('use client') + expect(indexEsm).toContain('use client') }) }) diff --git a/test/integration/server-components/index.test.ts b/test/integration/server-components/index.test.ts index b8d4f578..23b60b86 100644 --- a/test/integration/server-components/index.test.ts +++ b/test/integration/server-components/index.test.ts @@ -1,28 +1,20 @@ -import { - createIntegrationTest, - getFileNamesFromDirectory, -} from '../../testing-utils' +import { createJob, getFileNamesFromDirectory } from '../../testing-utils' describe('integration server-components', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should generate proper assets for each exports', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const jsFiles = await getFileNamesFromDirectory(distDir) + const jsFiles = await getFileNamesFromDirectory(distDir) - expect(jsFiles).toEqual([ - 'index.cjs', - 'index.js', - 'mod_actions-server-B2kXJwqw.cjs', - 'mod_actions-server-DSdgX-jM.js', - 'mod_client-client-BO96FYFA.js', - 'mod_client-client-DAeHkA4H.cjs', - 'ui.cjs', - 'ui.js', - ]) - }, - ) + expect(jsFiles).toEqual([ + 'index.cjs', + 'index.js', + 'mod_actions-server-B2kXJwqw.cjs', + 'mod_actions-server-DSdgX-jM.js', + 'mod_client-client-BO96FYFA.js', + 'mod_client-client-DAeHkA4H.cjs', + 'ui.cjs', + 'ui.js', + ]) }) }) diff --git a/test/integration/shared-any-module/index.test.ts b/test/integration/shared-any-module/index.test.ts index 910636b1..57c80fd1 100644 --- a/test/integration/shared-any-module/index.test.ts +++ b/test/integration/shared-any-module/index.test.ts @@ -1,38 +1,33 @@ import { assertFilesContent, - createIntegrationTest, + createJob, getFileNamesFromDirectory, } from '../../testing-utils' describe('integration shared-module', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should split all shared module into different chunks', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir, stdout }) => { - const jsFiles = await getFileNamesFromDirectory(distDir) - expect(jsFiles).toEqual([ - '_internal/util-a.cjs', - '_internal/util-a.d.ts', - '_internal/util-a.js', - '_internal/util-b.cjs', - '_internal/util-b.d.ts', - '_internal/util-b.js', - 'export-a.js', - 'export-b.js', - 'export-c.js', - 'private/_nested/util-c.cjs', - 'private/_nested/util-c.d.ts', - 'private/_nested/util-c.js', - ]) + const jsFiles = await getFileNamesFromDirectory(distDir) + expect(jsFiles).toEqual([ + '_internal/util-a.cjs', + '_internal/util-a.d.ts', + '_internal/util-a.js', + '_internal/util-b.cjs', + '_internal/util-b.d.ts', + '_internal/util-b.js', + 'export-a.js', + 'export-b.js', + 'export-c.js', + 'private/_nested/util-c.cjs', + 'private/_nested/util-c.d.ts', + 'private/_nested/util-c.js', + ]) - await assertFilesContent(distDir, { - 'export-a.js': `'./_internal/util-a.js'`, - 'export-b.js': `'./_internal/util-b.js'`, - 'export-c.js': `'./private/_nested/util-c.js'`, - }) - }, - ) + await assertFilesContent(distDir, { + 'export-a.js': `'./_internal/util-a.js'`, + 'export-b.js': `'./_internal/util-b.js'`, + 'export-c.js': `'./private/_nested/util-c.js'`, + }) }) }) diff --git a/test/integration/shared-entry/index.test.ts b/test/integration/shared-entry/index.test.ts index 2a9e7bed..dfcf2947 100644 --- a/test/integration/shared-entry/index.test.ts +++ b/test/integration/shared-entry/index.test.ts @@ -1,55 +1,41 @@ import { promises as fsp } from 'fs' import { join } from 'path' -import { assertContainFiles, createIntegrationTest } from '../../testing-utils' +import { assertContainFiles, createJob } from '../../testing-utils' describe('integration shared-entry', () => { - it('should split shared module into one chunk layer', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ dir }) => { - const distFiles = [ - './dist/index.js', - './dist/index.mjs', - './dist/shared.js', - './dist/shared.mjs', - ] - assertContainFiles(dir, distFiles) + const { dir } = createJob({ directory: __dirname }) - // ESM bundle imports from - const indexEsm = await fsp.readFile( - join(dir, './dist/index.mjs'), - 'utf-8', - ) - expect(indexEsm).toContain(`'./shared.mjs'`) - expect(indexEsm).toContain('index-export') - expect(indexEsm).not.toMatch(/['"]\.\/shared['"]/) - expect(indexEsm).not.toContain('shared-export') + it('should split shared module into one chunk layer', async () => { + const distFiles = [ + './dist/index.js', + './dist/index.mjs', + './dist/shared.js', + './dist/shared.mjs', + ] + assertContainFiles(dir, distFiles) - // CJS bundle imports from - const indexCjs = await fsp.readFile( - join(dir, './dist/index.js'), - 'utf-8', - ) - expect(indexCjs).toContain(`require('./shared.js')`) - expect(indexCjs).toContain('index-export') - expect(indexCjs).not.toMatch(/['"]\.\/shared['"]/) + // ESM bundle imports from + const indexEsm = await fsp.readFile(join(dir, './dist/index.mjs'), 'utf-8') + expect(indexEsm).toContain(`'./shared.mjs'`) + expect(indexEsm).toContain('index-export') + expect(indexEsm).not.toMatch(/['"]\.\/shared['"]/) + expect(indexEsm).not.toContain('shared-export') - // shared entry contains its own content - const sharedEsm = await fsp.readFile( - join(dir, './dist/shared.mjs'), - 'utf-8', - ) - expect(sharedEsm).toContain('shared-export') + // CJS bundle imports from + const indexCjs = await fsp.readFile(join(dir, './dist/index.js'), 'utf-8') + expect(indexCjs).toContain(`require('./shared.js')`) + expect(indexCjs).toContain('index-export') + expect(indexCjs).not.toMatch(/['"]\.\/shared['"]/) - // shared entry contains its own content - const sharedCjs = await fsp.readFile( - join(dir, './dist/shared.js'), - 'utf-8', - ) - expect(sharedCjs).toContain('shared-export') - }, + // shared entry contains its own content + const sharedEsm = await fsp.readFile( + join(dir, './dist/shared.mjs'), + 'utf-8', ) + expect(sharedEsm).toContain('shared-export') + + // shared entry contains its own content + const sharedCjs = await fsp.readFile(join(dir, './dist/shared.js'), 'utf-8') + expect(sharedCjs).toContain('shared-export') }) }) diff --git a/test/integration/shared-module-ts-esm/index.test.ts b/test/integration/shared-module-ts-esm/index.test.ts index 0938cab4..252a6006 100644 --- a/test/integration/shared-module-ts-esm/index.test.ts +++ b/test/integration/shared-module-ts-esm/index.test.ts @@ -1,25 +1,17 @@ -import { - createIntegrationTest, - getFileNamesFromDirectory, -} from '../../testing-utils' +import { createJob, getFileNamesFromDirectory } from '../../testing-utils' describe('integration shared-module-ts-esm', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should contain correct type file path of shared chunks', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const files = await getFileNamesFromDirectory(distDir) - expect(files).toEqual([ - '_util.d.ts', - '_util.js', - '_util.mjs', - 'cjs/index.d.ts', - 'cjs/index.js', - 'es/index.mjs', - ]) - }, - ) + const files = await getFileNamesFromDirectory(distDir) + expect(files).toEqual([ + '_util.d.ts', + '_util.js', + '_util.mjs', + 'cjs/index.d.ts', + 'cjs/index.js', + 'es/index.mjs', + ]) }) }) diff --git a/test/integration/shared-module-ts/index.test.ts b/test/integration/shared-module-ts/index.test.ts index 153578aa..c80dc78d 100644 --- a/test/integration/shared-module-ts/index.test.ts +++ b/test/integration/shared-module-ts/index.test.ts @@ -1,32 +1,24 @@ -import { - createIntegrationTest, - getFileNamesFromDirectory, -} from '../../testing-utils' +import { createJob, getFileNamesFromDirectory } from '../../testing-utils' describe('integration shared-module-ts', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should contain correct type file path of shared chunks', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const jsFiles = await getFileNamesFromDirectory(distDir) - expect(jsFiles).toEqual([ - 'another.cjs', - 'another.d.ts', - 'another.js', - 'index.cjs', - 'index.d.ts', - 'index.js', - 'index.react-server.js', - 'lib/_app-context.cjs', - 'lib/_app-context.d.ts', - 'lib/_app-context.js', - 'lib/_util.cjs', - 'lib/_util.d.ts', - 'lib/_util.js', - ]) - }, - ) + const jsFiles = await getFileNamesFromDirectory(distDir) + expect(jsFiles).toEqual([ + 'another.cjs', + 'another.d.ts', + 'another.js', + 'index.cjs', + 'index.d.ts', + 'index.js', + 'index.react-server.js', + 'lib/_app-context.cjs', + 'lib/_app-context.d.ts', + 'lib/_app-context.js', + 'lib/_util.cjs', + 'lib/_util.d.ts', + 'lib/_util.js', + ]) }) }) diff --git a/test/integration/shared-module-with-suffix/shared-module-with-suffix.test.ts b/test/integration/shared-module-with-suffix/shared-module-with-suffix.test.ts index 941ee31d..d4ff6ed2 100644 --- a/test/integration/shared-module-with-suffix/shared-module-with-suffix.test.ts +++ b/test/integration/shared-module-with-suffix/shared-module-with-suffix.test.ts @@ -1,16 +1,12 @@ -import { assertFilesContent, createIntegrationTest } from '../../testing-utils' +import { assertFilesContent, createJob } from '../../testing-utils' describe('integration - shared-module-with-suffix', () => { + const { distDir } = createJob({ + directory: __dirname, + }) it('should alias correctly for the shared module with special suffix', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - assertFilesContent(distDir, { - 'client.mjs': `./_private/util.browser.mjs`, - }) - }, - ) + await assertFilesContent(distDir, { + 'client.mjs': `./_private/util.browser.mjs`, + }) }) }) diff --git a/test/integration/shared-module/index.test.ts b/test/integration/shared-module/index.test.ts index 578e1df4..d6e6c0f9 100644 --- a/test/integration/shared-module/index.test.ts +++ b/test/integration/shared-module/index.test.ts @@ -1,44 +1,39 @@ import { assertFilesContent, - createIntegrationTest, + createJob, getFileNamesFromDirectory, } from '../../testing-utils' describe('integration shared-module', () => { + const { distDir, job } = createJob({ directory: __dirname }) + it('should split shared module into one chunk layer', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir, stdout }) => { - const jsFiles = await getFileNamesFromDirectory(distDir) - expect(jsFiles).toEqual([ - '_internal/index.cjs', - '_internal/index.js', - '_internal/index.react-server.cjs', - '_internal/index.react-server.js', - 'another.cjs', - 'another.js', - 'client.cjs', - 'client.js', - 'index.cjs', - 'index.js', - 'index.react-server.js', - 'lib/_app-context.cjs', - 'lib/_app-context.js', - 'lib/_util.cjs', - 'lib/_util.js', - ]) + const jsFiles = await getFileNamesFromDirectory(distDir) + expect(jsFiles).toEqual([ + '_internal/index.cjs', + '_internal/index.js', + '_internal/index.react-server.cjs', + '_internal/index.react-server.js', + 'another.cjs', + 'another.js', + 'client.cjs', + 'client.js', + 'index.cjs', + 'index.js', + 'index.react-server.js', + 'lib/_app-context.cjs', + 'lib/_app-context.js', + 'lib/_util.cjs', + 'lib/_util.js', + ]) - // In index.react-server.js, it should refers to _internal/index.react-server.js - await assertFilesContent(distDir, { - 'index.react-server.js': `'./_internal/index.react-server.js'`, - './_internal/index.react-server.js': 'internal:react-server', - }) + // In index.react-server.js, it should refers to _internal/index.react-server.js + await assertFilesContent(distDir, { + 'index.react-server.js': `'./_internal/index.react-server.js'`, + './_internal/index.react-server.js': 'internal:react-server', + }) - // Hide private shared module - expect(stdout).not.toContain('./lib/_util') - }, - ) + // Hide private shared module + expect(job.stdout).not.toContain('./lib/_util') }) }) diff --git a/test/integration/stage3-decorator/index.test.ts b/test/integration/stage3-decorator/index.test.ts index 24917757..af63f959 100644 --- a/test/integration/stage3-decorator/index.test.ts +++ b/test/integration/stage3-decorator/index.test.ts @@ -1,28 +1,22 @@ import { assertContainFiles, stripANSIColor } from '../../testing-utils' -import { createIntegrationTest } from '../../testing-utils' -import * as console from 'node:console' +import { createJob } from '../../testing-utils' describe('integration stage3-decorator', () => { + const { distDir, job } = createJob({ directory: __dirname }) + it('should build success when enable decorator', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir, stdout, stderr }) => { - console.log(distDir, stdout, stderr) - const distFiles = ['index.js', 'index.d.ts'] + const { stdout } = job + const distFiles = ['index.js', 'index.d.ts'] - await assertContainFiles(distDir, distFiles) + await assertContainFiles(distDir, distFiles) - const log = `\ - dist/index.d.ts - dist/index.js` + const log = `\ + dist/index.d.ts + dist/index.js` - const rawStdout = stripANSIColor(stdout) - log.split('\n').forEach((line: string) => { - expect(rawStdout).toContain(line.trim()) - }) - }, - ) + const rawStdout = stripANSIColor(stdout) + log.split('\n').forEach((line: string) => { + expect(rawStdout).toContain(line.trim()) + }) }) }) diff --git a/test/integration/subpath-imports/index.test.ts b/test/integration/subpath-imports/index.test.ts index f9a156bc..d784841f 100644 --- a/test/integration/subpath-imports/index.test.ts +++ b/test/integration/subpath-imports/index.test.ts @@ -1,16 +1,12 @@ -import { assertFilesContent, createIntegrationTest } from '../../testing-utils' +import { assertFilesContent, createJob } from '../../testing-utils' describe('integration shared-module', () => { + const { distDir } = createJob({ + directory: __dirname, + }) it('should split shared module into one chunk layer', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - assertFilesContent(distDir, { - 'index.js': `const dep = 'polyfill-dep'`, - }) - }, - ) + await assertFilesContent(distDir, { + 'index.js': `const dep = 'polyfill-dep'`, + }) }) }) diff --git a/test/integration/ts-allow-js/index.test.ts b/test/integration/ts-allow-js/index.test.ts index 835f0bd8..0a2e84ae 100644 --- a/test/integration/ts-allow-js/index.test.ts +++ b/test/integration/ts-allow-js/index.test.ts @@ -1,22 +1,17 @@ import { assertContainFiles, assertFilesContent, - createIntegrationTest, + createJob, } from '../../testing-utils' describe('integration - ts-allow-js', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should generate proper assets', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const distFiles = ['index.js', 'index.d.ts'] - await assertContainFiles(distDir, distFiles) - await assertFilesContent(distDir, { - 'index.d.ts': 'declare function _default(): string;', - }) - }, - ) + const distFiles = ['index.js', 'index.d.ts'] + await assertContainFiles(distDir, distFiles) + await assertFilesContent(distDir, { + 'index.d.ts': 'declare function _default(): string;', + }) }) }) diff --git a/test/integration/ts-dual-package-module/index.test.ts b/test/integration/ts-dual-package-module/index.test.ts index 7ac73922..325bc5f4 100644 --- a/test/integration/ts-dual-package-module/index.test.ts +++ b/test/integration/ts-dual-package-module/index.test.ts @@ -1,15 +1,10 @@ -import { createIntegrationTest, assertContainFiles } from '../../testing-utils' +import { createJob, assertContainFiles } from '../../testing-utils' describe('integration ts-dual-package-module', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should ensure generated assets', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const distFiles = ['index.js', 'index.cjs'] - assertContainFiles(distDir, distFiles) - }, - ) + const distFiles = ['index.js', 'index.cjs'] + assertContainFiles(distDir, distFiles) }) }) diff --git a/test/integration/ts-dual-package-type-cjs/index.test.ts b/test/integration/ts-dual-package-type-cjs/index.test.ts index ed014a00..09367af8 100644 --- a/test/integration/ts-dual-package-type-cjs/index.test.ts +++ b/test/integration/ts-dual-package-type-cjs/index.test.ts @@ -1,14 +1,9 @@ -import { createIntegrationTest, assertContainFiles } from '../../testing-utils' +import { createJob, assertContainFiles } from '../../testing-utils' describe('integration ts-dual-package-type-cjs', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should ensure generated assets', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - await assertContainFiles(distDir, ['index.js', 'index.mjs']) - }, - ) + await assertContainFiles(distDir, ['index.js', 'index.mjs']) }) }) diff --git a/test/integration/ts-error/index.test.ts b/test/integration/ts-error/index.test.ts index f5be5fc1..53fde2da 100644 --- a/test/integration/ts-error/index.test.ts +++ b/test/integration/ts-error/index.test.ts @@ -1,18 +1,14 @@ import { join } from 'path' import { existsSync } from 'fs' -import { createIntegrationTest } from '../../testing-utils' +import { createJob } from '../../testing-utils' describe('integration ts-error', () => { + const { distDir, job } = createJob({ directory: __dirname }) + it('should error when ts is not properly resolved', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ stderr, distDir }) => { - const distFile = join(distDir, './index.js') - expect(existsSync(distFile)).toBe(false) - expect(stderr).toMatch(/Could not load TypeScript compiler/) - }, - ) + const { stderr } = job + const distFile = join(distDir, './index.js') + expect(existsSync(distFile)).toBe(false) + expect(stderr).toMatch(/Could not load TypeScript compiler/) }) }) diff --git a/test/integration/ts-exports-multiple-conditions/index.test.ts b/test/integration/ts-exports-multiple-conditions/index.test.ts index 860c7646..37ada7f3 100644 --- a/test/integration/ts-exports-multiple-conditions/index.test.ts +++ b/test/integration/ts-exports-multiple-conditions/index.test.ts @@ -1,42 +1,35 @@ import { assertContainFiles, - createIntegrationTest, + createJob, assertFilesContent, } from '../../testing-utils' describe('integration - ts-exports-multiple-conditions', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should generate proper assets', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const distFiles = [ - // entry files - 'index.js', - 'index.cjs', - 'index.browser.js', - 'index.workerd.js', - 'index.edge-light.js', - // types - 'index.d.cts', - 'index.d.ts', - 'index.browser.d.ts', - 'index.workerd.d.ts', - 'index.edge-light.d.ts', - ] - assertContainFiles(distDir, distFiles) - await assertFilesContent(distDir, { - 'index.js': (code) => code.includes("const runtime = 'node'"), - 'index.cjs': (code) => code.includes("const runtime = 'node'"), - 'index.browser.js': (code) => - code.includes("const runtime = 'browser'"), - 'index.workerd.js': (code) => - code.includes("const runtime = 'workerd'"), - 'index.edge-light.js': (code) => - code.includes("const runtime = 'edge-light'"), - }) - }, - ) + const distFiles = [ + // entry files + 'index.js', + 'index.cjs', + 'index.browser.js', + 'index.workerd.js', + 'index.edge-light.js', + // types + 'index.d.cts', + 'index.d.ts', + 'index.browser.d.ts', + 'index.workerd.d.ts', + 'index.edge-light.d.ts', + ] + assertContainFiles(distDir, distFiles) + await assertFilesContent(distDir, { + 'index.js': (code) => code.includes("const runtime = 'node'"), + 'index.cjs': (code) => code.includes("const runtime = 'node'"), + 'index.browser.js': (code) => code.includes("const runtime = 'browser'"), + 'index.workerd.js': (code) => code.includes("const runtime = 'workerd'"), + 'index.edge-light.js': (code) => + code.includes("const runtime = 'edge-light'"), + }) }) }) diff --git a/test/integration/ts-exports-types/index.test.ts b/test/integration/ts-exports-types/index.test.ts index 61216a28..a9811bd8 100644 --- a/test/integration/ts-exports-types/index.test.ts +++ b/test/integration/ts-exports-types/index.test.ts @@ -1,15 +1,10 @@ -import { assertContainFiles, createIntegrationTest } from '../../testing-utils' +import { assertContainFiles, createJob } from '../../testing-utils' describe('integration - ts-exports-types', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should generate proper assets', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const distFiles = ['index.mjs', 'index.cjs', 'index.d.ts'] - await assertContainFiles(distDir, distFiles) - }, - ) + const distFiles = ['index.mjs', 'index.cjs', 'index.d.ts'] + await assertContainFiles(distDir, distFiles) }) }) diff --git a/test/integration/ts-import-json-exports-condition/index.test.ts b/test/integration/ts-import-json-exports-condition/index.test.ts index 6f2a3331..f3d97437 100644 --- a/test/integration/ts-import-json-exports-condition/index.test.ts +++ b/test/integration/ts-import-json-exports-condition/index.test.ts @@ -1,17 +1,13 @@ -import { assertFilesContent, createIntegrationTest } from '../../testing-utils' +import { assertFilesContent, createJob } from '../../testing-utils' describe('integration - ts-import-json-exports-condition', () => { + const { distDir } = createJob({ + directory: __dirname, + }) it('should output correct bundles and types import json with export condition', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - assertFilesContent(distDir, { - 'index.js': '0.0.1', - 'index.d.ts': 'declare const version: string', - }) - }, - ) + await assertFilesContent(distDir, { + 'index.js': '0.0.1', + 'index.d.ts': 'declare const version: string', + }) }) }) diff --git a/test/integration/ts-import-json-exports-string/index.test.ts b/test/integration/ts-import-json-exports-string/index.test.ts index 6f2a3331..f3d97437 100644 --- a/test/integration/ts-import-json-exports-string/index.test.ts +++ b/test/integration/ts-import-json-exports-string/index.test.ts @@ -1,17 +1,13 @@ -import { assertFilesContent, createIntegrationTest } from '../../testing-utils' +import { assertFilesContent, createJob } from '../../testing-utils' describe('integration - ts-import-json-exports-condition', () => { + const { distDir } = createJob({ + directory: __dirname, + }) it('should output correct bundles and types import json with export condition', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - assertFilesContent(distDir, { - 'index.js': '0.0.1', - 'index.d.ts': 'declare const version: string', - }) - }, - ) + await assertFilesContent(distDir, { + 'index.js': '0.0.1', + 'index.d.ts': 'declare const version: string', + }) }) }) diff --git a/test/integration/ts-incremental-with-buildinfofile/index.test.ts b/test/integration/ts-incremental-with-buildinfofile/index.test.ts index 4189e5c3..a2c48cb0 100644 --- a/test/integration/ts-incremental-with-buildinfofile/index.test.ts +++ b/test/integration/ts-incremental-with-buildinfofile/index.test.ts @@ -3,24 +3,20 @@ import { join } from 'path' import { assertContainFiles, assertFilesContent, - createIntegrationTest, + createJob, } from '../../testing-utils' describe('integration - ts-incremental-with-buildinfofile', () => { + const { distDir } = createJob({ + directory: __dirname, + }) it('should generate proper assets', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const distFiles = ['index.js', 'index.d.ts'] - await assertContainFiles(distDir, distFiles) - await assertFilesContent(distDir, { - 'index.d.ts': 'declare const _default: () => string;', - }) + const distFiles = ['index.js', 'index.d.ts'] + await assertContainFiles(distDir, distFiles) + await assertFilesContent(distDir, { + 'index.d.ts': 'declare const _default: () => string;', + }) - expect(existsSync(join(distDir, '.tsbuildinfo'))).toBe(false) - }, - ) + expect(existsSync(join(distDir, '.tsbuildinfo'))).toBe(false) }) }) diff --git a/test/integration/ts-incremental/index.test.ts b/test/integration/ts-incremental/index.test.ts index a4b7ab53..7b79f7d0 100644 --- a/test/integration/ts-incremental/index.test.ts +++ b/test/integration/ts-incremental/index.test.ts @@ -2,26 +2,22 @@ import { existsSync } from 'fs' import { assertContainFiles, assertFilesContent, - createIntegrationTest, + createJob, } from '../../testing-utils' import { join } from 'path' describe('integration - ts-incremental', () => { + const { distDir } = createJob({ + directory: __dirname, + }) it('should generate proper assets', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - const distFiles = ['index.js', 'index.d.ts'] + const distFiles = ['index.js', 'index.d.ts'] - await assertContainFiles(distDir, distFiles) - await assertFilesContent(distDir, { - 'index.d.ts': 'declare const _default: () => string;', - }) + await assertContainFiles(distDir, distFiles) + await assertFilesContent(distDir, { + 'index.d.ts': 'declare const _default: () => string;', + }) - expect(existsSync(join(distDir, '.tsbuildinfo'))).toBe(false) - }, - ) + expect(existsSync(join(distDir, '.tsbuildinfo'))).toBe(false) }) }) diff --git a/test/integration/ts-no-emit/index.test.ts b/test/integration/ts-no-emit/index.test.ts index afd4ce35..bfe894d2 100644 --- a/test/integration/ts-no-emit/index.test.ts +++ b/test/integration/ts-no-emit/index.test.ts @@ -1,24 +1,19 @@ import { assertContainFiles, assertFilesContent, - createIntegrationTest, + createJob, } from '../../testing-utils' describe('integration ts-no-emit', () => { + const { distDir } = createJob({ directory: __dirname }) + it('should succeed the build', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ distDir }) => { - // should still emit declaration files - const distFiles = ['index.js', 'index.d.ts'] + // should still emit declaration files + const distFiles = ['index.js', 'index.d.ts'] - await assertContainFiles(distDir, distFiles) - await assertFilesContent(distDir, { - 'index.d.ts': 'declare const _default: () => string;', - }) - }, - ) + await assertContainFiles(distDir, distFiles) + await assertFilesContent(distDir, { + 'index.d.ts': 'declare const _default: () => string;', + }) }) }) diff --git a/test/integration/tsconfig-override/.gitignore b/test/integration/tsconfig-override/.gitignore new file mode 100644 index 00000000..b66bc617 --- /dev/null +++ b/test/integration/tsconfig-override/.gitignore @@ -0,0 +1 @@ +!tsconfig.json \ No newline at end of file diff --git a/test/integration/tsconfig-override/index.test.ts b/test/integration/tsconfig-override/index.test.ts index 5da534e2..102fdc85 100644 --- a/test/integration/tsconfig-override/index.test.ts +++ b/test/integration/tsconfig-override/index.test.ts @@ -1,37 +1,29 @@ -import { assertFilesContent, createIntegrationTest } from '../../testing-utils' +import { assertFilesContent, createJob } from '../../testing-utils' -describe('integration tsconfig-override', () => { +describe('integration - tsconfig-override - default', () => { + const { distDir } = createJob({ + directory: __dirname, + }) it('should use es5 output in build without override', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ dir }) => { - assertFilesContent(dir, { - ['./dist/index.js']: (content) => { - return ( - content.includes('function A') && !content.includes('class A') - ) - }, - }) + await assertFilesContent(distDir, { + ['index.js']: (content) => { + return content.includes('function A') && !content.includes('class A') }, - ) + }) }) +}) + +describe('integration - tsconfig-override - customized', () => { + const { distDir } = createJob({ + directory: __dirname, + args: ['--tsconfig', 'tsconfig.override.json'], + }) + it('should use es8 output in build', async () => { - await createIntegrationTest( - { - directory: __dirname, - args: ['--tsconfig', 'tsconfig.build.json'], - }, - async ({ dir }) => { - assertFilesContent(dir, { - ['./dist/index.js']: (content) => { - return ( - content.includes('class A') && !content.includes('function A') - ) - }, - }) + await assertFilesContent(distDir, { + ['index.js']: (content) => { + return content.includes('class A') && !content.includes('function A') }, - ) + }) }) }) diff --git a/test/integration/unspecified-types-paths/index.test.ts b/test/integration/unspecified-types-paths/index.test.ts index 1a7c9bf9..7fc793e9 100644 --- a/test/integration/unspecified-types-paths/index.test.ts +++ b/test/integration/unspecified-types-paths/index.test.ts @@ -1,26 +1,22 @@ import { assertFilesContent, - createIntegrationTest, + createJob, getFileNamesFromDirectory, } from '../../testing-utils' describe('integration - tsconfig-override', () => { + const { dir } = createJob({ + directory: __dirname, + }) it('should not generate js types paths if not specified', async () => { - await createIntegrationTest( - { - directory: __dirname, - }, - async ({ dir }) => { - assertFilesContent(dir, { - './dist/subpath/nested.js': 'subpath/nested', - './dist/subpath/nested.cjs': 'subpath/nested', - }) - // No types files should be generated - expect(await getFileNamesFromDirectory(dir)).toEqual([ - 'dist/subpath/nested.cjs', - 'dist/subpath/nested.js', - ]) - }, - ) + await assertFilesContent(dir, { + './dist/subpath/nested.js': 'subpath/nested', + './dist/subpath/nested.cjs': 'subpath/nested', + }) + // No types files should be generated + expect(await getFileNamesFromDirectory(dir)).toEqual([ + 'dist/subpath/nested.cjs', + 'dist/subpath/nested.js', + ]) }) }) diff --git a/test/testing-utils/cli.ts b/test/testing-utils/cli.ts index 48875571..af866f89 100644 --- a/test/testing-utils/cli.ts +++ b/test/testing-utils/cli.ts @@ -1,6 +1,10 @@ -import { createTest, executeBunchee, type ExcuteBuncheeResult } from './shared' +import { + createAsyncTest, + executeBunchee, + type ExcuteBuncheeResult, +} from './shared' -export async function createCliJob({ +export async function runCli({ args, options, abortTimeout, @@ -11,7 +15,7 @@ export async function createCliJob({ abortTimeout?: number directory: string }) { - return await createTest({ + return await createAsyncTest({ directory, args: args ?? [], options: options ?? {}, diff --git a/test/testing-utils/helpers.ts b/test/testing-utils/helpers.ts index 0c97e5f3..e0e19947 100644 --- a/test/testing-utils/helpers.ts +++ b/test/testing-utils/helpers.ts @@ -3,6 +3,7 @@ import fsp from 'fs/promises' import path from 'path' import * as debug from './debug' +// TODO: remove distFIle, since it's only available in runCli export type CreateTestResultExtra = { dir: string distDir: string diff --git a/test/testing-utils/index.ts b/test/testing-utils/index.ts index 5b84f8af..2c65d683 100644 --- a/test/testing-utils/index.ts +++ b/test/testing-utils/index.ts @@ -1,108 +1,12 @@ import { glob } from 'glob' -import { - createSyncTest, - createTest, - executeBunchee, - type ExcuteBuncheeResult, -} from './shared' function normalizePath(filePath: string) { return filePath.replace(/\\/g, '/') } export * from './helpers' -type IntegrationTestOptions = { - args?: string[] - options?: { env?: NodeJS.ProcessEnv } - abortTimeout?: number - directory: string -} - -export { createCliJob } from './cli' - -/** - * @deprecated - * Use createJob instead - */ -export async function createIntegrationTest( - { args, options, abortTimeout, directory }: IntegrationTestOptions, - testFn?: Parameters>[1], -) { - return await createTest( - { - args: args ?? [], - options: options ?? {}, - abortTimeout, - directory, - run: executeBunchee, - }, - testFn, - ) -} - -/** Sync testing helper */ -export function createJob({ - args, - options, - abortTimeout, - directory, -}: IntegrationTestOptions) { - return createSyncTest({ - args: args ?? [], - options: options ?? {}, - abortTimeout, - directory, - run: executeBunchee, - }) -} - -export function createIntegration( - integrationTestOptions: IntegrationTestOptions, - testFn?: Parameters>[1], -): { - code: number | null - stdout: string - stderr: string - dir: string - distDir: string - distFile: string -} { - let result: any - beforeAll(async () => { - result = await createIntegrationTest(integrationTestOptions, testFn) - }) - - const proxy = new Proxy( - {}, - { - get(_, key) { - return result?.[key] ?? null - }, - }, - ) as any - - return { - get code() { - return proxy.code ?? null - }, - get stdout() { - return proxy.stdout ?? '' - }, - get stderr() { - return proxy.stderr ?? '' - }, - get dir() { - return integrationTestOptions.directory - }, - get distDir() { - console.log('distDir', proxy.distDir, result) - return proxy.distDir ?? '' - }, - get distFile() { - return proxy.distFile ?? null - }, - } -} +export { runCli } from './cli' +export { createJob } from './integration' export async function getFileNamesFromDirectory(directory: string) { const files = await glob(['**/*.{,c,m}js', '**/*.{,c,m}d.ts'], { diff --git a/test/testing-utils/integration.ts b/test/testing-utils/integration.ts new file mode 100644 index 00000000..3766a121 --- /dev/null +++ b/test/testing-utils/integration.ts @@ -0,0 +1,28 @@ +import { + createSyncTest, + executeBunchee, + type ExcuteBuncheeResult, +} from './shared' + +type IntegrationTestOptions = { + args?: string[] + options?: { env?: NodeJS.ProcessEnv } + abortTimeout?: number + directory: string +} + +/** Sync testing helper */ +export function createJob({ + args, + options, + abortTimeout, + directory, +}: IntegrationTestOptions) { + return createSyncTest({ + args: args ?? [], + options: options ?? {}, + abortTimeout, + directory, + run: executeBunchee, + }) +} diff --git a/test/testing-utils/shared.ts b/test/testing-utils/shared.ts index 73473f02..7df46a06 100644 --- a/test/testing-utils/shared.ts +++ b/test/testing-utils/shared.ts @@ -3,7 +3,7 @@ import { CreateTestResultExtra, removeDirectory } from './helpers' import path from 'path' import * as debug from './debug' -export async function createTest( +export async function createAsyncTest( { args, options, @@ -70,18 +70,13 @@ export function createSyncTest({ options: { env?: NodeJS.ProcessEnv }, processOptions?: { abortTimeout?: number }, ) => Promise -}): CreateTestResultExtra & { job: T } { +}): Omit & { job: T } { const fixturesDir = directory const distDir = path.join(fixturesDir, './dist') - let distFile = '' if (!args.includes('--cwd')) { args.push('--cwd', fixturesDir) } - const outputIndex = args.indexOf('-o') - if (outputIndex !== -1) { - distFile = path.join(fixturesDir, args[outputIndex + 1]) - } let result = undefined const resultProxy = new Proxy( @@ -108,7 +103,6 @@ export function createSyncTest({ return resultProxy }, distDir, - distFile, dir: directory, } }