2018-10-25 07:43:42 +00:00
|
|
|
import { readdir } from 'fs'
|
|
|
|
import { resolve } from 'path'
|
|
|
|
import { promisify } from 'util'
|
|
|
|
import { consola } from '../utils'
|
|
|
|
import { run } from '../../src'
|
|
|
|
import * as commands from '../../src/commands'
|
|
|
|
|
|
|
|
const readDir = promisify(readdir)
|
|
|
|
|
2018-10-25 15:40:55 +00:00
|
|
|
jest.mock('../../src/commands')
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
describe('cli', () => {
|
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('exports for all commands defined', async () => {
|
|
|
|
const cmds = await readDir(resolve(__dirname, '..', '..', 'src', 'commands'))
|
|
|
|
|
|
|
|
for (let cmd of cmds) {
|
|
|
|
if (cmd === 'index.js') {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cmd = cmd.substring(0, cmd.length - 3)
|
|
|
|
|
2018-10-25 15:40:55 +00:00
|
|
|
const cmdFn = commands[cmd] // eslint-disable-line import/namespace
|
|
|
|
expect(cmdFn).toBeDefined()
|
|
|
|
expect(typeof cmdFn).toBe('function')
|
2018-10-25 07:43:42 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
test('calls expected method', async () => {
|
|
|
|
const argv = process.argv
|
|
|
|
process.argv = ['', '', 'dev']
|
2018-10-25 15:40:55 +00:00
|
|
|
const defaultExport = jest.fn().mockImplementation(() => Promise.resolve())
|
|
|
|
commands.dev.mockImplementationOnce(() => Promise.resolve({ default: defaultExport }))
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
await run()
|
|
|
|
|
2018-10-25 15:40:55 +00:00
|
|
|
expect(defaultExport).toHaveBeenCalled()
|
2018-10-25 07:43:42 +00:00
|
|
|
process.argv = argv
|
|
|
|
})
|
|
|
|
|
|
|
|
test('unknown calls default method', async () => {
|
|
|
|
const argv = process.argv
|
|
|
|
process.argv = ['', '', 'test']
|
2018-10-25 15:40:55 +00:00
|
|
|
commands.dev.mockImplementationOnce(() => Promise.resolve())
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
await run()
|
|
|
|
|
|
|
|
expect(commands.dev).toHaveBeenCalled()
|
|
|
|
process.argv = argv
|
|
|
|
})
|
|
|
|
|
|
|
|
test('sets NODE_ENV=development for dev', async () => {
|
|
|
|
const nodeEnv = process.env.NODE_ENV
|
|
|
|
process.env.NODE_ENV = ''
|
2018-10-25 15:40:55 +00:00
|
|
|
commands.dev.mockImplementationOnce(() => Promise.resolve())
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
await run()
|
|
|
|
|
|
|
|
expect(process.env.NODE_ENV).toBe('development')
|
|
|
|
process.env.NODE_ENV = nodeEnv
|
|
|
|
})
|
|
|
|
|
2018-10-25 15:40:55 +00:00
|
|
|
test('sets NODE_ENV=production for build', async () => {
|
2018-10-25 07:43:42 +00:00
|
|
|
const argv = process.argv
|
|
|
|
const nodeEnv = process.env.NODE_ENV
|
|
|
|
process.argv = ['', '', 'build']
|
|
|
|
process.env.NODE_ENV = ''
|
2018-10-25 15:40:55 +00:00
|
|
|
commands.build.mockImplementationOnce(() => Promise.resolve())
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
await run()
|
|
|
|
|
|
|
|
expect(process.env.NODE_ENV).toBe('production')
|
|
|
|
process.argv = argv
|
|
|
|
process.env.NODE_ENV = nodeEnv
|
|
|
|
})
|
|
|
|
|
|
|
|
test('catches fatal error', async () => {
|
2018-10-25 15:40:55 +00:00
|
|
|
commands.dev.mockImplementationOnce(() => Promise.reject(new Error('Command Error')))
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
await run()
|
|
|
|
|
|
|
|
expect(consola.fatal).toHaveBeenCalledWith(new Error('Command Error'))
|
|
|
|
})
|
|
|
|
})
|