-
-
Notifications
You must be signed in to change notification settings - Fork 473
/
init.test.ts
193 lines (164 loc) · 5.16 KB
/
init.test.ts
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* @jest-environment node
*/
import * as path from 'path'
import * as fs from 'fs-extra'
import { exec } from 'child_process'
import { createTeardown } from 'fs-teardown'
import { fromTemp, promisifyChildProcess } from '../../support/utils'
const fsMock = createTeardown({
rootDir: fromTemp('cli/init'),
paths: {
'package.json': JSON.stringify({
name: 'package-name',
}),
},
})
const cliPath = path.join(__dirname, '../../../cli/index.js')
function readJson(filePath: string) {
const rawContent = fs.readFileSync(filePath, 'utf8')
try {
const content = JSON.parse(rawContent)
return content
} catch (error) {
return rawContent
}
}
beforeAll(async () => {
await fsMock.prepare()
})
beforeEach(async () => {
await fsMock.reset()
})
afterAll(async () => {
jest.restoreAllMocks()
await fsMock.cleanup()
})
test('copies the worker script into an existing directory without errors', async () => {
await fsMock.create({
public: null,
})
const init = await promisifyChildProcess(
exec(`node ${cliPath} init ${fsMock.resolve('public')} --no-save`),
)
// Does not produce any errors.
expect(init.stderr).toEqual('')
// Creates the worker script at the given path.
expect(fs.existsSync(fsMock.resolve('public/mockServiceWorker.js'))).toEqual(
true,
)
// Notifies the user about the created script.
expect(init.stdout).toContain('Service Worker successfully created')
// Does not change the package.json.
expect(readJson(fsMock.resolve('package.json'))).toEqual({
name: 'package-name',
})
})
test('creates the directory if it does not exist', async () => {
const { stderr } = await promisifyChildProcess(
exec(
`node ${cliPath} init ${fsMock.resolve('public/nested/path')} --no-save`,
),
)
// Does not produce any errors.
expect(stderr).toEqual('')
expect(
fs.existsSync(fsMock.resolve('public/nested/path/mockServiceWorker.js')),
).toEqual(true)
})
test('saves the worker directory in package.json when none are present', async () => {
await fsMock.create({
public: null,
})
const init = await promisifyChildProcess(
exec(`node ${cliPath} init ${fsMock.resolve('public')} --save`, {
cwd: fsMock.resolve(),
}),
)
expect(init.stderr).toEqual('')
expect(init.stdout).toContain(
'In order to ease the future updates to the worker script,\nwe recommend saving the path to the worker directory in your package.json.',
)
expect(init.stdout).toContain(
`Writing "msw.workerDirectory" to "${fsMock.resolve('package.json')}"...`,
)
expect(fs.existsSync(fsMock.resolve('public/mockServiceWorker.js')))
expect(readJson(fsMock.resolve('package.json'))).toEqual({
name: 'package-name',
msw: {
workerDirectory: 'public',
},
})
})
test('warns when current public directory does not match the saved directory from package.json', async () => {
await fsMock.create({
'package.json': JSON.stringify({
name: 'package-name',
msw: {
workerDirectory: 'dist',
},
}),
public: null,
})
const init = await promisifyChildProcess(
exec(`node ${cliPath} init ${fsMock.resolve('public')} --save`, {
cwd: fsMock.resolve('.'),
}),
)
expect(init.stderr).toEqual('')
expect(init.stdout).toContain(
'The "msw.workerDirectory" in your package.json ("dist")\nis different from the worker directory used right now ("public").',
)
expect(init.stdout).toContain(
`Writing "msw.workerDirectory" to "${fsMock.resolve('package.json')}"...`,
)
expect(fs.existsSync(fsMock.resolve('public/mockServiceWorker.js')))
expect(readJson(fsMock.resolve('package.json'))).toEqual({
name: 'package-name',
msw: {
workerDirectory: 'public',
},
})
})
test('does not produce eslint errors or warnings', async () => {
await fsMock.create({
'.eslintrc.json': JSON.stringify({
rules: {
'no-warning-comments': [
'warn',
// intentionally degenerate config to cause a warning in the initial comment
{ terms: ['mock'], location: 'anywhere' },
],
},
}),
public: null,
})
const init = await promisifyChildProcess(
exec(`node ${cliPath} init ${fsMock.resolve('public')} --no-save`),
)
expect(init.stderr).toEqual('')
const eslint = await promisifyChildProcess(
exec(path.resolve(`node_modules/.bin/eslint ${fsMock.resolve()}`)),
)
expect(eslint.stdout).toEqual('')
expect(eslint.stderr).toEqual('')
})
test('errors and shuts down if creating a directory fails', async () => {
const init = require('../../../cli/init')
const error = new Error('Could not create this directory')
jest.spyOn(fs.promises, 'mkdir').mockRejectedValue(error)
const exitSpy = jest.spyOn(process, 'exit').mockImplementationOnce(() => {
throw error
})
const consoleSpy = jest
.spyOn(console, 'error')
.mockImplementationOnce(jest.fn())
const publicDir = 'public'
await expect(init({ publicDir, save: false })).rejects.not.toBeUndefined()
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('Failed to create a Service Worker'),
expect.stringContaining(publicDir),
error,
)
expect(exitSpy).toHaveBeenCalledWith(1)
})