Nuxt/packages/config/test/options.test.js

237 lines
8.0 KiB
JavaScript
Raw Normal View History

2019-01-14 17:40:10 +00:00
import path from 'path'
import consola from 'consola'
2019-01-14 20:31:39 +00:00
import { getNuxtConfig } from '../src/options'
2019-01-14 19:47:00 +00:00
jest.mock('std-env', () => ({
browser: false,
test: 'test',
dev: false,
production: true,
debug: false,
ci: true,
windows: false,
darwin: false,
linux: true
}))
2019-01-14 17:40:10 +00:00
2019-01-14 20:31:39 +00:00
jest.mock('@nuxt/utils', () => ({
...jest.requireActual('@nuxt/utils'),
getMainModule: () => ({ paths: ['/var/nuxt/node_modules'] })
}))
2019-01-14 17:40:10 +00:00
describe('config: options', () => {
test('should return default nuxt config', () => {
2019-01-14 19:47:00 +00:00
jest.spyOn(process, 'cwd').mockReturnValue('/var/nuxt/test')
jest.spyOn(path, 'resolve').mockImplementation((...args) => args.join('/').replace(/\\+/, '/'))
2019-01-14 19:47:00 +00:00
2019-01-14 20:31:39 +00:00
expect(getNuxtConfig({})).toMatchSnapshot()
2019-01-14 19:47:00 +00:00
process.cwd.mockRestore()
path.resolve.mockRestore()
2019-01-14 17:40:10 +00:00
})
test('should prevent duplicate calls with same options', () => {
const options = {}
2019-01-14 20:31:39 +00:00
const firstConfig = getNuxtConfig(options)
const secondConfig = getNuxtConfig(firstConfig)
2019-01-14 17:40:10 +00:00
expect(firstConfig).toBe(secondConfig)
expect(firstConfig.__normalized__).toBe(true)
})
test('should return default loading when loading is true', () => {
2019-01-14 20:31:39 +00:00
const { loading } = getNuxtConfig({ loading: true })
2019-01-14 17:40:10 +00:00
expect(loading).toEqual({
color: 'black',
failedColor: 'red',
height: '2px',
throttle: 200,
duration: 5000,
continuous: false,
rtl: false,
css: true
})
})
test('should transform transition/layoutTransition to name', () => {
2019-01-14 20:31:39 +00:00
const { transition, layoutTransition } = getNuxtConfig({
2019-01-14 17:40:10 +00:00
transition: 'test-tran',
layoutTransition: 'test-layout-tran'
})
expect(transition).toMatchObject({ name: 'test-tran' })
expect(layoutTransition).toMatchObject({ name: 'test-layout-tran' })
})
test('should transform extensions to array', () => {
2019-01-14 20:31:39 +00:00
const { extensions } = getNuxtConfig({ extensions: 'ext' })
2019-01-14 17:40:10 +00:00
expect(extensions).toEqual(['js', 'mjs', 'ts', 'ext'])
})
test('should support custom global name', () => {
2019-01-14 20:31:39 +00:00
const { globalName } = getNuxtConfig({ globalName: 'globalNuxt' })
2019-01-14 17:40:10 +00:00
expect(globalName).toEqual('globalnuxt')
})
test('should detect store dir', () => {
2019-01-14 20:31:39 +00:00
const { store } = getNuxtConfig({ rootDir: path.resolve(__dirname, 'fixtures') })
2019-01-14 17:40:10 +00:00
expect(store).toEqual(true)
})
test('should enable csp', () => {
2019-01-14 20:31:39 +00:00
const { render: { csp } } = getNuxtConfig({ render: { csp: { allowedSources: true, test: true } } })
2019-01-14 17:40:10 +00:00
expect(csp).toEqual({
hashAlgorithm: 'sha256',
addMeta: false,
2019-01-14 17:40:10 +00:00
allowedSources: true,
policies: undefined,
reportOnly: false,
test: true
})
})
test('should check unknown mode', () => {
2019-01-14 20:31:39 +00:00
const { build, render } = getNuxtConfig({ mode: 'test' })
2019-01-14 17:40:10 +00:00
expect(consola.warn).toHaveBeenCalledWith('Unknown mode: test. Falling back to universal')
expect(build.ssr).toEqual(true)
expect(render.ssr).toEqual(true)
})
test('should add appear true in transition when no ssr', () => {
const { transition } = getNuxtConfig({ render: { ssr: false } })
expect(transition.appear).toEqual(true)
})
2019-01-14 17:40:10 +00:00
test('should return 404.html as default generate.fallback', () => {
2019-01-14 20:31:39 +00:00
const { generate: { fallback } } = getNuxtConfig({ generate: { fallback: true } })
2019-01-14 17:40:10 +00:00
expect(fallback).toEqual('404.html')
})
test('should disable parallel if extractCSS is enabled', () => {
const { build: { parallel } } = getNuxtConfig({ build: { extractCSS: true, parallel: true } })
expect(parallel).toEqual(false)
expect(consola.warn).toHaveBeenCalledWith('extractCSS cannot work with parallel build due to limited work pool in thread-loader')
})
2019-01-14 17:40:10 +00:00
describe('config: router dir', () => {
test('should transform middleware to array', () => {
2019-01-14 20:31:39 +00:00
const { router: { middleware } } = getNuxtConfig({ router: { middleware: 'midd' } })
2019-01-14 17:40:10 +00:00
expect(middleware).toEqual(['midd'])
})
test('should set _routerBaseSpecified when base is specified', () => {
2019-01-14 20:31:39 +00:00
const { _routerBaseSpecified } = getNuxtConfig({ router: { base: '/test' } })
2019-01-14 17:40:10 +00:00
expect(_routerBaseSpecified).toEqual(true)
})
})
describe('config: options dir', () => {
test('should support custom root dir', () => {
2019-01-14 20:31:39 +00:00
const { rootDir } = getNuxtConfig({
2019-01-14 17:40:10 +00:00
rootDir: 'root'
})
expect(rootDir).toEqual(path.resolve('root'))
})
test('should support custom src dir', () => {
2019-01-14 20:31:39 +00:00
const { srcDir } = getNuxtConfig({
2019-01-14 17:40:10 +00:00
rootDir: 'root',
srcDir: 'src'
})
expect(srcDir).toEqual(path.resolve('root', 'src'))
})
test('should support custom generate dir', () => {
2019-01-14 20:31:39 +00:00
const { generate: { dir } } = getNuxtConfig({
2019-01-14 17:40:10 +00:00
rootDir: 'root',
generate: { dir: 'generate' }
})
expect(dir).toEqual(path.resolve('root', 'generate'))
})
})
describe('config: options template', () => {
test('should use default appTemplatePath', () => {
2019-01-14 20:31:39 +00:00
const { appTemplatePath } = getNuxtConfig({})
2019-01-14 17:40:10 +00:00
expect(appTemplatePath).toEqual(path.resolve('.nuxt', 'views', 'app.template.html'))
})
test('should use custom appTemplatePath', () => {
2019-01-14 20:31:39 +00:00
const { appTemplatePath } = getNuxtConfig({ appTemplatePath: 'templates' })
2019-01-14 17:40:10 +00:00
expect(appTemplatePath).toEqual(path.resolve('templates'))
})
test('should use custom app.html', () => {
2019-01-14 20:31:39 +00:00
const { appTemplatePath } = getNuxtConfig({ rootDir: path.resolve(__dirname, 'fixtures') })
2019-01-14 17:40:10 +00:00
expect(appTemplatePath).toEqual(path.resolve(__dirname, 'fixtures', 'app.html'))
})
})
describe('config: options publicPath', () => {
test('should fallback to default when publicPath is falsy', () => {
2019-01-14 20:31:39 +00:00
const { build: { publicPath } } = getNuxtConfig({ build: { publicPath: false } })
2019-01-14 17:40:10 +00:00
expect(publicPath).toEqual('/_nuxt/')
})
test('should append slash in publicPath', () => {
2019-01-14 20:31:39 +00:00
const { build: { publicPath } } = getNuxtConfig({ build: { publicPath: '/nuxt_public' } })
2019-01-14 17:40:10 +00:00
expect(publicPath).toEqual('/nuxt_public/')
})
test('should ignore url publicPath in dev', () => {
2019-01-14 20:31:39 +00:00
const { build: { publicPath } } = getNuxtConfig({ dev: true, build: { publicPath: 'http://nuxt_public' } })
2019-01-14 17:40:10 +00:00
expect(publicPath).toEqual('/_nuxt/')
})
})
describe('config: options babel', () => {
test('should replace and deprecate @nuxtjs/babel-preset-app', () => {
2019-01-14 20:31:39 +00:00
const { build: { babel } } = getNuxtConfig({
2019-01-14 17:40:10 +00:00
build: { babel: { presets: ['@nuxtjs/babel-preset-app'] } }
})
expect(consola.warn).toHaveBeenCalledWith('@nuxtjs/babel-preset-app has been deprecated, please use @nuxt/babel-preset-app.')
expect(babel).toEqual({
configFile: false,
babelrc: false,
cacheDirectory: false,
presets: ['@nuxt/babel-preset-app']
2019-01-14 17:40:10 +00:00
})
})
test('should support options in babel presets', () => {
2019-01-14 20:31:39 +00:00
const { build: { babel } } = getNuxtConfig({
2019-01-14 17:40:10 +00:00
build: { babel: { presets: [['@nuxt/babel-preset-app', { test: true }]] } }
})
expect(babel).toEqual({
configFile: false,
babelrc: false,
cacheDirectory: false,
presets: [['@nuxt/babel-preset-app', { test: true }]]
2019-01-14 17:40:10 +00:00
})
})
})
describe('config: options deprecated', () => {
test('should deprecate render.gzip', () => {
2019-01-14 20:31:39 +00:00
getNuxtConfig({ render: { gzip: true } })
2019-01-14 17:40:10 +00:00
expect(consola.warn).toHaveBeenCalledWith('render.gzip is deprecated and will be removed in a future version! Please switch to render.compressor')
})
test('should deprecate build.vendor', () => {
2019-01-14 20:31:39 +00:00
getNuxtConfig({ build: { vendor: ['lodash'] } })
2019-01-14 17:40:10 +00:00
expect(consola.warn).toHaveBeenCalledWith('vendor has been deprecated due to webpack4 optimization')
})
test('should deprecate build.extractCSS.allChunks', () => {
2019-01-14 20:31:39 +00:00
getNuxtConfig({ build: { extractCSS: { allChunks: true } } })
2019-01-14 17:40:10 +00:00
expect(consola.warn).toHaveBeenCalledWith('build.extractCSS.allChunks has no effect from v2.0.0. Please use build.optimization.splitChunks settings instead.')
})
})
})
describe('config: router', () => {
test('should sanitize router.base', () => {
const config = getNuxtConfig({ router: { base: '/foo' } })
expect(config.router.base).toBe('/foo/')
})
})