Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7638193

Browse files
authored
feat: setup rolldown node test (#307)
1 parent 526a04b commit 7638193

8 files changed

Lines changed: 402 additions & 6 deletions

File tree

.ls-lint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ ignore:
1212
- packages/rollup-tests/test # Copied from rollup repo
1313
- '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.
1414
- packages/node/build.config.ts # convention name for using unbuild
15+
- packages/node/vitest.config.ts
1516
# FIXME: should not ignore following folders
1617
- web

CONTRIBUTING.md/testing.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ Rolldown will bundle the input into `/dist`, and using the same `node` instance
2626

2727
## Node Testing
2828

29+
### Rolldown Testing
30+
31+
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.
32+
2933
### Rollup Testing
3034

3135
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.

packages/node/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"dist"
2323
],
2424
"scripts": {
25-
"build": "unbuild"
25+
"build": "unbuild",
26+
"test": "vitest run"
2627
},
2728
"dependencies": {
2829
"@rolldown/node-binding": "workspace:*"
@@ -32,6 +33,7 @@
3233
"rollup": "^3.17.2",
3334
"type-fest": "^3.6.0",
3435
"typescript": "^5.0.0",
35-
"unbuild": "^2.0.0"
36+
"unbuild": "^2.0.0",
37+
"vitest": "^0.34.6"
3638
}
3739
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { RollupOptions } from '@rolldown/node'
2+
import path from 'path'
3+
4+
const config: RollupOptions = {
5+
input: path.join(__dirname, 'main.js'),
6+
}
7+
8+
export default config
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(1)

packages/node/test/runner.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { describe, expect, test } from 'vitest'
2+
import {
3+
InputOptions,
4+
OutputOptions,
5+
RollupOptions,
6+
rolldown,
7+
} from '@rolldown/node'
8+
import path from 'path'
9+
import fs from 'fs'
10+
11+
runCases()
12+
13+
function runCases() {
14+
const testCasesRoot = path.join(__dirname, 'cases')
15+
const cases = fs.readdirSync(testCasesRoot)
16+
17+
for (const name of cases) {
18+
describe(name, async () => {
19+
const subCasesRoot = path.join(testCasesRoot, name)
20+
const subCases = fs.readdirSync(subCasesRoot)
21+
22+
for (const subCaseName of subCases) {
23+
const caseRoot = path.join(subCasesRoot, subCaseName)
24+
const config = await getCaseConfig(caseRoot)
25+
26+
test(subCaseName, async () => {
27+
try {
28+
await runCaseBundle(caseRoot, config)
29+
expect(true)
30+
} catch (error) {
31+
throw error
32+
}
33+
})
34+
}
35+
})
36+
}
37+
}
38+
39+
async function runCaseBundle(caseRoot: string, config?: RollupOptions) {
40+
config = normalizedOptions(caseRoot, config)
41+
const build = await rolldown(config as InputOptions)
42+
await build.write(config.output as OutputOptions)
43+
}
44+
45+
function normalizedOptions(caseRoot: string, config?: RollupOptions) {
46+
if (Array.isArray(config?.output)) {
47+
throw new Error(`The ${caseRoot} output shouldn't be array`)
48+
}
49+
const output = config?.output ?? {}
50+
51+
return {
52+
input: config?.input ?? path.join(caseRoot, 'main.js'),
53+
output: {
54+
dir: output.dir ?? path.join(caseRoot, 'dist'),
55+
},
56+
...config,
57+
}
58+
}
59+
60+
async function getCaseConfig(caseRoot: string) {
61+
const caseConfigPath = path.join(caseRoot, 'config.ts')
62+
return fs.existsSync(caseConfigPath)
63+
? (await import(caseConfigPath)).default
64+
: undefined
65+
}

packages/node/vitest.config.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
include: ['./test/runner.ts'],
6+
testTimeout: 20000,
7+
},
8+
esbuild: {
9+
target: 'node18',
10+
},
11+
})

0 commit comments

Comments
 (0)