Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 9 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ ready:
just build
just test

build: build-core
pnpm --filter @oxc-node/cli build
build: build-core build-cli
pnpm install

build-cli: build-core
pnpm --filter @oxc-node/cli build

build-core:
pnpm --filter @oxc-node/core build
pnpm --filter @oxc-node/core export-oxc-runtime

test:
pnpm test
test: test-cli


test-cli: build-cli
pnpm --filter @oxc-node/cli-tests test
16 changes: 16 additions & 0 deletions packages/cli/tests/e2e-fixtures/basic-ts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Test that oxnode can execute TypeScript files directly
const message: string = '!__E2E_OK__!'

interface TestResult {
success: boolean
value: number
}

const result: TestResult = {
success: true,
value: 42,
} as const

if (result.success) {
console.log(message)
}
6 changes: 6 additions & 0 deletions packages/cli/tests/e2e-fixtures/basic-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "basic-ts-fixture",
"version": "0.0.0",
"private": true,
"type": "module"
}
18 changes: 18 additions & 0 deletions packages/cli/tests/e2e/basic-ts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import { OxnodeCLI } from '../src/invoke-cli.js'

const __dirname = path.dirname(fileURLToPath(import.meta.url))
const FIXTURE_PATH = path.resolve(__dirname, '../e2e-fixtures/basic-ts')

describe('basic-ts fixture', () => {
it('should execute TypeScript file directly', () => {
const [exitCode, logs] = OxnodeCLI()
.setCwd(FIXTURE_PATH)
.invoke(['index.ts'])

expect(exitCode).toBe(0)
logs.should.contain('!__E2E_OK__!')
})
})
19 changes: 19 additions & 0 deletions packages/cli/tests/e2e/sample.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest'
import { OxnodeCLI } from '../src/invoke-cli.js'

describe('oxnode CLI', () => {
it('should show help when --help flag is passed', () => {
const [exitCode, logs] = OxnodeCLI().invoke(['--help'])

expect(exitCode).toBe(0)
logs.should.contain('oxnode')
logs.should.contain('Run a script with oxc transformer and oxc-resolver')
})

it('should show version when --version flag is passed', () => {
const [exitCode, logs] = OxnodeCLI().invoke(['--version'])

expect(exitCode).toBe(0)
logs.should.contain('0.0.')
})
})
17 changes: 17 additions & 0 deletions packages/cli/tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@oxc-node/cli-tests",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"test": "vitest run"
},
"devDependencies": {
"@oxc-node/cli": "workspace:*",
"@oxc-node/core": "workspace:*",
"@types/node": "^24.1.0",
"execa": "^9.6.0",
"strip-ansi": "^7.1.0",
"vitest": "^2.1.8"
}
}
57 changes: 57 additions & 0 deletions packages/cli/tests/src/invoke-cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { execaSync } from 'execa'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import stripAnsi from 'strip-ansi'
import { createLogsMatcher, type LogsMatcher } from './matchers.js'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

// Path to the built CLI binary
const CLI_PATH = path.resolve(__dirname, '../../../cli/dist/index.js')

export interface OxnodeCLI {
invoke: (args: string[]) => [number, LogsMatcher]
setCwd: (cwd: string) => OxnodeCLI
}

export function OxnodeCLI(): OxnodeCLI {
let workingDirectory = process.cwd()

const setCwd = (cwd: string): OxnodeCLI => {
workingDirectory = cwd
return api
}

const invoke = (args: string[]): [number, LogsMatcher] => {
try {
const result = execaSync('node', [CLI_PATH, ...args], {
cwd: workingDirectory,
env: {
...process.env,
NODE_ENV: 'production',
},
all: true,
reject: false,
})

const output = stripAnsi(result.all || '')
const exitCode = result.exitCode

return [exitCode, createLogsMatcher(output)]
} catch (error) {
// If execa throws an error, capture it
const err = error as { all?: string; exitCode?: number }
const output = stripAnsi(err.all || '')
const exitCode = err.exitCode || 1

return [exitCode, createLogsMatcher(output)]
}
}

const api: OxnodeCLI = {
invoke,
setCwd,
}

return api
}
31 changes: 31 additions & 0 deletions packages/cli/tests/src/matchers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { expect } from 'vitest'

export interface LogsMatcher {
logOutput: () => void
should: {
contain: (expected: string) => void
not: {
contain: (expected: string) => void
}
}
}

export function createLogsMatcher(output: string): LogsMatcher {
return {
logOutput: () => {
console.log('=== CLI Output ===')
console.log(output)
console.log('=================')
},
should: {
contain: (expected: string) => {
expect(output).toContain(expected)
},
not: {
contain: (expected: string) => {
expect(output).not.toContain(expected)
},
},
},
}
}
9 changes: 9 additions & 0 deletions packages/cli/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"composite": true,
"types": ["vitest/globals", "node"]
},
"include": ["./**/*.ts"],
"exclude": ["node_modules"]
}
13 changes: 13 additions & 0 deletions packages/cli/tests/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
globals: true,
environment: 'node',
include: ['**/*.test.ts', '**/*.spec.ts'],
forceRerunTriggers: [
'**/packages/cli/dist/**',
'**/node_modules/@oxc-node/cli/dist/**',
],
},
})
Loading