Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve node/build.ts ut coverage #10786

Merged
merged 4 commits into from
Nov 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 80 additions & 5 deletions packages/vite/src/node/__tests__/build.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import colors from 'picocolors'
import type { Logger } from 'vite'
import { describe, expect, test, vi } from 'vitest'
import type { OutputOptions } from 'rollup'
Expand Down Expand Up @@ -350,18 +351,92 @@ describe('resolveBuildOutputs', () => {
name: 'entryA'
}

const outputs = resolveBuildOutputs(undefined, libOptions, {} as Logger)

expect(outputs).toEqual([{ format: 'es' }, { format: 'umd' }])
expect(resolveBuildOutputs(undefined, libOptions, {} as Logger)).toEqual([
{ format: 'es' },
{ format: 'umd' }
])
expect(
resolveBuildOutputs({ name: 'A' }, libOptions, {} as Logger)
).toEqual([
{ format: 'es', name: 'A' },
{ format: 'umd', name: 'A' }
])
expect(
resolveBuildOutputs([{ name: 'A' }], libOptions, {} as Logger)
).toEqual([{ name: 'A' }])
})

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

const outputs = resolveBuildOutputs(undefined, libOptions, {} as Logger)
expect(resolveBuildOutputs(undefined, libOptions, {} as Logger)).toEqual([
{ format: 'es' },
{ format: 'cjs' }
])
expect(
resolveBuildOutputs({ name: 'A' }, libOptions, {} as Logger)
).toEqual([
{ format: 'es', name: 'A' },
{ format: 'cjs', name: 'A' }
])
expect(
resolveBuildOutputs([{ name: 'A' }], libOptions, {} as Logger)
).toEqual([{ name: 'A' }])
})

test('umd or iife: should not support multiple entries', () => {
;['umd', 'iife'].forEach((format) => {
expect(() =>
resolveBuildOutputs(
undefined,
{
entry: ['entryA.js', 'entryB.js'],
formats: [format as LibraryFormats]
},
{} as Logger
)
).toThrow(
`Multiple entry points are not supported when output formats include "umd" or "iife".`
)
})
})

expect(outputs).toEqual([{ format: 'es' }, { format: 'cjs' }])
test('umd or iife: should define build.lib.name', () => {
;['umd', 'iife'].forEach((format) => {
expect(() =>
resolveBuildOutputs(
undefined,
{
entry: 'entryA.js',
formats: [format as LibraryFormats]
},
{} as Logger
)
).toThrow(
`Option "build.lib.name" is required when output formats include "umd" or "iife".`
)
})
})

test('array outputs: should ignore build.lib.formats', () => {
// @ts-expect-error mock Logger
const log = { warn: vi.fn() } as Logger
expect(
resolveBuildOutputs(
[{ name: 'A' }],
{
entry: 'entryA.js',
formats: ['es']
},
log
)
).toEqual([{ name: 'A' }])
expect(log.warn).toHaveBeenLastCalledWith(
colors.yellow(
`"build.lib.formats" will be ignored because "build.rollupOptions.output" is already an array format.`
)
)
})
})