Skip to content

Commit 411cc3d

Browse files
author
sun0day
authoredNov 17, 2022
test: improve node/build.ts ut coverage (#10786)
test: improve `node/build.ts` ut coverage - statements 48.32% branches 74.35% functions 26.82% lines 48.32%
1 parent 02c334a commit 411cc3d

File tree

1 file changed

+80
-5
lines changed

1 file changed

+80
-5
lines changed
 

‎packages/vite/src/node/__tests__/build.spec.ts

+80-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { resolve } from 'node:path'
22
import { fileURLToPath } from 'node:url'
3+
import colors from 'picocolors'
34
import type { Logger } from 'vite'
45
import { describe, expect, test, vi } from 'vitest'
56
import type { OutputOptions } from 'rollup'
@@ -350,18 +351,92 @@ describe('resolveBuildOutputs', () => {
350351
name: 'entryA'
351352
}
352353

353-
const outputs = resolveBuildOutputs(undefined, libOptions, {} as Logger)
354-
355-
expect(outputs).toEqual([{ format: 'es' }, { format: 'umd' }])
354+
expect(resolveBuildOutputs(undefined, libOptions, {} as Logger)).toEqual([
355+
{ format: 'es' },
356+
{ format: 'umd' }
357+
])
358+
expect(
359+
resolveBuildOutputs({ name: 'A' }, libOptions, {} as Logger)
360+
).toEqual([
361+
{ format: 'es', name: 'A' },
362+
{ format: 'umd', name: 'A' }
363+
])
364+
expect(
365+
resolveBuildOutputs([{ name: 'A' }], libOptions, {} as Logger)
366+
).toEqual([{ name: 'A' }])
356367
})
357368

358369
test('default format: multiple entries', () => {
359370
const libOptions: LibraryOptions = {
360371
entry: ['entryA.js', 'entryB.js']
361372
}
362373

363-
const outputs = resolveBuildOutputs(undefined, libOptions, {} as Logger)
374+
expect(resolveBuildOutputs(undefined, libOptions, {} as Logger)).toEqual([
375+
{ format: 'es' },
376+
{ format: 'cjs' }
377+
])
378+
expect(
379+
resolveBuildOutputs({ name: 'A' }, libOptions, {} as Logger)
380+
).toEqual([
381+
{ format: 'es', name: 'A' },
382+
{ format: 'cjs', name: 'A' }
383+
])
384+
expect(
385+
resolveBuildOutputs([{ name: 'A' }], libOptions, {} as Logger)
386+
).toEqual([{ name: 'A' }])
387+
})
388+
389+
test('umd or iife: should not support multiple entries', () => {
390+
;['umd', 'iife'].forEach((format) => {
391+
expect(() =>
392+
resolveBuildOutputs(
393+
undefined,
394+
{
395+
entry: ['entryA.js', 'entryB.js'],
396+
formats: [format as LibraryFormats]
397+
},
398+
{} as Logger
399+
)
400+
).toThrow(
401+
`Multiple entry points are not supported when output formats include "umd" or "iife".`
402+
)
403+
})
404+
})
364405

365-
expect(outputs).toEqual([{ format: 'es' }, { format: 'cjs' }])
406+
test('umd or iife: should define build.lib.name', () => {
407+
;['umd', 'iife'].forEach((format) => {
408+
expect(() =>
409+
resolveBuildOutputs(
410+
undefined,
411+
{
412+
entry: 'entryA.js',
413+
formats: [format as LibraryFormats]
414+
},
415+
{} as Logger
416+
)
417+
).toThrow(
418+
`Option "build.lib.name" is required when output formats include "umd" or "iife".`
419+
)
420+
})
421+
})
422+
423+
test('array outputs: should ignore build.lib.formats', () => {
424+
// @ts-expect-error mock Logger
425+
const log = { warn: vi.fn() } as Logger
426+
expect(
427+
resolveBuildOutputs(
428+
[{ name: 'A' }],
429+
{
430+
entry: 'entryA.js',
431+
formats: ['es']
432+
},
433+
log
434+
)
435+
).toEqual([{ name: 'A' }])
436+
expect(log.warn).toHaveBeenLastCalledWith(
437+
colors.yellow(
438+
`"build.lib.formats" will be ignored because "build.rollupOptions.output" is already an array format.`
439+
)
440+
)
366441
})
367442
})

0 commit comments

Comments
 (0)
Please sign in to comment.