Skip to content

Commit

Permalink
Distribute integration test to single test files (#455)
Browse files Browse the repository at this point in the history
Co-authored-by: Jiachi Liu <[email protected]>
  • Loading branch information
devjiwonchoi and huozhi authored Feb 18, 2024
1 parent a3856b7 commit bbfa097
Show file tree
Hide file tree
Showing 70 changed files with 429 additions and 263 deletions.
59 changes: 5 additions & 54 deletions test/cli/utils.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,8 @@
import { rm } from 'fs/promises'
import { fork } from 'child_process'
import * as debug from '../utils/debug'
import { createTest } from '../testing-utils'

type ExcuteBuncheeResult = {
code: number
stdout: string
stderr: string
}

export async function executeBunchee(
args: string[],
options: { env?: NodeJS.ProcessEnv },
processOptions?: { abortTimeout?: number },
): Promise<ExcuteBuncheeResult> {
debug.log(`Command: bunchee ${args.join(' ')}`)

const assetPath = process.env.POST_BUILD
? '/../../dist/bin/cli.js'
: '/../../src/bin/index.ts'

const ps = fork(
`${require.resolve('tsx/cli')}`,
[__dirname + assetPath].concat(args),
{
stdio: 'pipe',
env: options.env,
},
)
let stderr = ''
let stdout = ''
ps.stdout?.on('data', (chunk) => (stdout += chunk.toString()))
ps.stderr?.on('data', (chunk) => (stderr += chunk.toString()))

if (typeof processOptions?.abortTimeout === 'number') {
setTimeout(() => {
ps.kill('SIGTERM')
}, processOptions.abortTimeout)
}

const code = (await new Promise((resolve) => {
ps.on('close', resolve)
})) as number
if (stdout) console.log(stdout)
if (stderr) console.error(stderr)

return { code, stdout, stderr }
}
import {
createTest,
executeBunchee,
type ExcuteBuncheeResult,
} from '../testing-utils'

export async function createCliTest(
{
Expand All @@ -73,8 +29,3 @@ export async function createCliTest(
testFn,
)
}

export async function removeDirectory(tempDirPath: string) {
debug.log(`Clean up ${tempDirPath}`)
await rm(tempDirPath, { recursive: true, force: true })
}
203 changes: 0 additions & 203 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,6 @@ const testCases: {
{ stderr, stdout }: { stderr: string; stdout: string },
): Promise<void> | void
}[] = [
{
name: 'basic-jsx',
async expected(dir, { stderr, stdout }) {
// No warnings from swc3 plugin
expect(stderr + stdout).not.toContain('(swc plugin)')
assertContainFiles(dir, ['./dist/index.js', './dist/index.mjs'])
},
},
{
name: 'externals',
async expected(dir) {
const distFile = join(dir, './dist/index.js')
const content = await fsp.readFile(distFile, { encoding: 'utf-8' })
expect(content).toMatch(/['"]peer-dep['"]/)
expect(content).toMatch(/['"]peer-dep-meta['"]/)
},
},
{
name: 'duplicate-entry',
async expected(dir, { stdout }) {
const distFiles = [
'dist/index.js',
'dist/index.mjs',
'dist/index.d.ts',
'dist/index.d.mts',
]
assertContainFiles(dir, distFiles)
for (const filename of distFiles) {
// only contain file name once
expect(stdout.split(filename).length).toBe(2)
}
},
},
{
name: 'ts-error',
async expected(dir, { stdout, stderr }) {
Expand All @@ -85,22 +52,6 @@ const testCases: {
})
},
},
{
name: 'js-only',
async expected(dir) {
const distFile = join(dir, './dist/index.js')
expect(await existsFile(distFile)).toBe(true)
},
},
{
name: 'entry-index-index',
async expected(dir) {
await assertFilesContent(dir, {
'./dist/index.js': /'index'/,
'./dist/react-server.js': /\'react-server\'/,
})
},
},
{
name: 'pkg-exports',
async expected(dir) {
Expand Down Expand Up @@ -368,98 +319,6 @@ const testCases: {
expect(stderr).toContain(log)
},
},
{
name: 'cjs-pkg-esm-main-field',
args: [],
async expected(_, { stderr }) {
expect(stderr).toContain(
'Cannot export `main` field with .mjs extension in CJS package, only .js extension is allowed',
)
},
},
{
name: 'esm-pkg-cjs-main-field',
async expected(dir) {
const distFiles = ['./dist/index.cjs', './dist/index.mjs']
assertContainFiles(dir, distFiles)
},
},
{
name: 'bin/single-path',
args: [],
async expected(dir) {
const distFiles = [
join(dir, './dist/bin.js'),
join(dir, './dist/bin.d.ts'),
]
await assertContainFiles(dir, distFiles)
expect(await fsp.readFile(distFiles[0], 'utf-8')).toContain(
'#!/usr/bin/env node',
)
},
},
{
name: 'bin/multi-path',
args: [],
async expected(dir) {
const distBinFiles = [
join(dir, './dist/bin/a.js'),
join(dir, './dist/bin/b.js'),
]
const distTypeFiles = [
join(dir, './dist/bin/a.d.ts'),
join(dir, './dist/bin/b.d.ts'),
]
const distFiles = distBinFiles.concat(distTypeFiles)

for (const distFile of distFiles) {
expect(await existsFile(distFile)).toBe(true)
}
for (const distScriptFile of distBinFiles) {
expect(await fsp.readFile(distScriptFile, 'utf-8')).toContain(
'#!/usr/bin/env node',
)
}
},
},
{
name: 'bin/cts',
args: [],
async expected(dir) {
const distFiles = [
join(dir, './dist/bin/index.cjs'),
join(dir, './dist/bin/index.d.cts'),
]

for (const distFile of distFiles) {
expect(await existsFile(distFile)).toBe(true)
}

expect(await fsp.readFile(distFiles[0], 'utf-8')).toContain(
'#!/usr/bin/env node',
)
},
},
{
name: 'esm-shims',
async expected(dir) {
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(dir, {
'./dist/require.mjs': requirePolyfill,
'./dist/filename.mjs': filenamePolyfill,
'./dist/dirname.mjs': dirnamePolyfill,
'./dist/require.js': /require\(/,
'./dist/filename.js': /__filename/,
'./dist/dirname.js': /__dirname/,
})
},
},
{
name: 'raw-data',
args: [],
Expand Down Expand Up @@ -597,18 +456,6 @@ const testCases: {
expect(sharedCjs).toContain('shared-export')
},
},
{
name: 'default-node-mjs',
args: [],
async expected(dir) {
const distFiles = [join(dir, './dist/index.node.mjs')]
for (const f of distFiles) {
expect(await existsFile(f)).toBe(true)
}
expect(await fsp.readFile(distFiles[0], 'utf-8')).toContain('export {')
expect(await fsp.readFile(distFiles[0], 'utf-8')).not.toContain('exports')
},
},
{
name: 'output',
args: [],
Expand Down Expand Up @@ -850,19 +697,6 @@ const testCases: {
expect(pkgJson.module).toBe('./dist/es/index.mjs')
},
},
{
name: 'dev-prod-convention',
async expected(dir) {
await assertFilesContent(dir, {
'./dist/index.development.js': /= "development"/,
'./dist/index.development.mjs': /= "development"/,
'./dist/index.production.js': /= "production"/,
'dist/index.production.mjs': /= "production"/,
'./dist/index.js': /= 'index'/,
'./dist/index.mjs': /= 'index'/,
})
},
},
{
name: 'no-clean',
args: ['--no-clean'],
Expand Down Expand Up @@ -891,34 +725,6 @@ const testCases: {
expect(await existsFile(join(dir, './dist/index.js'))).toBe(true)
},
},
{
name: 'invalid-exports-cjs',
async expected(dir, { 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')
},
},
{
name: 'invalid-exports-esm',
async expected(dir, { 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')
},
},
{
name: 'ts-composite',
dir: 'monorepo-composite/packages/a',
Expand All @@ -935,15 +741,6 @@ const testCases: {
expect(await existsFile(join(dir, './dist/index.d.ts'))).toBe(true)
},
},
{
name: 'edge-variable',
async expected(dir) {
assertFilesContent(dir, {
'./dist/index.js': /typeof EdgeRuntime/,
'./dist/index.edge.js': /typeof "edge-runtime"/,
})
},
},
]

async function runBundle(
Expand Down
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions test/integration/basic-jsx/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createIntegrationTest, assertContainFiles } from '../utils'

describe('integration', () => {
test(`basic-jsx`, async () => {
await createIntegrationTest(
{
directory: __dirname,
},
({ distDir, stderr, stdout }) => {
expect(stderr + stdout).not.toContain('(swc plugin)')
assertContainFiles(distDir, ['index.js', 'index.mjs'])
},
)
})
})
1 change: 0 additions & 1 deletion test/integration/bin/cts/.gitignore

This file was deleted.

File renamed without changes.
File renamed without changes.
30 changes: 30 additions & 0 deletions test/integration/bin/cts/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { readFile } from 'fs/promises'
import { createIntegrationTest, deleteFile, existsFile } from '../../utils'

afterEach(async () => {
await deleteFile(`${__dirname}/fixtures/tsconfig.json`)
})

describe('integration', () => {
test(`bin/cts`, async () => {
await createIntegrationTest(
{
directory: __dirname,
},
async ({ distDir }) => {
const distFiles = [
`${distDir}/bin/index.cjs`,
`${distDir}/bin/index.d.cts`,
]

for (const distFile of distFiles) {
expect(await existsFile(distFile)).toBe(true)
}

expect(await readFile(distFiles[0], 'utf-8')).toContain(
'#!/usr/bin/env node',
)
},
)
})
})
1 change: 0 additions & 1 deletion test/integration/bin/multi-path/.gitignore

This file was deleted.

Loading

0 comments on commit bbfa097

Please sign in to comment.