Skip to content

Commit

Permalink
test: improve cli testing util
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jan 12, 2025
1 parent 9507054 commit 8633425
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 301 deletions.
118 changes: 49 additions & 69 deletions test/cli/basic/index.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
26 changes: 10 additions & 16 deletions test/cli/cjs-relative-imports/index.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
70 changes: 27 additions & 43 deletions test/cli/dts-bundle/index.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
})
})
26 changes: 11 additions & 15 deletions test/cli/dts/index.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
30 changes: 12 additions & 18 deletions test/cli/env-var/index.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
Loading

0 comments on commit 8633425

Please sign in to comment.