/
fixture.test.ts
54 lines (49 loc) · 1.64 KB
/
fixture.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { test } from 'vitest'
import type { TestConfig } from './src/types'
import { InputOptions, OutputOptions, rolldown } from 'rolldown'
import nodePath from 'node:path'
import * as fastGlob from 'fast-glob'
import { loadTestConfig } from '@tests/utils'
await main()
async function main() {
const fixturesPath = nodePath.join(__dirname, 'fixtures')
const testConfigPaths = fastGlob.sync('fixtures/**/_config.ts', {
absolute: true,
cwd: __dirname,
})
for (const testConfigPath of testConfigPaths) {
const dirPath = nodePath.relative(
fixturesPath,
nodePath.dirname(testConfigPath),
)
const testConfig = await loadTestConfig(testConfigPath)
test.skipIf(testConfig.skip)(dirPath, async () => {
// FIXME: This empty log is here to make vitest shows stdout/stderr content made from rust. Wonder why.
try {
const output = await compileFixture(
nodePath.dirname(testConfigPath),
testConfig,
)
if (testConfig.afterTest) {
testConfig.afterTest(output)
}
} catch (err) {
throw new Error(`Failed in ${testConfigPath}`)
}
})
}
}
async function compileFixture(fixturePath: string, config: TestConfig) {
let outputOptions: OutputOptions = config.config?.output ?? {}
delete config.config?.output
outputOptions = {
dir: outputOptions.dir ?? nodePath.join(fixturePath, 'dist'),
...outputOptions,
}
const inputOptions: InputOptions = {
input: config.config?.input ?? nodePath.join(fixturePath, 'main.js'),
...config.config,
}
const build = await rolldown(inputOptions)
return await build.write(outputOptions)
}