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 isObject from 'lodash/isObject'
import uniq from 'lodash/uniq' import uniq from 'lodash/uniq'
import consola from 'consola' 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' import { getDefaultNuxtConfig } from './config'
export function getNuxtConfig(_options) { export function getNuxtConfig(_options) {
@ -113,7 +113,7 @@ export function getNuxtConfig(_options) {
// Populate modulesDir // Populate modulesDir
options.modulesDir = uniq( options.modulesDir = uniq(
require.main.paths.concat( getMainModule().paths.concat(
[].concat(options.modulesDir).map(dir => path.resolve(options.rootDir, dir)) [].concat(options.modulesDir).map(dir => path.resolve(options.rootDir, dir))
) )
) )

View File

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

View File

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