Skip to content

Commit

Permalink
test: add sync helper
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jan 11, 2025
1 parent 88452bf commit b943b77
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 20 deletions.
14 changes: 5 additions & 9 deletions test/fixtures/integration-test-template/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { createIntegrationTest, assertContainFiles } from '../../testing-utils'
import { createJob, assertContainFiles } from '../../testing-utils'

describe('integration - <name>', () => {
const { distDir } = createJob({
directory: __dirname,
})
it('should work', async () => {
await createIntegrationTest(
{
directory: __dirname,
},
({ distDir }) => {
assertContainFiles(distDir, ['index.js'])
},
)
assertContainFiles(distDir, ['index.js'])
})
})
17 changes: 7 additions & 10 deletions test/integration/basic-jsx/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { createIntegrationTest, assertContainFiles } from '../../testing-utils'
import { createJob, assertContainFiles } from '../../testing-utils'

describe('integration basic-jsx', () => {
const { job, distDir } = createJob({
directory: __dirname,
})
it('should work with basic JSX format', async () => {
await createIntegrationTest(
{
directory: __dirname,
},
({ distDir, stderr, stdout }) => {
expect(stderr + stdout).not.toContain('(swc plugin)')
assertContainFiles(distDir, ['index.js', 'index.mjs'])
},
)
const { stdout, stderr } = job
expect(stderr + stdout).not.toContain('(swc plugin)')
assertContainFiles(distDir, ['index.js', 'index.mjs'])
})
})
27 changes: 26 additions & 1 deletion test/testing-utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { glob } from 'glob'
import { createTest, executeBunchee, type ExcuteBuncheeResult } from './shared'
import {
createSyncTest,
createTest,
executeBunchee,
type ExcuteBuncheeResult,
} from './shared'

function normalizePath(filePath: string) {
return filePath.replace(/\\/g, '/')
Expand All @@ -15,6 +20,10 @@ type IntegrationTestOptions = {

export { createCliTest } from './cli'

/**
* @deprecated
* Use createJob instead
*/
export async function createIntegrationTest(
{ args, options, abortTimeout, directory }: IntegrationTestOptions,
testFn?: Parameters<typeof createTest<ExcuteBuncheeResult>>[1],
Expand All @@ -31,6 +40,22 @@ export async function createIntegrationTest(
)
}

/** Sync testing helper */
export function createJob({
args,
options,
abortTimeout,
directory,
}: IntegrationTestOptions) {
return createSyncTest<ExcuteBuncheeResult>({
args: args ?? [],
options: options ?? {},
abortTimeout,
directory,
run: executeBunchee,
})
}

export function createIntegration(
integrationTestOptions: IntegrationTestOptions,
testFn?: Parameters<typeof createTest<ExcuteBuncheeResult>>[1],
Expand Down
65 changes: 65 additions & 0 deletions test/testing-utils/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,71 @@ export async function createTest<T>(
return build
}

export function createSyncTest<T>({
args,
options,
directory,
abortTimeout,
run,
}: {
args: string[]
options: { env?: NodeJS.ProcessEnv }
directory: string
abortTimeout?: number
run: (
args: string[],
options: { env?: NodeJS.ProcessEnv },
processOptions?: { abortTimeout?: number },
) => Promise<T>
}): CreateTestResultExtra & { 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])
}

const baseInfo = {
dir: directory,
distDir,
distFile,
}

let result = undefined
const resultProxy = new Proxy(
{},
{
get(_, key) {
return result?.[key] ?? null
},
},
) as any

beforeAll(async () => {
// execute the job
result = await run(args, options, { abortTimeout })
})
afterAll(async () => {
if (!process.env.TEST_NOT_CLEANUP) {
await removeDirectory(distDir)
}
})

return {
get job() {
return resultProxy
},
distDir,
distFile,
dir: directory,
}
}

export type ExcuteBuncheeResult = {
code: number | null
signal: NodeJS.Signals | null
Expand Down

0 comments on commit b943b77

Please sign in to comment.