Skip to content

Commit 275f517

Browse files
committed
test: expect warning
1 parent 4a3af33 commit 275f517

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

tests/shims.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('shims', () => {
2222
})
2323

2424
test('cjs on node w/o shims', async (context) => {
25-
const { snapshot } = await testBuild({
25+
const { snapshot, warnings } = await testBuild({
2626
context,
2727
files: { 'index.ts': code },
2828
options: {
@@ -32,6 +32,11 @@ describe('shims', () => {
3232
})
3333
expect(snapshot).contain('require("url").pathToFileURL(__filename).href')
3434
expect(snapshot).not.contain('import.meta')
35+
expect(warnings).toMatchObject([
36+
{
37+
code: 'EMPTY_IMPORT_META',
38+
},
39+
])
3540
})
3641

3742
test('cjs on neutral w/o shims', async (context) => {

tests/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { fileURLToPath } from 'node:url'
55
import { expectFilesSnapshot } from '@sxzz/test-utils'
66
import { glob } from 'tinyglobby'
77
import { build, type Options } from '../src/index'
8+
import type { RollupLog } from 'rolldown'
89
import type { RunnerTask, TestContext } from 'vitest'
910

1011
const dirname = path.dirname(fileURLToPath(import.meta.url))
@@ -123,18 +124,29 @@ export async function testBuild({
123124
outputDir: string
124125
snapshot: string
125126
fileMap: Record<string, string>
127+
warnings: RollupLog[]
126128
}> {
127129
const { expect } = context
128130
const { testName, testDir } = await writeFixtures(context, files, fixture)
129131

130132
const workingDir = path.join(testDir, cwd || '.')
131133
const restoreCwd = chdir(workingDir)
134+
const warnings: RollupLog[] = []
132135
const resolvedOptions: Options = {
133136
entry: 'index.ts',
134137
config: false,
135138
outDir: 'dist',
136139
dts: false,
137140
silent: true,
141+
inputOptions: {
142+
onLog(level, log, defaultHandler) {
143+
if (level === 'warn') {
144+
warnings.push(log)
145+
return
146+
}
147+
defaultHandler(level, log)
148+
},
149+
},
138150
...(typeof options === 'function' ? options(workingDir) : options),
139151
}
140152
await beforeBuild?.()
@@ -159,6 +171,7 @@ export async function testBuild({
159171
outputDir,
160172
snapshot,
161173
fileMap,
174+
warnings,
162175
}
163176
}
164177

0 commit comments

Comments
 (0)