Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distribute integration test to single test files #455

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
b144164
refactor: move common test utils from cli to testing-utils
devjiwonchoi Feb 18, 2024
b53f7ea
refactor: move integration to test utils
devjiwonchoi Feb 18, 2024
b9b4d97
test: move basic-jsx
devjiwonchoi Feb 18, 2024
ce4e737
refactor: assert for fixtures
devjiwonchoi Feb 18, 2024
040042e
test: move cjs-pkg-esm-main-field
devjiwonchoi Feb 18, 2024
4a2cad6
refactor: add assert fixtures file content
devjiwonchoi Feb 18, 2024
f10ab62
test: add default node mjs
devjiwonchoi Feb 18, 2024
76e25dc
test: remove default-node-mjs from integration
devjiwonchoi Feb 18, 2024
1284b60
refactor: reuse the test utils
devjiwonchoi Feb 18, 2024
2b1998e
test: move bin/cts
devjiwonchoi Feb 18, 2024
4a5ba85
Merge branch 'main' of github.com:devjiwonchoi/bunchee into migrate-i…
devjiwonchoi Feb 18, 2024
072cb95
test: remove bin/cts from integration
devjiwonchoi Feb 18, 2024
4a1de65
test: move bin/multi-path
devjiwonchoi Feb 18, 2024
b2c367f
test: move bin/single-path
devjiwonchoi Feb 18, 2024
0071760
refactor: receive dist and fixtures dir from create test callback
devjiwonchoi Feb 18, 2024
8376366
test: move dev-prod-convention
devjiwonchoi Feb 18, 2024
fffdd21
refactor: add additional files to remove param for running test
devjiwonchoi Feb 18, 2024
6e7267a
chore: apply filesToRemove
devjiwonchoi Feb 18, 2024
1ac54a4
test: move duplicate-entry
devjiwonchoi Feb 18, 2024
bb282ae
test: move edge-variable
devjiwonchoi Feb 18, 2024
e14c1d9
test: move entry-index-index
devjiwonchoi Feb 18, 2024
439bf6e
test: move esm-pkg-cjs-main-field
devjiwonchoi Feb 18, 2024
d711f84
test: move esm-shims
devjiwonchoi Feb 18, 2024
6dd216d
test: move externals
devjiwonchoi Feb 18, 2024
c62a3f6
test: move invalid-exports-cjs
devjiwonchoi Feb 18, 2024
6a0bcdd
test: move invalid-exports-esm
devjiwonchoi Feb 18, 2024
f83e725
test: move js-only
devjiwonchoi Feb 18, 2024
38cb3ea
refactor: export testing-utils from integrations utils
devjiwonchoi Feb 18, 2024
d8d971c
refactor: resolve dist dir directly
devjiwonchoi Feb 18, 2024
546ccdd
refactor: revert passing fixturesDir on createTest callback
devjiwonchoi Feb 18, 2024
b06db21
refactor: use distDir param on bin/cts
devjiwonchoi Feb 18, 2024
6827ce3
refactor: import child_process fork as named export
devjiwonchoi Feb 18, 2024
6c4ea5f
refactor: remove filesToRemove, temporarily removing tsconfig.json ma…
devjiwonchoi Feb 18, 2024
fd98787
refactor: delete tsconfig file aftereach
devjiwonchoi Feb 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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.

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 () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably don't need it here, for single test

await deleteFile(`${__dirname}/fixtures/tsconfig.json`)
})

describe('integration', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to use different names, like "integration - cts" for describe() name, then give it("should ..") to describe the expectation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! Do you want me to resolve it on this PR or other?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ye not blocking, can follow up later!

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
Loading