Skip to content

Commit

Permalink
feat: setup rolldown node test (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Nov 17, 2023
1 parent 526a04b commit 7638193
Show file tree
Hide file tree
Showing 8 changed files with 402 additions and 6 deletions.
1 change: 1 addition & 0 deletions .ls-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ ignore:
- packages/rollup-tests/test # Copied from rollup repo
- 'crates/rolldown/tests/fixtures/**/_test.js' # `_test.js` is a special file, which will be treat as the entry file while executing the build output of fixture.
- packages/node/build.config.ts # convention name for using unbuild
- packages/node/vitest.config.ts
# FIXME: should not ignore following folders
- web
4 changes: 4 additions & 0 deletions CONTRIBUTING.md/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Rolldown will bundle the input into `/dist`, and using the same `node` instance

## Node Testing

### Rolldown Testing

The rolldown testing is located at `packages/node/test`. It is used to test rolldown exported node api, also including options. If you add a options from rollup, please add corresponding test case for it.

### Rollup Testing

Tests cases are stored in `/rollup`, which is a git submodule of `rolldown`. So you need to run `just update` to initialize the submodule before running these tests.
Expand Down
6 changes: 4 additions & 2 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"dist"
],
"scripts": {
"build": "unbuild"
"build": "unbuild",
"test": "vitest run"
},
"dependencies": {
"@rolldown/node-binding": "workspace:*"
Expand All @@ -32,6 +33,7 @@
"rollup": "^3.17.2",
"type-fest": "^3.6.0",
"typescript": "^5.0.0",
"unbuild": "^2.0.0"
"unbuild": "^2.0.0",
"vitest": "^0.34.6"
}
}
8 changes: 8 additions & 0 deletions packages/node/test/cases/input/string/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { RollupOptions } from '@rolldown/node'
import path from 'path'

const config: RollupOptions = {
input: path.join(__dirname, 'main.js'),
}

export default config
1 change: 1 addition & 0 deletions packages/node/test/cases/input/string/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(1)
65 changes: 65 additions & 0 deletions packages/node/test/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { describe, expect, test } from 'vitest'
import {
InputOptions,
OutputOptions,
RollupOptions,
rolldown,
} from '@rolldown/node'
import path from 'path'
import fs from 'fs'

runCases()

function runCases() {
const testCasesRoot = path.join(__dirname, 'cases')
const cases = fs.readdirSync(testCasesRoot)

for (const name of cases) {
describe(name, async () => {
const subCasesRoot = path.join(testCasesRoot, name)
const subCases = fs.readdirSync(subCasesRoot)

for (const subCaseName of subCases) {
const caseRoot = path.join(subCasesRoot, subCaseName)
const config = await getCaseConfig(caseRoot)

test(subCaseName, async () => {
try {
await runCaseBundle(caseRoot, config)
expect(true)
} catch (error) {
throw error
}
})
}
})
}
}

async function runCaseBundle(caseRoot: string, config?: RollupOptions) {
config = normalizedOptions(caseRoot, config)
const build = await rolldown(config as InputOptions)
await build.write(config.output as OutputOptions)
}

function normalizedOptions(caseRoot: string, config?: RollupOptions) {
if (Array.isArray(config?.output)) {
throw new Error(`The ${caseRoot} output shouldn't be array`)
}
const output = config?.output ?? {}

return {
input: config?.input ?? path.join(caseRoot, 'main.js'),
output: {
dir: output.dir ?? path.join(caseRoot, 'dist'),
},
...config,
}
}

async function getCaseConfig(caseRoot: string) {
const caseConfigPath = path.join(caseRoot, 'config.ts')
return fs.existsSync(caseConfigPath)
? (await import(caseConfigPath)).default
: undefined
}
11 changes: 11 additions & 0 deletions packages/node/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
include: ['./test/runner.ts'],
testTimeout: 20000,
},
esbuild: {
target: 'node18',
},
})

0 comments on commit 7638193

Please sign in to comment.