generated from christian-bromann/typescript-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.test.ts
301 lines (282 loc) · 10.2 KB
/
index.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import fs from 'node:fs/promises'
import semver from 'semver'
import { resolve } from 'import-meta-resolve'
import { vi, test, expect, beforeEach, afterEach } from 'vitest'
import { Command } from 'commander'
import { execSync } from 'node:child_process'
import { run, createWebdriverIO } from '../src'
import { runProgram } from '../src/utils'
import type { ProgramOpts } from '../src/types'
vi.mock('node:fs/promises', () => ({
default: {
access: vi.fn(),
mkdir: vi.fn(),
writeFile: vi.fn()
}
}))
vi.mock('node:child_process', () => ({
execSync: vi.fn()
}))
vi.mock('import-meta-resolve', () => ({
resolve: vi.fn()
}))
vi.mock('commander')
vi.mock('semver', () => ({
default: {
satisfies: vi.fn().mockReturnValue(true)
}
}))
vi.mock('../src/utils.js', () => ({
runProgram: vi.fn(),
colorItBold: vi.fn((log) => log),
colorIt: vi.fn((log) => log),
getPackageVersion: vi.fn().mockReturnValue('0.1.1'),
shouldUseYarn: vi.fn().mockReturnValue(true)
}))
const consoleLog = console.log.bind(console)
beforeEach(() => {
console.log = vi.fn()
vi.mocked(runProgram).mockClear()
vi.mocked(resolve).mockImplementation(() => {
throw new Error('foo')
})
vi.mocked(execSync).mockReturnValue(`
`)
vi.mocked(fs.access).mockResolvedValue()
})
test('run', async () => {
const op = vi.fn().mockResolvedValue({})
await run(op)
expect(op).toBeCalledTimes(1)
expect(op).toBeCalledWith('foobar')
expect(new Command().arguments).toBeCalledWith('[project-path]')
expect(console.log).toBeCalledTimes(1)
expect(vi.mocked(console.log).mock.calls[0][0]).toContain('oooooooooooo')
expect(vi.mocked(console.log).mock.calls[0][1]).toContain('Next-gen browser and mobile automation')
})
test('does not run if Node.js version is too low', async () => {
const consoleLog = vi.spyOn(console, 'log')
vi.mocked(semver.satisfies).mockReturnValue(false)
const op = vi.fn().mockResolvedValue({})
await run(op)
expect(op).toBeCalledTimes(0)
expect(consoleLog).toBeCalledWith(expect.stringContaining('Please update to Node.js v20 to continue.'))
})
test('createWebdriverIO with Yarn', async () => {
vi.stubEnv('npm_config_user_agent', 'yarn/4.5.0 npm/? node/v20.11.0 darwin arm64')
await createWebdriverIO({ npmTag: 'latest' } as ProgramOpts)
expect(runProgram).toBeCalledWith(
'yarn',
['add', '@wdio/cli@latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledWith(
'yarn',
['exec', 'wdio', 'config', '--npm-tag', 'latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledTimes(2)
expect(fs.mkdir).toBeCalledTimes(0)
expect(fs.writeFile).toBeCalledTimes(0)
})
test('createWebdriverIO with NPM', async () => {
vi.stubEnv('npm_config_user_agent', 'npm/10.2.4 node/v20.11.0 darwin arm64 workspaces/false')
await createWebdriverIO({ npmTag: 'latest' } as ProgramOpts)
expect(runProgram).toBeCalledWith(
'npm',
['install', '@wdio/cli@latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledWith(
'npx',
['wdio', 'config', '--npm-tag', 'latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledTimes(2)
expect(fs.mkdir).toBeCalledTimes(0)
expect(fs.writeFile).toBeCalledTimes(0)
})
test('createWebdriverIO with invalid agent should run npm commands', async () => {
vi.stubEnv('npm_config_user_agent', 'invalid/10.2.4 node/v20.11.0 darwin arm64 workspaces/false')
await createWebdriverIO({ npmTag: 'latest' } as ProgramOpts)
expect(runProgram).toBeCalledWith(
'npm',
['install', '@wdio/cli@latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledWith(
'npx',
['wdio', 'config', '--npm-tag', 'latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledTimes(2)
expect(fs.mkdir).toBeCalledTimes(0)
expect(fs.writeFile).toBeCalledTimes(0)
})
test('createWebdriverIO with no npm user agent should run npm commands', async () => {
vi.stubEnv('npm_config_user_agent', '')
await createWebdriverIO({ npmTag: 'latest' } as ProgramOpts)
expect(runProgram).toBeCalledWith(
'npm',
['install', '@wdio/cli@latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledWith(
'npx',
['wdio', 'config', '--npm-tag', 'latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledTimes(2)
expect(fs.mkdir).toBeCalledTimes(0)
expect(fs.writeFile).toBeCalledTimes(0)
})
test('createWebdriverIO with pnpm', async () => {
vi.stubEnv('npm_config_user_agent', 'pnpm/9.10.0 npm/? node/v20.11.0 darwin arm64')
await createWebdriverIO({ npmTag: 'latest' } as ProgramOpts)
expect(runProgram).toBeCalledWith(
'pnpm',
['add', '@wdio/cli@latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledWith(
'pnpm',
['exec', 'wdio', 'config', '--npm-tag', 'latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledTimes(2)
expect(fs.mkdir).toBeCalledTimes(0)
expect(fs.writeFile).toBeCalledTimes(0)
})
test('createWebdriverIO with bun', async () => {
vi.stubEnv('npm_config_user_agent', 'bun/1.1.27 npm/? node/v22.6.0 darwin arm64')
await createWebdriverIO({ npmTag: 'latest' } as ProgramOpts)
expect(runProgram).toBeCalledWith(
'bun',
['install', '@wdio/cli@latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledWith(
'bunx',
['wdio', 'config', '--npm-tag', 'latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledTimes(2)
expect(fs.mkdir).toBeCalledTimes(0)
expect(fs.writeFile).toBeCalledTimes(0)
})
test('creates a directory if it does not exist', async () => {
vi.stubEnv('npm_config_user_agent', 'npm/10.2.4 node/v20.11.0 darwin arm64 workspaces/false')
await createWebdriverIO({ npmTag: 'latest', dev: true } as ProgramOpts)
expect(runProgram).toBeCalledWith(
'npm',
['install', '--save-dev', '@wdio/cli@latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledWith(
'npx',
['wdio', 'config', '--npm-tag', 'latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledTimes(2)
expect(fs.mkdir).toBeCalledTimes(0)
expect(fs.writeFile).toBeCalledTimes(0)
})
test('does not install the @wdio/cli package when the @wdio/cli package is already installed in the current project', async () => {
vi.stubEnv('npm_config_user_agent', 'npm/10.2.4 node/v20.11.0 darwin arm64 workspaces/false')
vi.mocked(resolve).mockReturnValue('/Users/user/dev/my-monorepo/package.json')
await createWebdriverIO({ npmTag: 'latest' } as ProgramOpts)
expect(runProgram).toBeCalledWith(
'npx',
['wdio', 'config', '--npm-tag', 'latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledTimes(1)
expect(fs.mkdir).toBeCalledTimes(0)
expect(fs.writeFile).toBeCalledTimes(0)
})
test('does not install the @wdio/cli package when the @wdio/cli package is already installed globally', async () => {
vi.stubEnv('npm_config_user_agent', 'npm/10.2.4 node/v20.11.0 darwin arm64 workspaces/false')
vi.mocked(execSync).mockReturnValue(`
├── @wdio/[email protected]
`)
await createWebdriverIO({ npmTag: 'latest' } as ProgramOpts)
expect(runProgram).toBeCalledWith(
'npx',
['wdio', 'config', '--npm-tag', 'latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledTimes(1)
expect(fs.mkdir).toBeCalledTimes(0)
expect(fs.writeFile).toBeCalledTimes(0)
})
test('runs the wdio config command with --yes when the yes option is set to true', async () => {
vi.stubEnv('npm_config_user_agent', 'npm/10.2.4 node/v20.11.0 darwin arm64 workspaces/false')
await createWebdriverIO({ npmTag: 'latest', yes: true } as ProgramOpts)
expect(runProgram).toBeCalledWith(
'npm',
['install', '@wdio/cli@latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledWith(
'npx',
['wdio', 'config', '--yes', '--npm-tag', 'latest'],
expect.any(Object)
)
expect(runProgram).toBeCalledTimes(2)
expect(fs.mkdir).toBeCalledTimes(0)
expect(fs.writeFile).toBeCalledTimes(0)
})
test('does create a package.json to be used by the wdio config command when one does not exist', async () => {
vi.stubEnv('npm_config_user_agent', 'npm/10.2.4 node/v20.11.0 darwin arm64 workspaces/false')
vi.mocked(fs.access).mockRejectedValue(new Error('not existing'))
await createWebdriverIO({ npmTag: 'next' } as ProgramOpts)
expect(runProgram).toBeCalledWith(
'npm',
['install', '@wdio/cli@next'],
expect.any(Object)
)
expect(runProgram).toBeCalledWith(
'npx',
['wdio', 'config', '--npm-tag', 'next'],
expect.any(Object)
)
expect(runProgram).toBeCalledTimes(2)
expect(fs.mkdir).toBeCalledTimes(1)
expect(fs.writeFile).toBeCalledTimes(1)
expect(fs.writeFile).toBeCalledWith(
expect.any(String),
JSON.stringify({ name: 'someProjectName', type: 'module' }, null, 2),
)
})
test('installs the next version when the npmTag option is set to "next"', async () => {
vi.stubEnv('npm_config_user_agent', 'npm/10.2.4 node/v20.11.0 darwin arm64 workspaces/false')
await createWebdriverIO({ npmTag: 'next' } as ProgramOpts)
expect(runProgram).toBeCalledWith(
'npm',
['install', '@wdio/cli@next'],
expect.any(Object)
)
expect(runProgram).toBeCalledWith(
'npx',
['wdio', 'config', '--npm-tag', 'next'],
expect.any(Object)
)
expect(runProgram).toBeCalledTimes(2)
expect(fs.mkdir).toBeCalledTimes(0)
expect(fs.writeFile).toBeCalledTimes(0)
})
afterEach(() => {
vi.mocked(fs.access).mockClear()
vi.mocked(fs.mkdir).mockClear()
vi.mocked(fs.writeFile).mockClear()
vi.mocked(runProgram).mockClear()
vi.mocked(resolve).mockClear()
vi.mocked(execSync).mockClear()
console.log = consoleLog
})