forked from nuxt/nuxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.test.js
More file actions
141 lines (105 loc) · 4.01 KB
/
dev.test.js
File metadata and controls
141 lines (105 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import * as utils from '../../src/utils/'
import { consola, mockNuxt, mockBuilder, mockGetNuxtConfig, NuxtCommand } from '../utils'
describe('dev', () => {
let dev
beforeAll(async () => {
dev = await import('../../src/commands/dev').then(m => m.default)
jest.spyOn(utils, 'forceExit').mockImplementation(() => {})
})
afterEach(() => jest.clearAllMocks())
test('has run function', () => {
expect(typeof dev.run).toBe('function')
})
test('reloads on fileRestartHook', async () => {
const Nuxt = mockNuxt()
const Builder = mockBuilder()
await NuxtCommand.from(dev).run()
expect(consola.error).not.toHaveBeenCalled()
expect(Builder.prototype.build).toHaveBeenCalled()
expect(Nuxt.prototype.server.listen).toHaveBeenCalled()
// expect(Builder.prototype.watchRestart).toHaveBeenCalled()
jest.clearAllMocks()
const builder = new Builder()
builder.nuxt = new Nuxt()
await Nuxt.fileRestartHook(builder)
expect(consola.log).toHaveBeenCalled()
expect(Builder.prototype.build).toHaveBeenCalled()
expect(Nuxt.prototype.close).toHaveBeenCalled()
expect(Nuxt.prototype.server.listen).toHaveBeenCalled()
expect(consola.error).not.toHaveBeenCalled()
})
test('catches build error and calls hook', async () => {
const Nuxt = mockNuxt()
const Builder = mockBuilder()
await NuxtCommand.from(dev).run()
jest.clearAllMocks()
// Test error on second build so we cover oldInstance stuff
const builder = new Builder()
builder.nuxt = new Nuxt()
Builder.prototype.build = () => { throw new Error('Build Error') }
await Nuxt.fileRestartHook(builder)
expect(Nuxt.prototype.close).toHaveBeenCalled()
expect(Nuxt.prototype.callHook).toHaveBeenCalledWith('cli:buildError', expect.any(Error))
expect(consola.error).toHaveBeenCalledWith(new Error('Build Error'))
})
test.skip('catches watchRestart error', async () => {
const Nuxt = mockNuxt()
const Builder = mockBuilder()
await NuxtCommand.from(dev).run()
jest.clearAllMocks()
const builder = new Builder()
builder.nuxt = new Nuxt()
Builder.prototype.watchRestart = jest.fn().mockImplementationOnce(() => Promise.reject(new Error('watchRestart Error')))
await Nuxt.fileRestartHook(builder)
expect(consola.error).toHaveBeenCalledWith(new Error('watchRestart Error'))
expect(Builder.prototype.watchRestart).toHaveBeenCalledTimes(2)
})
test('catches error on hook error', async () => {
const Nuxt = mockNuxt()
const Builder = mockBuilder()
await NuxtCommand.from(dev).run()
jest.clearAllMocks()
mockGetNuxtConfig().mockImplementationOnce(() => {
throw new Error('Config Error')
})
const builder = new Builder()
builder.nuxt = new Nuxt()
await Nuxt.fileRestartHook(builder)
expect(consola.fatal).toHaveBeenCalledWith(new Error('Config Error'))
// expect(Builder.prototype.watchRestart).toHaveBeenCalledTimes(1)
})
test('catches error on startDev', async () => {
mockNuxt({
server: {
listen: jest.fn().mockImplementation(() => {
throw new Error('Listen Error')
})
}
})
mockBuilder()
await NuxtCommand.from(dev).run()
expect(consola.fatal).toHaveBeenCalledWith(new Error('Listen Error'))
})
test('dev doesnt force-exit by default', async () => {
mockNuxt()
mockBuilder()
const cmd = NuxtCommand.from(dev, ['dev', '.'])
await cmd.run()
expect(utils.forceExit).not.toHaveBeenCalled()
})
test('dev can set force exit explicitly', async () => {
mockNuxt()
mockBuilder()
const cmd = NuxtCommand.from(dev, ['dev', '.', '--force-exit'])
await cmd.run()
expect(utils.forceExit).toHaveBeenCalledTimes(1)
expect(utils.forceExit).toHaveBeenCalledWith('dev', false)
})
test('dev can disable force exit explicitly', async () => {
mockNuxt()
mockBuilder()
const cmd = NuxtCommand.from(dev, ['dev', '.', '--no-force-exit'])
await cmd.run()
expect(utils.forceExit).not.toHaveBeenCalled()
})
})