From 8633425fb5f497ee617e8e8d2a93e150c5c97583 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Sun, 12 Jan 2025 16:23:01 +0100 Subject: [PATCH] test: improve cli testing util --- test/cli/basic/index.test.ts | 118 ++++++++------------ test/cli/cjs-relative-imports/index.test.ts | 26 ++--- test/cli/dts-bundle/index.test.ts | 70 +++++------- test/cli/dts/index.test.ts | 26 ++--- test/cli/env-var/index.test.ts | 30 ++--- test/cli/externals/index.test.ts | 64 +++++------ test/cli/no-dts/index.test.ts | 28 ++--- test/cli/no-entry/index.test.ts | 30 ++--- test/cli/output-in-watch/index.test.ts | 20 ++-- test/cli/target/index.test.ts | 30 ++--- test/cli/workspace/index.test.ts | 24 ++-- test/testing-utils/cli.ts | 40 +++---- test/testing-utils/index.ts | 2 +- 13 files changed, 207 insertions(+), 301 deletions(-) diff --git a/test/cli/basic/index.test.ts b/test/cli/basic/index.test.ts index 29f743f1..e8d5f94c 100644 --- a/test/cli/basic/index.test.ts +++ b/test/cli/basic/index.test.ts @@ -1,87 +1,67 @@ import fs from 'fs' -import { createCliTest } from '../../testing-utils' +import { createCliJob } from '../../testing-utils' describe('cli', () => { it(`cli basic should work properly`, async () => { - await createCliTest( - { - directory: __dirname, - args: ['hello.js', '-o', 'dist/hello.bundle.js'], - }, - ({ code, distFile }) => { - expect(fs.existsSync(distFile)).toBe(true) - expect(code).toBe(0) - }, - ) + const { code, distFile } = await createCliJob({ + directory: __dirname, + args: ['hello.js', '-o', 'dist/hello.bundle.js'], + }) + expect(fs.existsSync(distFile)).toBe(true) + expect(code).toBe(0) }) it(`cli format should work properly`, async () => { - await createCliTest( - { - directory: __dirname, - args: ['hello.js', '-f', 'cjs', '-o', 'dist/hello.cjs'], - }, - ({ code, distFile }) => { - expect(fs.existsSync(distFile)).toBe(true) - expect( - fs.readFileSync(distFile, { encoding: 'utf-8' }).includes('exports.'), - ).toBe(true) - expect(code).toBe(0) - }, - ) + const { code, distFile } = await createCliJob({ + directory: __dirname, + args: ['hello.js', '-f', 'cjs', '-o', 'dist/hello.cjs'], + }) + expect(fs.existsSync(distFile)).toBe(true) + expect( + fs.readFileSync(distFile, { encoding: 'utf-8' }).includes('exports.'), + ).toBe(true) + expect(code).toBe(0) }) it(`cli compress should work properly`, async () => { - await createCliTest( - { - directory: __dirname, - args: ['hello.js', '-m', '-o', 'dist/hello.bundle.min.js'], - }, - ({ code, distFile }) => { - expect(fs.existsSync(distFile)).toBe(true) - expect( - fs.readFileSync(distFile, { encoding: 'utf-8' }).includes('sayHello'), - ).toBe(false) - expect(code).toBe(0) - }, - ) + const { code, distFile } = await createCliJob({ + directory: __dirname, + args: ['hello.js', '-m', '-o', 'dist/hello.bundle.min.js'], + }) + expect(fs.existsSync(distFile)).toBe(true) + expect( + fs.readFileSync(distFile, { encoding: 'utf-8' }).includes('sayHello'), + ).toBe(false) + expect(code).toBe(0) }) it(`cli sourcemap should work properly`, async () => { - await createCliTest( - { - directory: __dirname, - args: ['hello.js', '--sourcemap', '-o', 'dist/hello.js'], - }, - ({ code, distFile }) => { - expect(fs.existsSync(distFile)).toBe(true) - expect( - fs - .readFileSync(distFile, { encoding: 'utf-8' }) - .includes('sourceMappingURL'), - ).toBe(true) - expect(fs.existsSync(distFile + '.map')).toBe(true) - expect(code).toBe(0) - }, - ) + const { code, distFile } = await createCliJob({ + directory: __dirname, + args: ['hello.js', '--sourcemap', '-o', 'dist/hello.js'], + }) + expect(fs.existsSync(distFile)).toBe(true) + expect( + fs + .readFileSync(distFile, { encoding: 'utf-8' }) + .includes('sourceMappingURL'), + ).toBe(true) + expect(fs.existsSync(distFile + '.map')).toBe(true) + expect(code).toBe(0) }) it(`cli minified with sourcemap should work properly`, async () => { - await createCliTest( - { - directory: __dirname, - args: ['hello.js', '-m', '--sourcemap', '-o', 'dist/hello.js'], - }, - ({ code, distFile }) => { - expect(fs.existsSync(distFile)).toBe(true) - expect( - fs - .readFileSync(distFile, { encoding: 'utf-8' }) - .includes('sourceMappingURL'), - ).toBe(true) - expect(fs.existsSync(distFile + '.map')).toBe(true) - expect(code).toBe(0) - }, - ) + const { code, distFile } = await createCliJob({ + directory: __dirname, + args: ['hello.js', '-m', '--sourcemap', '-o', 'dist/hello.js'], + }) + expect(fs.existsSync(distFile)).toBe(true) + expect( + fs + .readFileSync(distFile, { encoding: 'utf-8' }) + .includes('sourceMappingURL'), + ).toBe(true) + expect(fs.existsSync(distFile + '.map')).toBe(true) + expect(code).toBe(0) }) }) diff --git a/test/cli/cjs-relative-imports/index.test.ts b/test/cli/cjs-relative-imports/index.test.ts index baf6c8e8..ed1eacb0 100644 --- a/test/cli/cjs-relative-imports/index.test.ts +++ b/test/cli/cjs-relative-imports/index.test.ts @@ -1,21 +1,15 @@ import fs from 'fs' -import { createCliTest } from '../../testing-utils' +import { createCliJob } from '../../testing-utils' -describe('cli', () => { - it(`cli cjs-relative-imports should work properly`, async () => { - await createCliTest( - { - directory: __dirname, - args: ['index.js', '-o', 'dist/index.js'], - }, - ({ code, distFile }) => { - expect(code).toBe(0) +test(`cli cjs-relative-imports should work properly`, async () => { + const { code, distFile } = await createCliJob({ + directory: __dirname, + args: ['index.js', '-o', 'dist/index.js'], + }) + expect(code).toBe(0) - const content = fs.readFileSync(distFile, { encoding: 'utf-8' }) + const content = fs.readFileSync(distFile, { encoding: 'utf-8' }) - expect(content.includes('dot-js-dep')).toBe(true) - expect(content.includes('dot-cjs-dep')).toBe(true) - }, - ) - }) + expect(content.includes('dot-js-dep')).toBe(true) + expect(content.includes('dot-cjs-dep')).toBe(true) }) diff --git a/test/cli/dts-bundle/index.test.ts b/test/cli/dts-bundle/index.test.ts index c65946ab..fd909df7 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 { createCliTest, deleteFile } from '../../testing-utils' +import { createCliJob, deleteFile } from '../../testing-utils' describe('cli', () => { const dir = __dirname @@ -16,55 +16,39 @@ describe('cli', () => { }) it(`cli dts-bundle option should work properly`, async () => { - await createCliTest( - { - directory: __dirname, - args: ['./base.ts', '-o', 'dist/base.js', '--dts-bundle'], - }, - ({ code, distFile }) => { - const typeFile = distFile.replace('.js', '.d.ts') - expect(fs.existsSync(typeFile)).toBe(true) - expect(fs.readFileSync(typeFile, 'utf-8')).toContain( - 'type ParserConfig =', - ) - expect(code).toBe(0) - }, - ) + const { code, distFile } = await createCliJob({ + directory: __dirname, + args: ['./base.ts', '-o', 'dist/base.js', '--dts-bundle'], + }) + const typeFile = distFile.replace('.js', '.d.ts') + expect(fs.existsSync(typeFile)).toBe(true) + expect(fs.readFileSync(typeFile, 'utf-8')).toContain('type ParserConfig =') + expect(code).toBe(0) }) + it(`cli dts-bundle option should not bundle dts without dts-bundle`, async () => { - await createCliTest( - { - directory: __dirname, - args: ['./base.ts', '-o', 'dist/base.js'], - }, - ({ code, distFile }) => { - const typeFile = distFile.replace('.js', '.d.ts') - expect(fs.existsSync(typeFile)).toBe(true) - expect(fs.readFileSync(typeFile, 'utf-8')).toContain( - "from '@swc/types';", - ) - expect(code).toBe(0) - }, - ) + const { code, distFile } = await createCliJob({ + directory: __dirname, + args: ['./base.ts', '-o', 'dist/base.js'], + }) + const typeFile = distFile.replace('.js', '.d.ts') + expect(fs.existsSync(typeFile)).toBe(true) + expect(fs.readFileSync(typeFile, 'utf-8')).toContain("from '@swc/types';") + expect(code).toBe(0) }) + it(`cli dts-bundle option should not bundle dts from dependencies`, async () => { await fsp.writeFile( join(dir, './package.json'), '{ "name": "prepare-ts-with-pkg-json", "dependencies": { "@swc/types": "*" } }', ) - await createCliTest( - { - directory: __dirname, - args: ['./base.ts', '-o', 'dist/base.js'], - }, - ({ code, distFile }) => { - const typeFile = distFile.replace('.js', '.d.ts') - expect(fs.existsSync(typeFile)).toBe(true) - expect(fs.readFileSync(typeFile, 'utf-8')).toContain( - "from '@swc/types';", - ) - expect(code).toBe(0) - }, - ) + const { code, distFile } = await createCliJob({ + directory: __dirname, + args: ['./base.ts', '-o', 'dist/base.js'], + }) + const typeFile = distFile.replace('.js', '.d.ts') + expect(fs.existsSync(typeFile)).toBe(true) + expect(fs.readFileSync(typeFile, 'utf-8')).toContain("from '@swc/types';") + expect(code).toBe(0) }) }) diff --git a/test/cli/dts/index.test.ts b/test/cli/dts/index.test.ts index 6726ce9f..0fefec09 100644 --- a/test/cli/dts/index.test.ts +++ b/test/cli/dts/index.test.ts @@ -1,23 +1,19 @@ import fs from 'fs' import path from 'path' -import { createCliTest } from '../../testing-utils' +import { createCliJob } from '../../testing-utils' describe('cli', () => { it(`cli dts should work properly`, async () => { - await createCliTest( - { - directory: __dirname, - args: ['./input.ts', '-o', 'dist/output.js'], - }, - ({ code, distFile }) => { - const typeFile = distFile.replace('.js', '.d.ts') + const { code, distFile } = await createCliJob({ + directory: __dirname, + args: ['./input.ts', '-o', 'dist/output.js'], + }) + const typeFile = distFile.replace('.js', '.d.ts') - expect(path.basename(distFile)).toBe('output.js') - expect(path.basename(typeFile)).toBe('output.d.ts') - expect(fs.existsSync(distFile)).toBe(true) - expect(fs.existsSync(typeFile)).toBe(true) - expect(code).toBe(0) - }, - ) + expect(path.basename(distFile)).toBe('output.js') + expect(path.basename(typeFile)).toBe('output.d.ts') + expect(fs.existsSync(distFile)).toBe(true) + expect(fs.existsSync(typeFile)).toBe(true) + expect(code).toBe(0) }) }) diff --git a/test/cli/env-var/index.test.ts b/test/cli/env-var/index.test.ts index b24b0839..ee65c844 100644 --- a/test/cli/env-var/index.test.ts +++ b/test/cli/env-var/index.test.ts @@ -1,23 +1,17 @@ import fs from 'fs' -import { createCliTest } from '../../testing-utils' +import { createCliJob } from '../../testing-utils' -describe('cli', () => { - it(`cli env-var should work properly`, async () => { - await createCliTest( - { - directory: __dirname, - args: ['index.js', '--env', 'MY_TEST_ENV', '-o', 'dist/index.js'], - options: { - env: { - MY_TEST_ENV: 'my-test-value', - }, - }, +test(`cli env-var should work properly`, async () => { + const { code, distFile } = await createCliJob({ + directory: __dirname, + args: ['index.js', '--env', 'MY_TEST_ENV', '-o', 'dist/index.js'], + options: { + env: { + MY_TEST_ENV: 'my-test-value', }, - ({ code, distFile }) => { - const content = fs.readFileSync(distFile, { encoding: 'utf-8' }) - expect(content.includes('my-test-value')).toBe(true) - expect(code).toBe(0) - }, - ) + }, }) + const content = fs.readFileSync(distFile, { encoding: 'utf-8' }) + expect(content.includes('my-test-value')).toBe(true) + expect(code).toBe(0) }) diff --git a/test/cli/externals/index.test.ts b/test/cli/externals/index.test.ts index ab8a76f9..abcb9ad4 100644 --- a/test/cli/externals/index.test.ts +++ b/test/cli/externals/index.test.ts @@ -1,47 +1,39 @@ import fs from 'fs' -import { createCliTest } from '../../testing-utils' +import { createCliJob } from '../../testing-utils' describe('cli', () => { it(`cli external should work properly`, async () => { - await createCliTest( - { - directory: __dirname, - args: [ - 'with-externals.js', - '-o', - 'dist/with-externals.bundle.js', - '--external', - '@huozhi/testing-package', - ], - }, - ({ code, distFile }) => { - const content = fs.readFileSync(distFile, { encoding: 'utf-8' }) + const { code, distFile } = await createCliJob({ + directory: __dirname, + args: [ + 'with-externals.js', + '-o', + 'dist/with-externals.bundle.js', + '--external', + '@huozhi/testing-package', + ], + }) + const content = fs.readFileSync(distFile, { encoding: 'utf-8' }) - expect(content.includes('@@test_expected_string@@')).toBe(false) - expect(content.includes('bar-package')).toBe(true) - expect(code).toBe(0) - }, - ) + expect(content.includes('@@test_expected_string@@')).toBe(false) + expect(content.includes('bar-package')).toBe(true) + expect(code).toBe(0) }) it(`cli no-externals should work properly`, async () => { - await createCliTest( - { - directory: __dirname, - args: [ - 'with-externals.js', - '--no-external', - '-o', - 'dist/with-externals.bundle.js', - ], - }, - ({ code, distFile }) => { - const content = fs.readFileSync(distFile, { encoding: 'utf-8' }) + const { code, distFile } = await createCliJob({ + directory: __dirname, + args: [ + 'with-externals.js', + '--no-external', + '-o', + 'dist/with-externals.bundle.js', + ], + }) + const content = fs.readFileSync(distFile, { encoding: 'utf-8' }) - expect(content.includes('@@test_expected_string@@')).toBe(true) - expect(content.includes('bar-package')).toBe(true) - expect(code).toBe(0) - }, - ) + expect(content.includes('@@test_expected_string@@')).toBe(true) + expect(content.includes('bar-package')).toBe(true) + expect(code).toBe(0) }) }) diff --git a/test/cli/no-dts/index.test.ts b/test/cli/no-dts/index.test.ts index dc2dccd7..83a0b05f 100644 --- a/test/cli/no-dts/index.test.ts +++ b/test/cli/no-dts/index.test.ts @@ -1,22 +1,16 @@ import fs from 'fs' import path from 'path' -import { createCliTest } from '../../testing-utils' +import { createCliJob } from '../../testing-utils' -describe('cli', () => { - it(`cli no-dts option should work properly`, async () => { - await createCliTest( - { - directory: __dirname, - args: ['./base.ts', '-o', 'dist/base.js', '--no-dts'], - }, - ({ code, distFile }) => { - const typeFile = distFile.replace('.js', '.d.ts') - - expect(path.basename(distFile)).toBe('base.js') - expect(fs.existsSync(distFile)).toBe(true) - expect(fs.existsSync(typeFile)).toBe(false) - expect(code).toBe(0) - }, - ) +test(`cli no-dts option should work properly`, async () => { + const { code, distFile } = await createCliJob({ + directory: __dirname, + args: ['./base.ts', '-o', 'dist/base.js', '--no-dts'], }) + const typeFile = distFile.replace('.js', '.d.ts') + + expect(path.basename(distFile)).toBe('base.js') + expect(fs.existsSync(distFile)).toBe(true) + expect(fs.existsSync(typeFile)).toBe(false) + expect(code).toBe(0) }) diff --git a/test/cli/no-entry/index.test.ts b/test/cli/no-entry/index.test.ts index 5f3eb831..a46e7635 100644 --- a/test/cli/no-entry/index.test.ts +++ b/test/cli/no-entry/index.test.ts @@ -1,22 +1,14 @@ -import { createCliTest } from '../../testing-utils' +import { createCliJob } from '../../testing-utils' -describe('cli', () => { - it(`cli no-entry should work properly`, async () => { - await createCliTest( - { - directory: __dirname, - }, - ({ code, stderr }) => { - expect( - // The "src" directory does not contain any entry files. - // For proper usage, please refer to the following link: - // https://github.com/huozhi/bunchee#usage - stderr.includes( - 'The "src" directory does not contain any entry files.', - ), - ).toBe(true) - expect(code).toBe(0) - }, - ) +test(`cli no-entry should work properly`, async () => { + const { code, stderr } = await createCliJob({ + directory: __dirname, }) + expect( + // The "src" directory does not contain any entry files. + // For proper usage, please refer to the following link: + // https://github.com/huozhi/bunchee#usage + stderr.includes('The "src" directory does not contain any entry files.'), + ).toBe(true) + expect(code).toBe(0) }) diff --git a/test/cli/output-in-watch/index.test.ts b/test/cli/output-in-watch/index.test.ts index b89d88f2..e53148ec 100644 --- a/test/cli/output-in-watch/index.test.ts +++ b/test/cli/output-in-watch/index.test.ts @@ -1,18 +1,14 @@ -import { createJob } from '../../testing-utils' +import { createCliJob } from '../../testing-utils' -describe('cli - output-in-watch', () => { - const { job } = createJob({ +test('cli - output-in-watch', async () => { + const { stdout, signal } = await createCliJob({ directory: __dirname, args: ['hello.js', '-w', '-o', 'dist/hello.bundle.js'], abortTimeout: 3000, }) - it(`cli output-in-watch should work properly`, async () => { - const { signal, stdout } = job - - expect(stdout).toMatchInlineSnapshot(` - "Watching changes in the project directory... - " - `) - expect(signal).toBe('SIGTERM') // SIGTERM exit code - }) + expect(stdout).toMatchInlineSnapshot(` + "Watching changes in the project directory... + " + `) + expect(signal).toBe('SIGTERM') // SIGTERM exit code }) diff --git a/test/cli/target/index.test.ts b/test/cli/target/index.test.ts index 746e2712..363272b5 100644 --- a/test/cli/target/index.test.ts +++ b/test/cli/target/index.test.ts @@ -1,22 +1,16 @@ import fs from 'fs' -import { createCliTest } from '../../testing-utils' +import { createCliJob } from '../../testing-utils' -describe('cli', () => { - it(`cli es2020-target should work properly`, async () => { - await createCliTest( - { - directory: __dirname, - args: ['es2020.ts', '--target', 'es2020', '-o', 'dist/es2020.js'], - }, - ({ code, distFile }) => { - const content = fs.readFileSync(distFile, { encoding: 'utf-8' }) - - expect(content.includes(`...globalThis`)).toBe(true) - expect(content.includes(`setTimeout?.apply?.bind`)).toBe(true) - expect(content.includes(`async function`)).toBe(true) - expect(content.includes(`class A`)).toBe(true) - expect(code).toBe(0) - }, - ) +test(`cli es2020-target should work properly`, async () => { + const { code, distFile } = await createCliJob({ + directory: __dirname, + args: ['es2020.ts', '--target', 'es2020', '-o', 'dist/es2020.js'], }) + const content = fs.readFileSync(distFile, { encoding: 'utf-8' }) + + expect(content.includes(`...globalThis`)).toBe(true) + expect(content.includes(`setTimeout?.apply?.bind`)).toBe(true) + expect(content.includes(`async function`)).toBe(true) + expect(content.includes(`class A`)).toBe(true) + expect(code).toBe(0) }) diff --git a/test/cli/workspace/index.test.ts b/test/cli/workspace/index.test.ts index d2100c9c..cab78b17 100644 --- a/test/cli/workspace/index.test.ts +++ b/test/cli/workspace/index.test.ts @@ -1,22 +1,18 @@ import fs from 'fs' -import { createCliTest } from '../../testing-utils' +import { createCliJob } from '../../testing-utils' import path from 'path' describe('cli', () => { it(`cli workspace should work properly`, async () => { - await createCliTest( - { - directory: __dirname, - args: ['./index.ts'], - }, - ({ code, distDir }) => { - const distFile = path.join(distDir, 'index.mjs') + const { code, distDir } = await createCliJob({ + directory: __dirname, + args: ['./index.ts'], + }) + const distFile = path.join(distDir, 'index.mjs') - expect(fs.existsSync(distFile)).toBe(true) - // CLI does not generate declaration files - expect(fs.existsSync(distFile.replace('.mjs', '.d.mts'))).toBe(false) - expect(code).toBe(0) - }, - ) + expect(fs.existsSync(distFile)).toBe(true) + // CLI does not generate declaration files + expect(fs.existsSync(distFile.replace('.mjs', '.d.mts'))).toBe(false) + expect(code).toBe(0) }) }) diff --git a/test/testing-utils/cli.ts b/test/testing-utils/cli.ts index 89d59237..48875571 100644 --- a/test/testing-utils/cli.ts +++ b/test/testing-utils/cli.ts @@ -1,27 +1,21 @@ import { createTest, executeBunchee, type ExcuteBuncheeResult } from './shared' -export async function createCliTest( - { - args, - options, - abortTimeout, +export async function createCliJob({ + args, + options, + abortTimeout, + directory, +}: { + args?: string[] + options?: { env?: NodeJS.ProcessEnv } + abortTimeout?: number + directory: string +}) { + return await createTest({ directory, - }: { - args?: string[] - options?: { env?: NodeJS.ProcessEnv } - abortTimeout?: number - directory: string - }, - testFn: Parameters>[1], -): Promise { - await createTest( - { - directory, - args: args ?? [], - options: options ?? {}, - abortTimeout, - run: executeBunchee, - }, - testFn, - ) + args: args ?? [], + options: options ?? {}, + abortTimeout, + run: executeBunchee, + }) } diff --git a/test/testing-utils/index.ts b/test/testing-utils/index.ts index e723eea3..5b84f8af 100644 --- a/test/testing-utils/index.ts +++ b/test/testing-utils/index.ts @@ -18,7 +18,7 @@ type IntegrationTestOptions = { directory: string } -export { createCliTest } from './cli' +export { createCliJob } from './cli' /** * @deprecated