-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.test.ts
More file actions
230 lines (192 loc) · 7.53 KB
/
config.test.ts
File metadata and controls
230 lines (192 loc) · 7.53 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
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
import fs from 'node:fs/promises'
import path from 'node:path'
import { pathToFileURL } from 'node:url'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import * as configModule from '../src/config.js'
// Mock fs module
vi.mock('node:fs/promises', () => ({
default: {
readFile: vi.fn(),
writeFile: vi.fn(),
},
}))
vi.mock('node:url', () => ({
pathToFileURL: vi.fn((file) => ({
href: `file://${file}`,
})),
}))
vi.mock('node:path', async (importOriginal) => {
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
const actual = await importOriginal<typeof import('node:path')>()
return {
default: {
...actual,
resolve: vi.fn((_, f) => path.posix.resolve(`/path/to/parser/${f}`)),
},
}
})
describe('config.ts', () => {
const VSCODE_REPORTER_PATH = path.resolve(__dirname, '../reporter.cjs')
const reporterPathUrl = pathToFileURL(VSCODE_REPORTER_PATH).href
beforeEach(() => {
// Reset mocks before each test
vi.resetAllMocks()
})
afterEach(() => {
// Restore spies and mocks after each test
vi.restoreAllMocks()
})
describe('createTempConfigFile', () => {
// Test for ESM format
it('should add reporter to an ESM config file if not present', async () => {
// Arrange
const filename = 'wdio.conf.ts'
const outDir = '/output/dir'
const mockConfig = `
import { defineConfig } from '@wdio/cli'
export const config = defineConfig({
runner: 'local',
specs: ['./test/specs/**/*.ts'],
capabilities: [{
browserName: 'chrome'
}],
logLevel: 'info',
services: ['chromedriver'],
})
`
vi.mocked(fs.readFile).mockResolvedValue(mockConfig)
vi.mocked(fs.writeFile).mockResolvedValue()
// Act
const result = await configModule.createTempConfigFile(filename, outDir)
// Assertq
expect(fs.readFile).toHaveBeenCalledWith(filename, { encoding: 'utf8' })
const writeFileCalls = vi.mocked(fs.writeFile).mock.calls
expect(writeFileCalls.length).toBe(1)
// Check that the output includes the reporter import
const outputContent = writeFileCalls[0][1] as string
expect(outputContent).toMatchSnapshot()
// Check the temp filename structure
expect(result).toContain('wdio-vscode-')
expect(result).toContain('.ts')
})
// Test for ESM format with existing reporters
it('should add reporter to an ESM config with existing reporters', async () => {
// Arrange
const filename = 'wdio.conf.ts'
const outDir = '/output/dir'
const mockConfig = `
import { defineConfig } from '@wdio/cli'
export const config = defineConfig({
runner: 'local',
specs: ['./test/specs/**/*.ts'],
reporters: ['spec', ['allure', { outputDir: 'allure-results' }]],
capabilities: [{
browserName: 'chrome'
}],
})
`
vi.mocked(fs.readFile).mockResolvedValue(mockConfig)
vi.mocked(fs.writeFile).mockResolvedValue()
// Act
await configModule.createTempConfigFile(filename, outDir)
// Assert
const writeFileCalls = vi.mocked(fs.writeFile).mock.calls
const outputContent = writeFileCalls[0][1] as string
// Check that the reporter import is added
expect(outputContent).toMatchSnapshot()
})
// Test for CJS format
it('should add reporter to a CJS config file if not present', async () => {
// Arrange
const filename = 'wdio.conf.js'
const outDir = '/output/dir'
const mockConfig = `
const { config } = require('@wdio/cli')
exports.config = {
runner: 'local',
specs: ['./test/specs/**/*.js'],
capabilities: [{
browserName: 'chrome'
}],
logLevel: 'info',
services: ['chromedriver'],
}
`
vi.mocked(fs.readFile).mockResolvedValue(mockConfig)
vi.mocked(fs.writeFile).mockResolvedValue()
// Act
const result = await configModule.createTempConfigFile(filename, outDir)
// Assert
expect(fs.readFile).toHaveBeenCalledWith(filename, { encoding: 'utf8' })
const writeFileCalls = vi.mocked(fs.writeFile).mock.calls
expect(writeFileCalls.length).toBe(1)
// Check that the output includes the reporter require
const outputContent = writeFileCalls[0][1] as string
// Check that reporters is added to config
expect(outputContent).toMatchSnapshot()
// Check the temp filename structure
expect(result).toContain('wdio-vscode-')
expect(result).toContain('.js')
})
// Test for module.exports format
it('should add reporter to a config using module.exports', async () => {
// Arrange
const filename = 'wdio.conf.js'
const outDir = '/output/dir'
const mockConfig = `
const path = require('path')
module.exports = {
runner: 'local',
specs: ['./test/specs/**/*.js'],
capabilities: [{
browserName: 'chrome'
}],
logLevel: 'info',
}
`
vi.mocked(fs.readFile).mockResolvedValue(mockConfig)
vi.mocked(fs.writeFile).mockResolvedValue()
// Act
await configModule.createTempConfigFile(filename, outDir)
// Assert
const writeFileCalls = vi.mocked(fs.writeFile).mock.calls
const outputContent = writeFileCalls[0][1] as string
// Check that the reporter require is added
expect(outputContent).toMatchSnapshot()
})
// Test when reporter import already exists
it('should not add duplicate reporter import if already present in ESM', async () => {
// Arrange
const filename = 'wdio.conf.ts'
const outDir = '/output/dir'
const mockConfig = `
import { defineConfig } from '@wdio/cli'
import VscodeJsonReporter from "${reporterPathUrl}"
export const config = defineConfig({
runner: 'local',
specs: ['./test/specs/**/*.ts'],
capabilities: [{
browserName: 'chrome'
}],
})
`
vi.mocked(fs.readFile).mockResolvedValue(mockConfig)
vi.mocked(fs.writeFile).mockResolvedValue()
// Act
await configModule.createTempConfigFile(filename, outDir)
// Assert
const writeFileCalls = vi.mocked(fs.writeFile).mock.calls
const outputContent = writeFileCalls[0][1] as string
// Count occurrences of import statement
const importMatches = outputContent.match(
new RegExp(
`import VscodeJsonReporter from "${reporterPathUrl.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}"`,
'g'
)
)
expect(importMatches).toHaveLength(1)
// Check that reporters is added to config
expect(outputContent).toMatchSnapshot()
})
})
})