test: mock require.main

This commit is contained in:
Clark Du 2019-01-14 20:31:39 +00:00
parent 8a11bb8bea
commit e594f09914
No known key found for this signature in database
GPG Key ID: D0E5986AF78B86D9
4 changed files with 41 additions and 32 deletions

View File

@ -6,7 +6,7 @@ import pick from 'lodash/pick'
import isObject from 'lodash/isObject'
import uniq from 'lodash/uniq'
import consola from 'consola'
import { guardDir, isNonEmptyString, isPureObject, isUrl } from '@nuxt/utils'
import { guardDir, isNonEmptyString, isPureObject, isUrl, getMainModule } from '@nuxt/utils'
import { getDefaultNuxtConfig } from './config'
export function getNuxtConfig(_options) {
@ -113,7 +113,7 @@ export function getNuxtConfig(_options) {
// Populate modulesDir
options.modulesDir = uniq(
require.main.paths.concat(
getMainModule().paths.concat(
[].concat(options.modulesDir).map(dir => path.resolve(options.rootDir, dir))
)
)

View File

@ -240,6 +240,10 @@ Object {
},
},
"modules": Array [],
"modulesDir": Array [
"/var/nuxt/node_modules",
"/var/nuxt/test/node_modules",
],
"plugins": Array [],
"render": Object {
"bundleRenderer": Object {

View File

@ -1,6 +1,6 @@
import path from 'path'
import consola from 'consola'
import * as options from '../src/options'
import { getNuxtConfig } from '../src/options'
jest.mock('std-env', () => ({
browser: false,
@ -14,28 +14,31 @@ jest.mock('std-env', () => ({
linux: true
}))
jest.mock('@nuxt/utils', () => ({
...jest.requireActual('@nuxt/utils'),
getMainModule: () => ({ paths: ['/var/nuxt/node_modules'] })
}))
describe('config: options', () => {
test('should return default nuxt config', () => {
jest.spyOn(process, 'cwd').mockReturnValue('/var/nuxt/test')
const config = options.getNuxtConfig({})
delete config.modulesDir
expect(config).toMatchSnapshot()
expect(getNuxtConfig({})).toMatchSnapshot()
process.cwd.mockRestore()
})
test('should prevent duplicate calls with same options', () => {
const options = {}
const firstConfig = options.getNuxtConfig(options)
const secondConfig = options.getNuxtConfig(firstConfig)
const firstConfig = getNuxtConfig(options)
const secondConfig = getNuxtConfig(firstConfig)
expect(firstConfig).toBe(secondConfig)
expect(firstConfig.__normalized__).toBe(true)
})
test('should return default loading when loading is true', () => {
const { loading } = options.getNuxtConfig({ loading: true })
const { loading } = getNuxtConfig({ loading: true })
expect(loading).toEqual({
color: 'black',
failedColor: 'red',
@ -49,7 +52,7 @@ describe('config: options', () => {
})
test('should transform transition/layoutTransition to name', () => {
const { transition, layoutTransition } = options.getNuxtConfig({
const { transition, layoutTransition } = getNuxtConfig({
transition: 'test-tran',
layoutTransition: 'test-layout-tran'
})
@ -58,22 +61,22 @@ describe('config: options', () => {
})
test('should transform extensions to array', () => {
const { extensions } = options.getNuxtConfig({ extensions: 'ext' })
const { extensions } = getNuxtConfig({ extensions: 'ext' })
expect(extensions).toEqual(['js', 'mjs', 'ts', 'ext'])
})
test('should support custom global name', () => {
const { globalName } = options.getNuxtConfig({ globalName: 'globalNuxt' })
const { globalName } = getNuxtConfig({ globalName: 'globalNuxt' })
expect(globalName).toEqual('globalnuxt')
})
test('should detect store dir', () => {
const { store } = options.getNuxtConfig({ rootDir: path.resolve(__dirname, 'fixtures') })
const { store } = getNuxtConfig({ rootDir: path.resolve(__dirname, 'fixtures') })
expect(store).toEqual(true)
})
test('should enable csp', () => {
const { render: { csp } } = options.getNuxtConfig({ render: { csp: { allowedSources: true, test: true } } })
const { render: { csp } } = getNuxtConfig({ render: { csp: { allowedSources: true, test: true } } })
expect(csp).toEqual({
hashAlgorithm: 'sha256',
allowedSources: true,
@ -84,39 +87,39 @@ describe('config: options', () => {
})
test('should check unknown mode', () => {
const { build, render } = options.getNuxtConfig({ mode: 'test' })
const { build, render } = getNuxtConfig({ mode: 'test' })
expect(consola.warn).toHaveBeenCalledWith('Unknown mode: test. Falling back to universal')
expect(build.ssr).toEqual(true)
expect(render.ssr).toEqual(true)
})
test('should return 404.html as default generate.fallback', () => {
const { generate: { fallback } } = options.getNuxtConfig({ generate: { fallback: true } })
const { generate: { fallback } } = getNuxtConfig({ generate: { fallback: true } })
expect(fallback).toEqual('404.html')
})
describe('config: router dir', () => {
test('should transform middleware to array', () => {
const { router: { middleware } } = options.getNuxtConfig({ router: { middleware: 'midd' } })
const { router: { middleware } } = getNuxtConfig({ router: { middleware: 'midd' } })
expect(middleware).toEqual(['midd'])
})
test('should set _routerBaseSpecified when base is specified', () => {
const { _routerBaseSpecified } = options.getNuxtConfig({ router: { base: '/test' } })
const { _routerBaseSpecified } = getNuxtConfig({ router: { base: '/test' } })
expect(_routerBaseSpecified).toEqual(true)
})
})
describe('config: options dir', () => {
test('should support custom root dir', () => {
const { rootDir } = options.getNuxtConfig({
const { rootDir } = getNuxtConfig({
rootDir: 'root'
})
expect(rootDir).toEqual(path.resolve('root'))
})
test('should support custom src dir', () => {
const { srcDir } = options.getNuxtConfig({
const { srcDir } = getNuxtConfig({
rootDir: 'root',
srcDir: 'src'
})
@ -124,7 +127,7 @@ describe('config: options', () => {
})
test('should support custom generate dir', () => {
const { generate: { dir } } = options.getNuxtConfig({
const { generate: { dir } } = getNuxtConfig({
rootDir: 'root',
generate: { dir: 'generate' }
})
@ -134,41 +137,41 @@ describe('config: options', () => {
describe('config: options template', () => {
test('should use default appTemplatePath', () => {
const { appTemplatePath } = options.getNuxtConfig({})
const { appTemplatePath } = getNuxtConfig({})
expect(appTemplatePath).toEqual(path.resolve('.nuxt', 'views', 'app.template.html'))
})
test('should use custom appTemplatePath', () => {
const { appTemplatePath } = options.getNuxtConfig({ appTemplatePath: 'templates' })
const { appTemplatePath } = getNuxtConfig({ appTemplatePath: 'templates' })
expect(appTemplatePath).toEqual(path.resolve('templates'))
})
test('should use custom app.html', () => {
const { appTemplatePath } = options.getNuxtConfig({ rootDir: path.resolve(__dirname, 'fixtures') })
const { appTemplatePath } = getNuxtConfig({ rootDir: path.resolve(__dirname, 'fixtures') })
expect(appTemplatePath).toEqual(path.resolve(__dirname, 'fixtures', 'app.html'))
})
})
describe('config: options publicPath', () => {
test('should fallback to default when publicPath is falsy', () => {
const { build: { publicPath } } = options.getNuxtConfig({ build: { publicPath: false } })
const { build: { publicPath } } = getNuxtConfig({ build: { publicPath: false } })
expect(publicPath).toEqual('/_nuxt/')
})
test('should append slash in publicPath', () => {
const { build: { publicPath } } = options.getNuxtConfig({ build: { publicPath: '/nuxt_public' } })
const { build: { publicPath } } = getNuxtConfig({ build: { publicPath: '/nuxt_public' } })
expect(publicPath).toEqual('/nuxt_public/')
})
test('should ignore url publicPath in dev', () => {
const { build: { publicPath } } = options.getNuxtConfig({ dev: true, build: { publicPath: 'http://nuxt_public' } })
const { build: { publicPath } } = getNuxtConfig({ dev: true, build: { publicPath: 'http://nuxt_public' } })
expect(publicPath).toEqual('/_nuxt/')
})
})
describe('config: options babel', () => {
test('should replace and deprecate @nuxtjs/babel-preset-app', () => {
const { build: { babel } } = options.getNuxtConfig({
const { build: { babel } } = getNuxtConfig({
build: { babel: { presets: ['@nuxtjs/babel-preset-app'] } }
})
expect(consola.warn).toHaveBeenCalledWith('@nuxtjs/babel-preset-app has been deprecated, please use @nuxt/babel-preset-app.')
@ -180,7 +183,7 @@ describe('config: options', () => {
})
test('should support options in babel presets', () => {
const { build: { babel } } = options.getNuxtConfig({
const { build: { babel } } = getNuxtConfig({
build: { babel: { presets: [['@nuxt/babel-preset-app', { test: true }]] } }
})
expect(babel).toEqual({
@ -193,17 +196,17 @@ describe('config: options', () => {
describe('config: options deprecated', () => {
test('should deprecate render.gzip', () => {
options.getNuxtConfig({ render: { gzip: true } })
getNuxtConfig({ render: { gzip: true } })
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', () => {
options.getNuxtConfig({ build: { vendor: ['lodash'] } })
getNuxtConfig({ build: { vendor: ['lodash'] } })
expect(consola.warn).toHaveBeenCalledWith('vendor has been deprecated due to webpack4 optimization')
})
test('should deprecate build.extractCSS.allChunks', () => {
options.getNuxtConfig({ build: { extractCSS: { allChunks: true } } })
getNuxtConfig({ build: { extractCSS: { allChunks: true } } })
expect(consola.warn).toHaveBeenCalledWith('build.extractCSS.allChunks has no effect from v2.0.0. Please use build.optimization.splitChunks settings instead.')
})
})

View File

@ -108,3 +108,5 @@ export function isIndexFileAndFolder(pluginFiles) {
}
return pluginFiles.some(isIndex)
}
export const getMainModule = () => require.main