mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-27 13:52:38 +00:00
feat(cli): add imports mock and cleanup (#4195)
This commit is contained in:
parent
79eb9c8010
commit
4b7afcc218
@ -67,35 +67,19 @@ export default class NuxtCommand {
|
||||
return options
|
||||
}
|
||||
|
||||
importCore() {
|
||||
return imports.core()
|
||||
}
|
||||
|
||||
importBuilder() {
|
||||
return imports.builder()
|
||||
}
|
||||
|
||||
importGenerator() {
|
||||
return imports.generator()
|
||||
}
|
||||
|
||||
importWebpack() {
|
||||
return imports.webpack()
|
||||
}
|
||||
|
||||
async getNuxt(options) {
|
||||
const { Nuxt } = await this.importCore()
|
||||
const { Nuxt } = await imports.core()
|
||||
return new Nuxt(options)
|
||||
}
|
||||
|
||||
async getBuilder(nuxt) {
|
||||
const { Builder } = await this.importBuilder()
|
||||
const { BundleBuilder } = await this.importWebpack()
|
||||
const { Builder } = await imports.builder()
|
||||
const { BundleBuilder } = await imports.webpack()
|
||||
return new Builder(nuxt, BundleBuilder)
|
||||
}
|
||||
|
||||
async getGenerator(nuxt) {
|
||||
const { Generator } = await this.importGenerator()
|
||||
const { Generator } = await imports.generator()
|
||||
const builder = await this.getBuilder(nuxt)
|
||||
return new Generator(nuxt, builder)
|
||||
}
|
||||
|
@ -7,17 +7,7 @@ import * as commands from '../../src/commands'
|
||||
|
||||
const readDir = promisify(readdir)
|
||||
|
||||
consola.add = jest.fn()
|
||||
|
||||
const mockCommand = (cmd, p) => {
|
||||
commands[cmd] = jest.fn().mockImplementationOnce(() => { // eslint-disable-line import/namespace
|
||||
return Promise.resolve({
|
||||
default: () => {
|
||||
return p
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
jest.mock('../../src/commands')
|
||||
|
||||
describe('cli', () => {
|
||||
afterEach(() => {
|
||||
@ -33,26 +23,28 @@ describe('cli', () => {
|
||||
}
|
||||
cmd = cmd.substring(0, cmd.length - 3)
|
||||
|
||||
expect(commands[cmd]).toBeDefined() // eslint-disable-line import/namespace
|
||||
expect(typeof commands[cmd]).toBe('function') // eslint-disable-line import/namespace
|
||||
const cmdFn = commands[cmd] // eslint-disable-line import/namespace
|
||||
expect(cmdFn).toBeDefined()
|
||||
expect(typeof cmdFn).toBe('function')
|
||||
}
|
||||
})
|
||||
|
||||
test('calls expected method', async () => {
|
||||
const argv = process.argv
|
||||
process.argv = ['', '', 'dev']
|
||||
mockCommand('dev', Promise.resolve())
|
||||
const defaultExport = jest.fn().mockImplementation(() => Promise.resolve())
|
||||
commands.dev.mockImplementationOnce(() => Promise.resolve({ default: defaultExport }))
|
||||
|
||||
await run()
|
||||
|
||||
expect(commands.dev).toHaveBeenCalled()
|
||||
expect(defaultExport).toHaveBeenCalled()
|
||||
process.argv = argv
|
||||
})
|
||||
|
||||
test('unknown calls default method', async () => {
|
||||
const argv = process.argv
|
||||
process.argv = ['', '', 'test']
|
||||
mockCommand('dev', Promise.resolve())
|
||||
commands.dev.mockImplementationOnce(() => Promise.resolve())
|
||||
|
||||
await run()
|
||||
|
||||
@ -63,7 +55,7 @@ describe('cli', () => {
|
||||
test('sets NODE_ENV=development for dev', async () => {
|
||||
const nodeEnv = process.env.NODE_ENV
|
||||
process.env.NODE_ENV = ''
|
||||
mockCommand('dev', Promise.resolve())
|
||||
commands.dev.mockImplementationOnce(() => Promise.resolve())
|
||||
|
||||
await run()
|
||||
|
||||
@ -71,12 +63,12 @@ describe('cli', () => {
|
||||
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 nodeEnv = process.env.NODE_ENV
|
||||
process.argv = ['', '', 'build']
|
||||
process.env.NODE_ENV = ''
|
||||
mockCommand('build', Promise.resolve())
|
||||
commands.build.mockImplementationOnce(() => Promise.resolve())
|
||||
|
||||
await run()
|
||||
|
||||
@ -86,7 +78,7 @@ describe('cli', () => {
|
||||
})
|
||||
|
||||
test('catches fatal error', async () => {
|
||||
mockCommand('dev', Promise.reject(new Error('Command Error')))
|
||||
commands.dev.mockImplementationOnce(() => Promise.reject(new Error('Command Error')))
|
||||
|
||||
await run()
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { consola } from '../utils'
|
||||
import Command from '../../src/command'
|
||||
import { options as Options } from '../../src/options'
|
||||
import { consola } from '../utils'
|
||||
|
||||
jest.mock('@nuxt/core')
|
||||
jest.mock('@nuxt/builder')
|
||||
|
@ -1,7 +1,6 @@
|
||||
import { consola } from '../utils'
|
||||
import { mockNuxt, mockBuilder, mockGetNuxtConfig } from '../utils/mocking'
|
||||
import { consola, mockNuxt, mockBuilder, mockGetNuxtConfig } from '../utils'
|
||||
|
||||
describe.skip('dev', () => {
|
||||
describe('dev', () => {
|
||||
let dev
|
||||
|
||||
beforeAll(async () => {
|
||||
@ -10,7 +9,7 @@ describe.skip('dev', () => {
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks()
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
test('is function', () => {
|
||||
@ -30,7 +29,7 @@ describe.skip('dev', () => {
|
||||
expect(Nuxt.prototype.showReady).toHaveBeenCalled()
|
||||
expect(Builder.prototype.watchServer).toHaveBeenCalled()
|
||||
|
||||
jest.resetAllMocks()
|
||||
jest.clearAllMocks()
|
||||
|
||||
const builder = new Builder()
|
||||
builder.nuxt = new Nuxt()
|
||||
@ -53,7 +52,7 @@ describe.skip('dev', () => {
|
||||
const Builder = mockBuilder()
|
||||
|
||||
await dev()
|
||||
jest.resetAllMocks()
|
||||
jest.clearAllMocks()
|
||||
|
||||
// Test error on second build so we cover oldInstance stuff
|
||||
const builder = new Builder()
|
||||
@ -70,7 +69,7 @@ describe.skip('dev', () => {
|
||||
const Builder = mockBuilder()
|
||||
|
||||
await dev()
|
||||
jest.resetAllMocks()
|
||||
jest.clearAllMocks()
|
||||
|
||||
const builder = new Builder()
|
||||
builder.nuxt = new Nuxt()
|
||||
@ -86,7 +85,7 @@ describe.skip('dev', () => {
|
||||
const Builder = mockBuilder()
|
||||
|
||||
await dev()
|
||||
jest.resetAllMocks()
|
||||
jest.clearAllMocks()
|
||||
|
||||
mockGetNuxtConfig().mockImplementationOnce(() => {
|
||||
throw new Error('Config Error')
|
||||
|
@ -3,6 +3,8 @@ export * from './mocking'
|
||||
|
||||
jest.mock('consola')
|
||||
|
||||
consola.add = jest.fn()
|
||||
|
||||
export {
|
||||
consola
|
||||
}
|
||||
|
@ -1,5 +1,23 @@
|
||||
import * as imports from '../../src/imports'
|
||||
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) => {
|
||||
Command.prototype.getNuxt = jest.fn().mockImplementationOnce(() => {
|
||||
return Object.assign({
|
||||
@ -75,9 +93,8 @@ export const mockNuxt = (implementation) => {
|
||||
showReady: jest.fn().mockImplementationOnce(() => Promise.resolve())
|
||||
}, implementation || {})
|
||||
|
||||
Command.prototype.importCore = jest.fn().mockImplementationOnce(() => {
|
||||
return { Nuxt }
|
||||
})
|
||||
imports.core.mockImplementation(() => ({ Nuxt }))
|
||||
|
||||
return Nuxt
|
||||
}
|
||||
|
||||
@ -89,8 +106,7 @@ export const mockBuilder = (implementation) => {
|
||||
watchServer: jest.fn().mockImplementationOnce(() => Promise.resolve())
|
||||
}, implementation || {})
|
||||
|
||||
Command.prototype.importBuilder = jest.fn().mockImplementationOnce(() => {
|
||||
return { Builder }
|
||||
})
|
||||
imports.builder.mockImplementation(() => ({ Builder }))
|
||||
|
||||
return Builder
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user