2018-10-29 22:16:16 +00:00
|
|
|
import { consola, mockNuxt, mockBuilder, mockGetNuxtConfig, NuxtCommand } from '../utils'
|
2018-10-25 07:43:42 +00:00
|
|
|
|
2018-10-25 15:40:55 +00:00
|
|
|
describe('dev', () => {
|
2018-10-25 07:43:42 +00:00
|
|
|
let dev
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2018-10-29 22:16:16 +00:00
|
|
|
dev = await import('../../src/commands/dev').then(m => m.default)
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
afterEach(() => jest.clearAllMocks())
|
2018-10-25 07:43:42 +00:00
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
test('has run function', () => {
|
|
|
|
expect(typeof dev.run).toBe('function')
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
test('reloads on fileChanged hook', async () => {
|
|
|
|
const Nuxt = mockNuxt()
|
|
|
|
const Builder = mockBuilder()
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
await NuxtCommand.from(dev).run()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
expect(consola.error).not.toHaveBeenCalled()
|
|
|
|
|
|
|
|
expect(Builder.prototype.build).toHaveBeenCalled()
|
2018-10-30 20:42:53 +00:00
|
|
|
expect(Nuxt.prototype.server.listen).toHaveBeenCalled()
|
|
|
|
expect(Nuxt.prototype.server.showReady).toHaveBeenCalled()
|
2018-10-25 07:43:42 +00:00
|
|
|
expect(Builder.prototype.watchServer).toHaveBeenCalled()
|
|
|
|
|
2018-10-25 15:40:55 +00:00
|
|
|
jest.clearAllMocks()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
const builder = new Builder()
|
|
|
|
builder.nuxt = new Nuxt()
|
|
|
|
await Nuxt.fileChangedHook(builder)
|
|
|
|
expect(consola.debug).toHaveBeenCalled()
|
|
|
|
|
|
|
|
expect(Nuxt.prototype.clearHook).toHaveBeenCalled()
|
|
|
|
expect(Builder.prototype.unwatch).toHaveBeenCalled()
|
|
|
|
expect(Builder.prototype.build).toHaveBeenCalled()
|
|
|
|
expect(Nuxt.prototype.close).toHaveBeenCalled()
|
2018-10-30 20:42:53 +00:00
|
|
|
expect(Nuxt.prototype.server.listen).toHaveBeenCalled()
|
|
|
|
expect(Nuxt.prototype.server.showReady).not.toHaveBeenCalled()
|
2018-10-25 07:43:42 +00:00
|
|
|
expect(Builder.prototype.watchServer).toHaveBeenCalled()
|
|
|
|
|
|
|
|
expect(consola.error).not.toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('catches build error', async () => {
|
|
|
|
const Nuxt = mockNuxt()
|
|
|
|
const Builder = mockBuilder()
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
await NuxtCommand.from(dev).run()
|
2018-10-25 15:40:55 +00:00
|
|
|
jest.clearAllMocks()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
// Test error on second build so we cover oldInstance stuff
|
|
|
|
const builder = new Builder()
|
|
|
|
builder.nuxt = new Nuxt()
|
|
|
|
Builder.prototype.build = jest.fn().mockImplementationOnce(() => Promise.reject(new Error('Build Error')))
|
|
|
|
await Nuxt.fileChangedHook(builder)
|
|
|
|
|
|
|
|
expect(Nuxt.prototype.close).toHaveBeenCalled()
|
|
|
|
expect(consola.error).toHaveBeenCalledWith(new Error('Build Error'))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('catches watchServer error', async () => {
|
|
|
|
const Nuxt = mockNuxt()
|
|
|
|
const Builder = mockBuilder()
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
await NuxtCommand.from(dev).run()
|
2018-10-25 15:40:55 +00:00
|
|
|
jest.clearAllMocks()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
const builder = new Builder()
|
|
|
|
builder.nuxt = new Nuxt()
|
|
|
|
Builder.prototype.watchServer = jest.fn().mockImplementationOnce(() => Promise.reject(new Error('watchServer Error')))
|
|
|
|
await Nuxt.fileChangedHook(builder)
|
|
|
|
|
|
|
|
expect(consola.error).toHaveBeenCalledWith(new Error('watchServer Error'))
|
|
|
|
expect(Builder.prototype.watchServer).toHaveBeenCalledTimes(2)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('catches error on hook error', async () => {
|
|
|
|
const Nuxt = mockNuxt()
|
|
|
|
const Builder = mockBuilder()
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
await NuxtCommand.from(dev).run()
|
2018-10-25 15:40:55 +00:00
|
|
|
jest.clearAllMocks()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
mockGetNuxtConfig().mockImplementationOnce(() => {
|
|
|
|
throw new Error('Config Error')
|
|
|
|
})
|
|
|
|
const builder = new Builder()
|
|
|
|
builder.nuxt = new Nuxt()
|
|
|
|
await Nuxt.fileChangedHook(builder)
|
|
|
|
|
|
|
|
expect(consola.error).toHaveBeenCalledWith(new Error('Config Error'))
|
|
|
|
expect(Builder.prototype.watchServer).toHaveBeenCalledTimes(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('catches error on startDev', async () => {
|
|
|
|
mockNuxt({
|
2018-10-30 20:42:53 +00:00
|
|
|
server: {
|
|
|
|
listen: jest.fn().mockImplementation(() => {
|
|
|
|
throw new Error('Listen Error')
|
|
|
|
})
|
|
|
|
}
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|
|
|
|
mockBuilder()
|
|
|
|
|
2018-10-29 22:16:16 +00:00
|
|
|
await NuxtCommand.from(dev).run()
|
2018-10-25 07:43:42 +00:00
|
|
|
|
|
|
|
expect(consola.error).toHaveBeenCalledWith(new Error('Listen Error'))
|
|
|
|
})
|
|
|
|
})
|