2018-10-30 20:42:53 +00:00
|
|
|
import { getDefaultNuxtConfig } from '@nuxt/config'
|
2020-10-08 14:30:02 +00:00
|
|
|
import { TARGETS } from '@nuxt/utils'
|
2018-10-25 07:43:42 +00:00
|
|
|
import { consola } from '../utils'
|
2019-04-11 10:04:21 +00:00
|
|
|
import { loadNuxtConfig } from '../../src/utils/config'
|
2018-10-25 07:43:42 +00:00
|
|
|
import * as utils from '../../src/utils'
|
2019-04-11 10:04:21 +00:00
|
|
|
import { showBanner } from '../../src/utils/banner'
|
2019-04-12 17:19:46 +00:00
|
|
|
import { showMemoryUsage } from '../../src/utils/memory'
|
2018-11-08 09:15:56 +00:00
|
|
|
import * as fmt from '../../src/utils/formatting'
|
2018-10-25 07:43:42 +00:00
|
|
|
|
2019-03-03 08:12:46 +00:00
|
|
|
jest.mock('std-env', () => ({
|
|
|
|
test: false,
|
|
|
|
minimalCLI: false
|
|
|
|
}))
|
2019-05-30 09:25:36 +00:00
|
|
|
jest.mock('boxen', () => text => `[boxen] ${text}`)
|
2019-03-03 08:12:46 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-04-11 10:04:21 +00:00
|
|
|
const options = await loadNuxtConfig(argv)
|
2018-10-25 07:43:42 +00:00
|
|
|
expect(options.rootDir).toBe(process.cwd())
|
2020-10-08 14:30:02 +00:00
|
|
|
expect(options.mode).toBeUndefined()
|
|
|
|
expect(options.ssr).toBe(true)
|
2018-10-25 07:43:42 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-04-11 10:04:21 +00:00
|
|
|
const options = await loadNuxtConfig(argv)
|
2018-10-25 07:43:42 +00:00
|
|
|
expect(options.testOption).toBe(true)
|
|
|
|
expect(options.rootDir).toBe('/some/path')
|
2020-10-08 14:30:02 +00:00
|
|
|
expect(options.mode).toBe('supercharged')
|
|
|
|
expect(options.ssr).toBe(false)
|
2018-10-25 07:43:42 +00:00
|
|
|
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'
|
|
|
|
}
|
|
|
|
|
2019-04-11 10:04:21 +00:00
|
|
|
const options = await loadNuxtConfig(argv)
|
2018-10-25 07:43:42 +00:00
|
|
|
expect(options.testOption).not.toBeDefined()
|
|
|
|
|
|
|
|
expect(consola.fatal).toHaveBeenCalledTimes(1)
|
2020-01-19 08:36:06 +00:00
|
|
|
expect(consola.fatal).toHaveBeenCalledWith(expect.stringMatching(/Config file not found/))
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
|
2019-04-11 10:04:21 +00:00
|
|
|
const options = await loadNuxtConfig(argv)
|
2018-10-25 07:43:42 +00:00
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
2020-01-13 16:33:37 +00:00
|
|
|
test('loadNuxtConfig: passes context to config fn', async () => {
|
|
|
|
const argv = {
|
|
|
|
_: [__dirname],
|
|
|
|
'config-file': '../fixtures/nuxt.fn-config.js'
|
|
|
|
}
|
|
|
|
|
|
|
|
const context = { command: 'test', dev: true }
|
|
|
|
const options = await loadNuxtConfig(argv, context)
|
|
|
|
expect(options.context.command).toBe('test')
|
|
|
|
expect(options.context.dev).toBe(true)
|
|
|
|
})
|
|
|
|
|
2018-10-25 07:43:42 +00:00
|
|
|
test('loadNuxtConfig: async config-file with error', async () => {
|
|
|
|
const argv = {
|
|
|
|
_: [__dirname],
|
|
|
|
'config-file': '../fixtures/nuxt.async-error.js'
|
|
|
|
}
|
|
|
|
|
2019-04-11 10:04:21 +00:00
|
|
|
const options = await loadNuxtConfig(argv)
|
2018-10-25 07:43:42 +00:00
|
|
|
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
|
|
|
})
|
2019-03-03 08:12:46 +00:00
|
|
|
|
2019-04-12 17:19:46 +00:00
|
|
|
test('showBanner prints full-info box with memory usage', () => {
|
2019-03-03 08:12:46 +00:00
|
|
|
const stdout = jest.spyOn(process.stdout, 'write').mockImplementation(() => {})
|
|
|
|
const successBox = jest.fn().mockImplementation((m, t) => t + m)
|
|
|
|
jest.spyOn(fmt, 'successBox').mockImplementation(successBox)
|
|
|
|
|
2019-11-26 22:42:39 +00:00
|
|
|
const badgeMessages = ['badgeMessage']
|
2019-09-30 08:46:58 +00:00
|
|
|
const bannerColor = 'green'
|
2019-03-03 08:12:46 +00:00
|
|
|
const listeners = [
|
|
|
|
{ url: 'first' },
|
|
|
|
{ url: 'second' }
|
|
|
|
]
|
|
|
|
|
2019-04-11 10:04:21 +00:00
|
|
|
showBanner({
|
2019-03-03 08:12:46 +00:00
|
|
|
options: {
|
2020-05-07 19:08:01 +00:00
|
|
|
render: {
|
|
|
|
ssr: true
|
|
|
|
},
|
2019-03-03 08:12:46 +00:00
|
|
|
cli: {
|
2019-09-30 08:46:58 +00:00
|
|
|
badgeMessages,
|
|
|
|
bannerColor
|
2019-03-03 08:12:46 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
server: {
|
|
|
|
listeners
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
expect(successBox).toHaveBeenCalledTimes(1)
|
|
|
|
expect(stdout).toHaveBeenCalledTimes(1)
|
|
|
|
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('Nuxt.js'))
|
2020-06-17 14:37:19 +00:00
|
|
|
expect(stdout).toHaveBeenCalledWith(expect.stringMatching(`Listening: ${listeners[0].url}`))
|
|
|
|
expect(stdout).toHaveBeenCalledWith(expect.stringMatching(`Listening: ${listeners[1].url}`))
|
2019-04-12 17:19:46 +00:00
|
|
|
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('Memory usage'))
|
2019-03-03 08:12:46 +00:00
|
|
|
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('badgeMessage'))
|
|
|
|
stdout.mockRestore()
|
|
|
|
})
|
|
|
|
|
2019-04-12 17:19:46 +00:00
|
|
|
test('showBanner doesnt print memory usage', () => {
|
|
|
|
const stdout = jest.spyOn(process.stdout, 'write').mockImplementation(() => {})
|
|
|
|
const successBox = jest.fn().mockImplementation((m, t) => t + m)
|
|
|
|
jest.spyOn(fmt, 'successBox').mockImplementation(successBox)
|
|
|
|
|
|
|
|
showBanner({
|
|
|
|
options: {
|
|
|
|
cli: {
|
2019-09-30 08:46:58 +00:00
|
|
|
badgeMessages: [],
|
|
|
|
bannerColor: 'green'
|
2020-05-07 19:08:01 +00:00
|
|
|
},
|
|
|
|
render: {
|
|
|
|
ssr: false
|
2019-04-12 17:19:46 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
server: {
|
|
|
|
listeners: []
|
|
|
|
}
|
|
|
|
}, false)
|
|
|
|
|
|
|
|
expect(successBox).toHaveBeenCalledTimes(1)
|
|
|
|
expect(stdout).toHaveBeenCalledTimes(1)
|
|
|
|
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('Nuxt.js'))
|
|
|
|
expect(stdout).not.toHaveBeenCalledWith(expect.stringMatching('Memory usage'))
|
|
|
|
stdout.mockRestore()
|
|
|
|
})
|
|
|
|
|
2020-05-07 19:08:01 +00:00
|
|
|
test('showBanner does print env, rendering mode and target', () => {
|
|
|
|
const stdout = jest.spyOn(process.stdout, 'write').mockImplementation(() => {})
|
|
|
|
const successBox = jest.fn().mockImplementation((m, t) => t + m)
|
|
|
|
jest.spyOn(fmt, 'successBox').mockImplementation(successBox)
|
|
|
|
|
|
|
|
showBanner({
|
|
|
|
options: {
|
|
|
|
dev: false,
|
|
|
|
target: TARGETS.static,
|
|
|
|
render: {
|
|
|
|
ssr: false
|
|
|
|
},
|
|
|
|
cli: {
|
|
|
|
bannerColor: 'green',
|
|
|
|
badgeMessages: []
|
|
|
|
}
|
|
|
|
},
|
|
|
|
server: {
|
|
|
|
listeners: []
|
|
|
|
}
|
|
|
|
}, false)
|
|
|
|
|
|
|
|
expect(successBox).toHaveBeenCalledTimes(1)
|
|
|
|
expect(stdout).toHaveBeenCalledTimes(1)
|
|
|
|
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('Nuxt.js'))
|
2020-06-17 14:37:19 +00:00
|
|
|
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('▸ Environment:'))
|
|
|
|
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('▸ Rendering:'))
|
|
|
|
expect(stdout).toHaveBeenCalledWith(expect.stringMatching('▸ Target:'))
|
2020-05-07 19:08:01 +00:00
|
|
|
stdout.mockRestore()
|
|
|
|
})
|
|
|
|
|
2019-04-12 17:19:46 +00:00
|
|
|
test('showMemoryUsage prints memory usage', () => {
|
|
|
|
showMemoryUsage()
|
|
|
|
|
|
|
|
expect(consola.info).toHaveBeenCalledTimes(1)
|
|
|
|
expect(consola.info).toHaveBeenCalledWith(expect.stringMatching('Memory usage'))
|
|
|
|
})
|
|
|
|
|
2019-03-03 08:12:46 +00:00
|
|
|
test('forceExit exits after timeout', () => {
|
|
|
|
jest.useFakeTimers()
|
|
|
|
const exit = jest.spyOn(process, 'exit').mockImplementation(() => {})
|
|
|
|
const stderr = jest.spyOn(process.stderr, 'write').mockImplementation(() => {})
|
|
|
|
|
|
|
|
utils.forceExit('test', 1)
|
|
|
|
expect(exit).not.toHaveBeenCalled()
|
|
|
|
jest.runAllTimers()
|
|
|
|
|
|
|
|
expect(stderr).toHaveBeenCalledWith(expect.stringMatching('Nuxt.js will now force exit'))
|
|
|
|
expect(exit).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
|
|
stderr.mockRestore()
|
|
|
|
exit.mockRestore()
|
|
|
|
jest.useRealTimers()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('forceExit exits immediately without timeout', () => {
|
|
|
|
jest.useFakeTimers()
|
|
|
|
const exit = jest.spyOn(process, 'exit').mockImplementation(() => {})
|
|
|
|
const stderr = jest.spyOn(process.stderr, 'write').mockImplementation(() => {})
|
|
|
|
|
|
|
|
utils.forceExit('test', false)
|
|
|
|
expect(stderr).not.toHaveBeenCalledWith()
|
|
|
|
expect(exit).toHaveBeenCalledTimes(1)
|
|
|
|
|
|
|
|
stderr.mockRestore()
|
|
|
|
exit.mockRestore()
|
|
|
|
jest.useRealTimers()
|
|
|
|
})
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|