/
loadConfig.spec.js
184 lines (145 loc) Β· 4 KB
/
loadConfig.spec.js
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
import path from 'path'
import makeConsoleMock from 'consolemock'
import { loadConfig } from '../lib/loadConfig'
jest.mock('../lib/resolveConfig', () => ({
/** Unfortunately necessary due to non-ESM tests. */
resolveConfig: (configPath) => {
try {
return require.resolve(configPath)
} catch {
return configPath
}
},
}))
jest.unmock('execa')
/**
* This converts paths into `file://` urls, but this doesn't
* work with `import()` when using babel + jest.
*/
jest.mock('node:url', () => ({
pathToFileURL: (path) => path,
}))
// TODO: Never run tests in the project's WC because this might change source files git status
describe('loadConfig', () => {
const logger = makeConsoleMock()
beforeEach(() => {
logger.clearHistory()
})
it('should load JSON config file', async () => {
expect.assertions(1)
const { config } = await loadConfig(
{ configPath: path.join(__dirname, '__mocks__', 'my-config.json') },
logger
)
expect(config).toMatchInlineSnapshot(`
Object {
"*": "mytask",
}
`)
})
it('should load YAML config file', async () => {
expect.assertions(1)
const { config } = await loadConfig(
{ configPath: path.join(__dirname, '__mocks__', 'my-config.yml') },
logger
)
expect(config).toMatchInlineSnapshot(`
Object {
"*": "mytask",
}
`)
})
it('should load CommonJS config file from absolute path', async () => {
expect.assertions(1)
const { config } = await loadConfig(
{ configPath: path.join(__dirname, '__mocks__', 'advanced-config.js') },
logger
)
expect(config).toMatchInlineSnapshot(`
Object {
"*.css": [Function],
"*.js": [Function],
}
`)
})
it('should load CommonJS config file from relative path', async () => {
expect.assertions(1)
const { config } = await loadConfig(
{ configPath: path.join('test', '__mocks__', 'advanced-config.js') },
logger
)
expect(config).toMatchInlineSnapshot(`
Object {
"*.css": [Function],
"*.js": [Function],
}
`)
})
it('should load CommonJS config file from .cjs file', async () => {
expect.assertions(1)
const { config } = await loadConfig(
{ configPath: path.join('test', '__mocks__', 'my-config.cjs') },
logger
)
expect(config).toMatchInlineSnapshot(`
Object {
"*": "mytask",
}
`)
})
it('should load EMS config file from .mjs file', async () => {
expect.assertions(1)
const { config } = await loadConfig(
{
configPath: path.join('test', '__mocks__', 'esm-config.mjs'),
debug: true,
quiet: true,
},
logger
)
expect(config).toMatchInlineSnapshot(`
Object {
"*": "mytask",
}
`)
})
it('should load EMS config file from .js file', async () => {
expect.assertions(1)
const { config } = await loadConfig(
{
configPath: path.join('test', '__mocks__', 'esm-config-in-js.js'),
debug: true,
quiet: true,
},
logger
)
expect(config).toMatchInlineSnapshot(`
Object {
"*": "mytask",
}
`)
})
it('should load a CJS module when specified', async () => {
expect.assertions(1)
jest.mock('my-lint-staged-config')
const { config } = await loadConfig(
{ configPath: 'my-lint-staged-config', quiet: true, debug: true },
logger
)
expect(config).toMatchInlineSnapshot(`
Object {
"*": "mytask",
}
`)
})
it('should return empty object when config file is not found', async () => {
expect.assertions(1)
const result = await loadConfig({ cwd: '/' })
expect(result).toMatchInlineSnapshot(`Object {}`)
})
it('should return empty object when explicit config file is not found', async () => {
expect.assertions(1)
const result = await loadConfig({ configPath: 'fake-config-file.yml' }, logger)
expect(result).toMatchInlineSnapshot(`Object {}`)
})
})