2018-10-30 20:42:53 +00:00
|
|
|
import { getDefaultNuxtConfig } from '@nuxt/config'
|
2018-10-25 07:43:42 +00:00
|
|
|
import { consola } from '../utils'
|
|
|
|
import * as utils from '../../src/utils'
|
2018-11-08 09:15:56 +00:00
|
|
|
import * as fmt from '../../src/utils/formatting'
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
describe('cli/utils', () => {
|
2018-10-29 22:16:16 +00:00
|
|
|
afterEach(() => jest.resetAllMocks())
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
test('loadNuxtConfig: defaults', async () => {
|
|
|
|
const argv = {
|
|
|
|
_: ['.'],
|
|
|
|
'config-file': 'nuxt.config.js',
|
|
|
|
universal: true
|
|
|
|
}
|
|
|
|
|
|
|
|
const options = await utils.loadNuxtConfig(argv)
|
|
|
|
expect(options.rootDir).toBe(process.cwd())
|
|
|
|
expect(options.mode).toBe('universal')
|
|
|
|
expect(options.server.host).toBe('localhost')
|
|
|
|
expect(options.server.port).toBe(3000)
|
|
|
|
expect(options.server.socket).not.toBeDefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('loadNuxtConfig: config-file', async () => {
|
|
|
|
const argv = {
|
|
|
|
_: [__dirname],
|
|
|
|
'config-file': '../fixtures/nuxt.config.js',
|
|
|
|
spa: true
|
|
|
|
}
|
|
|
|
|
|
|
|
const options = await utils.loadNuxtConfig(argv)
|
|
|
|
expect(options.testOption).toBe(true)
|
|
|
|
expect(options.rootDir).toBe('/some/path')
|
|
|
|
expect(options.mode).toBe('spa')
|
|
|
|
expect(options.server.host).toBe('nuxt-host')
|
|
|
|
expect(options.server.port).toBe(3001)
|
|
|
|
expect(options.server.socket).toBe('/var/run/nuxt.sock')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('loadNuxtConfig: not-existing config-file', async () => {
|
|
|
|
const argv = {
|
|
|
|
_: [__dirname],
|
|
|
|
'config-file': '../fixtures/nuxt.doesnt-exist.js'
|
|
|
|
}
|
|
|
|
|
|
|
|
const options = await utils.loadNuxtConfig(argv)
|
|
|
|
expect(options.testOption).not.toBeDefined()
|
|
|
|
|
|
|
|
expect(consola.fatal).toHaveBeenCalledTimes(1)
|
|
|
|
expect(consola.fatal).toHaveBeenCalledWith(expect.stringMatching(/Could not load config file/))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('loadNuxtConfig: async config-file', async () => {
|
|
|
|
const argv = {
|
|
|
|
_: [__dirname],
|
|
|
|
'config-file': '../fixtures/nuxt.async-config.js',
|
|
|
|
hostname: 'async-host',
|
|
|
|
port: 3002,
|
|
|
|
'unix-socket': '/var/run/async.sock'
|
|
|
|
}
|
|
|
|
|
|
|
|
const options = await utils.loadNuxtConfig(argv)
|
|
|
|
expect(options.testOption).toBe(true)
|
|
|
|
expect(options.mode).toBe('supercharged')
|
|
|
|
expect(options.server.host).toBe('async-host')
|
|
|
|
expect(options.server.port).toBe(3002)
|
|
|
|
expect(options.server.socket).toBe('/var/run/async.sock')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('loadNuxtConfig: async config-file with error', async () => {
|
|
|
|
const argv = {
|
|
|
|
_: [__dirname],
|
|
|
|
'config-file': '../fixtures/nuxt.async-error.js'
|
|
|
|
}
|
|
|
|
|
|
|
|
const options = await utils.loadNuxtConfig(argv)
|
|
|
|
expect(options.testOption).not.toBeDefined()
|
|
|
|
|
|
|
|
expect(consola.error).toHaveBeenCalledTimes(1)
|
|
|
|
expect(consola.error).toHaveBeenCalledWith(new Error('Async Config Error'))
|
|
|
|
expect(consola.fatal).toHaveBeenCalledWith('Error while fetching async configuration')
|
|
|
|
})
|
|
|
|
|
2018-11-22 15:48:26 +00:00
|
|
|
test('normalizeArg: normalize string argument in command', () => {
|
|
|
|
expect(utils.normalizeArg('true')).toBe(true)
|
|
|
|
expect(utils.normalizeArg('false')).toBe(false)
|
|
|
|
expect(utils.normalizeArg(true)).toBe(true)
|
|
|
|
expect(utils.normalizeArg(false)).toBe(false)
|
|
|
|
expect(utils.normalizeArg('')).toBe(true)
|
|
|
|
expect(utils.normalizeArg(undefined, 'default')).toBe('default')
|
|
|
|
expect(utils.normalizeArg('text')).toBe('text')
|
|
|
|
})
|
|
|
|
|
2018-10-27 16:48:23 +00:00
|
|
|
test('nuxtServerConfig: server env', () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const options = getDefaultNuxtConfig({
|
|
|
|
env: {
|
2018-10-27 16:48:23 +00:00
|
|
|
...process.env,
|
|
|
|
HOST: 'env-host',
|
|
|
|
PORT: 3003,
|
|
|
|
UNIX_SOCKET: '/var/run/env.sock'
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
})
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
expect(options.server.host).toBe('env-host')
|
2018-10-27 16:48:23 +00:00
|
|
|
expect(options.server.port).toBe(3003)
|
2018-10-25 07:43:42 +00:00
|
|
|
expect(options.server.socket).toBe('/var/run/env.sock')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('indent', () => {
|
2018-11-01 03:53:06 +00:00
|
|
|
expect(fmt.indent(4)).toBe(' ')
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('indent custom char', () => {
|
2018-11-01 03:53:06 +00:00
|
|
|
expect(fmt.indent(4, '-')).toBe('----')
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|
|
|
|
})
|