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' ) ,
2020-06-03 19:34:10 +00:00
getMainModule : ( ) => ( { paths : [ '/var/nuxt/node_modules' ] } ) ,
getPKG : ( ) => ( { name : 'fake' } )
2019-01-14 20:31:39 +00:00
} ) )
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' )
2019-01-19 12:00:51 +00:00
jest . spyOn ( path , 'resolve' ) . mockImplementation ( ( ... args ) => args . join ( '/' ) . replace ( /\\+/ , '/' ) )
2019-05-06 13:59:37 +00:00
jest . spyOn ( path , 'join' ) . mockImplementation ( ( ... args ) => args . join ( '/' ) . replace ( /\\+/ , '/' ) )
2019-01-14 19:47:00 +00:00
2020-05-07 19:08:01 +00:00
expect ( getNuxtConfig ( { generate : { staticAssets : { version : 'x' } } } ) ) . toMatchSnapshot ( )
2019-01-14 19:47:00 +00:00
process . cwd . mockRestore ( )
2019-01-19 12:00:51 +00:00
path . resolve . mockRestore ( )
2019-05-06 13:59:37 +00:00
path . join . 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
} )
} )
2019-04-20 10:01:59 +00:00
test ( '[Compatibility] should transform transition to pageTransition' , ( ) => {
const { pageTransition , transition } = getNuxtConfig ( {
transition : 'test-tran'
} )
expect ( pageTransition ) . toMatchObject ( { name : 'test-tran' } )
expect ( transition ) . toBeUndefined ( )
} )
test ( 'should transform pageTransition/layoutTransition to name' , ( ) => {
const { pageTransition , layoutTransition } = getNuxtConfig ( {
pageTransition : 'test-tran' ,
2019-01-14 17:40:10 +00:00
layoutTransition : 'test-layout-tran'
} )
2019-04-20 10:01:59 +00:00
expect ( pageTransition ) . toMatchObject ( { name : 'test-tran' } )
2019-01-14 17:40:10 +00:00
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-07-24 07:26:44 +00:00
expect ( extensions ) . toEqual ( [ 'js' , 'mjs' , 'ext' ] )
2019-01-14 17:40:10 +00:00
} )
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 )
} )
2019-09-20 18:49:16 +00:00
test ( 'should unset and warn when etag.hash not a function' , ( ) => {
const { render : { etag } } = getNuxtConfig ( { render : { etag : { hash : true } } } )
expect ( etag ) . toMatchObject ( { hash : undefined } )
expect ( consola . warn ) . not . toHaveBeenCalledWith ( 'render.etag.hash should be a function, received boolean instead' )
const { render : { etag : etagDev } } = getNuxtConfig ( { dev : true , render : { etag : { hash : true } } } )
expect ( etagDev ) . toMatchObject ( { hash : undefined } )
expect ( consola . warn ) . toHaveBeenCalledWith ( 'render.etag.hash should be a function, received boolean instead' )
} )
2019-01-14 17:40:10 +00:00
test ( 'should enable csp' , ( ) => {
2019-12-16 20:16:51 +00:00
const { render : { csp } } = getNuxtConfig ( { render : { csp : { allowedSources : [ '/nuxt/*' ] , test : true } } } )
2019-01-14 17:40:10 +00:00
expect ( csp ) . toEqual ( {
hashAlgorithm : 'sha256' ,
2019-04-01 19:59:58 +00:00
addMeta : false ,
2019-10-19 08:15:07 +00:00
unsafeInlineCompatibility : false ,
2019-12-16 20:16:51 +00:00
allowedSources : [ '/nuxt/*' ] ,
2019-10-19 08:15:07 +00:00
policies : undefined ,
reportOnly : false ,
test : true
} )
} )
// TODO: Remove this test in Nuxt 3, we will stop supporting this typo (more on: https://github.com/nuxt/nuxt.js/pull/6583)
test ( 'should enable csp with old typo property name, avoiding breaking changes' , ( ) => {
2019-12-16 20:16:51 +00:00
const { render : { csp } } = getNuxtConfig ( { render : { csp : { allowedSources : [ '/nuxt/*' ] , test : true , unsafeInlineCompatiblity : true } } } )
2019-10-19 08:15:07 +00:00
expect ( csp ) . toEqual ( {
hashAlgorithm : 'sha256' ,
addMeta : false ,
unsafeInlineCompatibility : true ,
2019-12-16 20:16:51 +00:00
allowedSources : [ '/nuxt/*' ] ,
2019-01-14 17:40:10 +00:00
policies : undefined ,
reportOnly : false ,
test : true
} )
} )
2020-05-07 19:08:01 +00:00
test ( 'should fallback to server target' , ( ) => {
const { target } = getNuxtConfig ( { target : 0 } )
expect ( target ) . toEqual ( 'server' )
} )
test ( 'should check unknown target' , ( ) => {
const { target } = getNuxtConfig ( { target : 'test' } )
expect ( consola . warn ) . toHaveBeenCalledWith ( 'Unknown target: test. Falling back to server' )
expect ( target ) . toEqual ( 'server' )
} )
2019-01-14 17:40:10 +00:00
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 )
} )
2019-04-20 10:01:59 +00:00
test ( 'should add appear true in pageTransition when no ssr' , ( ) => {
const { pageTransition } = getNuxtConfig ( { render : { ssr : false } } )
expect ( pageTransition . appear ) . toEqual ( true )
2019-01-19 12:00:51 +00:00
} )
2019-08-25 22:41:32 +00:00
test ( 'should return 200.html as default generate.fallback' , ( ) => {
const { generate : { fallback } } = getNuxtConfig ( { } )
expect ( fallback ) . toEqual ( '200.html' )
} )
test ( 'should return 404.html when generate.fallback is true' , ( ) => {
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' )
} )
2019-08-25 22:41:32 +00:00
test ( 'should return fallback html when generate.fallback is string' , ( ) => {
const { generate : { fallback } } = getNuxtConfig ( { generate : { fallback : 'fallback.html' } } )
expect ( fallback ) . toEqual ( 'fallback.html' )
} )
2020-05-20 15:43:25 +00:00
test ( 'export should alias to generate' , ( ) => {
const { generate : { fallback } } = getNuxtConfig ( { export : { fallback : 'fallback.html' } } )
expect ( fallback ) . toEqual ( 'fallback.html' )
} )
2019-02-13 10:32:13 +00:00
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 ( {
2019-03-24 17:44:22 +00:00
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 ( {
2019-03-24 17:44:22 +00:00
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' )
} )
2019-08-09 10:32:53 +00:00
test ( 'should deprecate devModules' , ( ) => {
const config = getNuxtConfig ( { devModules : [ 'foo' ] , buildModules : [ 'bar' ] } )
expect ( consola . warn ) . toHaveBeenCalledWith ( '`devModules` has been renamed to `buildModules` and will be removed in Nuxt 3.' )
expect ( config . devModules ) . toBe ( undefined )
expect ( config . buildModules ) . toEqual ( [ 'bar' , 'foo' ] )
} )
2019-01-14 17:40:10 +00:00
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.' )
} )
2020-04-07 09:38:49 +00:00
test ( 'should deprecate build.crossorigin' , ( ) => {
getNuxtConfig ( { build : { crossorigin : 'use-credentials' } } )
expect ( consola . warn ) . toHaveBeenCalledWith ( 'Using `build.crossorigin` is deprecated and will be removed in Nuxt 3. Please use `render.crossorigin` instead.' )
} )
2019-01-14 17:40:10 +00:00
} )
} )
2019-04-05 14:15:58 +00:00
2019-09-29 09:11:25 +00:00
describe ( 'config: serverMiddleware' , ( ) => {
test ( 'should transform serverMiddleware hash' , ( ) => {
const serverMiddleware = {
'/resource' : ( req , res , next ) => {
}
}
const config = getNuxtConfig ( { serverMiddleware } )
expect ( config . serverMiddleware [ 0 ] . path ) . toBe ( '/resource' )
expect ( config . serverMiddleware [ 0 ] . handler ) . toBe ( serverMiddleware [ '/resource' ] )
} )
} )
2019-04-05 14:15:58 +00:00
describe ( 'config: router' , ( ) => {
test ( 'should sanitize router.base' , ( ) => {
const config = getNuxtConfig ( { router : { base : '/foo' } } )
expect ( config . router . base ) . toBe ( '/foo/' )
} )
} )