feat(cli): add imports mock and cleanup (#4195)

This commit is contained in:
Pim 2018-10-25 17:40:55 +02:00 committed by Pooya Parsa
parent 79eb9c8010
commit 4b7afcc218
6 changed files with 48 additions and 55 deletions

View File

@ -67,35 +67,19 @@ export default class NuxtCommand {
return options return options
} }
importCore() {
return imports.core()
}
importBuilder() {
return imports.builder()
}
importGenerator() {
return imports.generator()
}
importWebpack() {
return imports.webpack()
}
async getNuxt(options) { async getNuxt(options) {
const { Nuxt } = await this.importCore() const { Nuxt } = await imports.core()
return new Nuxt(options) return new Nuxt(options)
} }
async getBuilder(nuxt) { async getBuilder(nuxt) {
const { Builder } = await this.importBuilder() const { Builder } = await imports.builder()
const { BundleBuilder } = await this.importWebpack() const { BundleBuilder } = await imports.webpack()
return new Builder(nuxt, BundleBuilder) return new Builder(nuxt, BundleBuilder)
} }
async getGenerator(nuxt) { async getGenerator(nuxt) {
const { Generator } = await this.importGenerator() const { Generator } = await imports.generator()
const builder = await this.getBuilder(nuxt) const builder = await this.getBuilder(nuxt)
return new Generator(nuxt, builder) return new Generator(nuxt, builder)
} }

View File

@ -7,17 +7,7 @@ import * as commands from '../../src/commands'
const readDir = promisify(readdir) const readDir = promisify(readdir)
consola.add = jest.fn() jest.mock('../../src/commands')
const mockCommand = (cmd, p) => {
commands[cmd] = jest.fn().mockImplementationOnce(() => { // eslint-disable-line import/namespace
return Promise.resolve({
default: () => {
return p
}
})
})
}
describe('cli', () => { describe('cli', () => {
afterEach(() => { afterEach(() => {
@ -33,26 +23,28 @@ describe('cli', () => {
} }
cmd = cmd.substring(0, cmd.length - 3) cmd = cmd.substring(0, cmd.length - 3)
expect(commands[cmd]).toBeDefined() // eslint-disable-line import/namespace const cmdFn = commands[cmd] // eslint-disable-line import/namespace
expect(typeof commands[cmd]).toBe('function') // eslint-disable-line import/namespace expect(cmdFn).toBeDefined()
expect(typeof cmdFn).toBe('function')
} }
}) })
test('calls expected method', async () => { test('calls expected method', async () => {
const argv = process.argv const argv = process.argv
process.argv = ['', '', 'dev'] process.argv = ['', '', 'dev']
mockCommand('dev', Promise.resolve()) const defaultExport = jest.fn().mockImplementation(() => Promise.resolve())
commands.dev.mockImplementationOnce(() => Promise.resolve({ default: defaultExport }))
await run() await run()
expect(commands.dev).toHaveBeenCalled() expect(defaultExport).toHaveBeenCalled()
process.argv = argv process.argv = argv
}) })
test('unknown calls default method', async () => { test('unknown calls default method', async () => {
const argv = process.argv const argv = process.argv
process.argv = ['', '', 'test'] process.argv = ['', '', 'test']
mockCommand('dev', Promise.resolve()) commands.dev.mockImplementationOnce(() => Promise.resolve())
await run() await run()
@ -63,7 +55,7 @@ describe('cli', () => {
test('sets NODE_ENV=development for dev', async () => { test('sets NODE_ENV=development for dev', async () => {
const nodeEnv = process.env.NODE_ENV const nodeEnv = process.env.NODE_ENV
process.env.NODE_ENV = '' process.env.NODE_ENV = ''
mockCommand('dev', Promise.resolve()) commands.dev.mockImplementationOnce(() => Promise.resolve())
await run() await run()
@ -71,12 +63,12 @@ describe('cli', () => {
process.env.NODE_ENV = nodeEnv process.env.NODE_ENV = nodeEnv
}) })
test('sets ODE_ENV=production for build', async () => { test('sets NODE_ENV=production for build', async () => {
const argv = process.argv const argv = process.argv
const nodeEnv = process.env.NODE_ENV const nodeEnv = process.env.NODE_ENV
process.argv = ['', '', 'build'] process.argv = ['', '', 'build']
process.env.NODE_ENV = '' process.env.NODE_ENV = ''
mockCommand('build', Promise.resolve()) commands.build.mockImplementationOnce(() => Promise.resolve())
await run() await run()
@ -86,7 +78,7 @@ describe('cli', () => {
}) })
test('catches fatal error', async () => { test('catches fatal error', async () => {
mockCommand('dev', Promise.reject(new Error('Command Error'))) commands.dev.mockImplementationOnce(() => Promise.reject(new Error('Command Error')))
await run() await run()

View File

@ -1,6 +1,6 @@
import { consola } from '../utils'
import Command from '../../src/command' import Command from '../../src/command'
import { options as Options } from '../../src/options' import { options as Options } from '../../src/options'
import { consola } from '../utils'
jest.mock('@nuxt/core') jest.mock('@nuxt/core')
jest.mock('@nuxt/builder') jest.mock('@nuxt/builder')

View File

@ -1,7 +1,6 @@
import { consola } from '../utils' import { consola, mockNuxt, mockBuilder, mockGetNuxtConfig } from '../utils'
import { mockNuxt, mockBuilder, mockGetNuxtConfig } from '../utils/mocking'
describe.skip('dev', () => { describe('dev', () => {
let dev let dev
beforeAll(async () => { beforeAll(async () => {
@ -10,7 +9,7 @@ describe.skip('dev', () => {
}) })
afterEach(() => { afterEach(() => {
jest.resetAllMocks() jest.clearAllMocks()
}) })
test('is function', () => { test('is function', () => {
@ -30,7 +29,7 @@ describe.skip('dev', () => {
expect(Nuxt.prototype.showReady).toHaveBeenCalled() expect(Nuxt.prototype.showReady).toHaveBeenCalled()
expect(Builder.prototype.watchServer).toHaveBeenCalled() expect(Builder.prototype.watchServer).toHaveBeenCalled()
jest.resetAllMocks() jest.clearAllMocks()
const builder = new Builder() const builder = new Builder()
builder.nuxt = new Nuxt() builder.nuxt = new Nuxt()
@ -53,7 +52,7 @@ describe.skip('dev', () => {
const Builder = mockBuilder() const Builder = mockBuilder()
await dev() await dev()
jest.resetAllMocks() jest.clearAllMocks()
// Test error on second build so we cover oldInstance stuff // Test error on second build so we cover oldInstance stuff
const builder = new Builder() const builder = new Builder()
@ -70,7 +69,7 @@ describe.skip('dev', () => {
const Builder = mockBuilder() const Builder = mockBuilder()
await dev() await dev()
jest.resetAllMocks() jest.clearAllMocks()
const builder = new Builder() const builder = new Builder()
builder.nuxt = new Nuxt() builder.nuxt = new Nuxt()
@ -86,7 +85,7 @@ describe.skip('dev', () => {
const Builder = mockBuilder() const Builder = mockBuilder()
await dev() await dev()
jest.resetAllMocks() jest.clearAllMocks()
mockGetNuxtConfig().mockImplementationOnce(() => { mockGetNuxtConfig().mockImplementationOnce(() => {
throw new Error('Config Error') throw new Error('Config Error')

View File

@ -3,6 +3,8 @@ export * from './mocking'
jest.mock('consola') jest.mock('consola')
consola.add = jest.fn()
export { export {
consola consola
} }

View File

@ -1,5 +1,23 @@
import * as imports from '../../src/imports'
import Command from '../../src/command' import Command from '../../src/command'
jest.mock('../../src/imports', () => {
return {
core: jest.fn().mockImplementation(() => ({
Nuxt: function () {}
})),
builder: jest.fn().mockImplementation(() => ({
Builder: function () {}
})),
generator: jest.fn().mockImplementation(() => ({
Generator: function () {}
})),
webpack: jest.fn().mockImplementation(() => ({
BundleBuilder: function () {}
}))
}
})
export const mockGetNuxt = (options, implementation) => { export const mockGetNuxt = (options, implementation) => {
Command.prototype.getNuxt = jest.fn().mockImplementationOnce(() => { Command.prototype.getNuxt = jest.fn().mockImplementationOnce(() => {
return Object.assign({ return Object.assign({
@ -75,9 +93,8 @@ export const mockNuxt = (implementation) => {
showReady: jest.fn().mockImplementationOnce(() => Promise.resolve()) showReady: jest.fn().mockImplementationOnce(() => Promise.resolve())
}, implementation || {}) }, implementation || {})
Command.prototype.importCore = jest.fn().mockImplementationOnce(() => { imports.core.mockImplementation(() => ({ Nuxt }))
return { Nuxt }
})
return Nuxt return Nuxt
} }
@ -89,8 +106,7 @@ export const mockBuilder = (implementation) => {
watchServer: jest.fn().mockImplementationOnce(() => Promise.resolve()) watchServer: jest.fn().mockImplementationOnce(() => Promise.resolve())
}, implementation || {}) }, implementation || {})
Command.prototype.importBuilder = jest.fn().mockImplementationOnce(() => { imports.builder.mockImplementation(() => ({ Builder }))
return { Builder }
})
return Builder return Builder
} }