-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig-loader.test.js
More file actions
218 lines (174 loc) · 6.62 KB
/
config-loader.test.js
File metadata and controls
218 lines (174 loc) · 6.62 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
import assert from 'node:assert';
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { afterEach, beforeEach, describe, it } from 'node:test';
import {
getScreenshotPaths,
loadConfig,
} from '../../src/utils/config-loader.js';
describe('utils/config-loader', () => {
describe('getScreenshotPaths', () => {
it('returns default path when not configured', () => {
let paths = getScreenshotPaths({});
assert.strictEqual(paths.length, 1);
assert.ok(paths[0].includes('screenshots'));
});
it('returns single path from string config', () => {
let paths = getScreenshotPaths({
upload: { screenshotsDir: './my-screenshots' },
});
assert.strictEqual(paths.length, 1);
assert.ok(paths[0].includes('my-screenshots'));
});
it('returns multiple paths from array config', () => {
let paths = getScreenshotPaths({
upload: { screenshotsDir: ['./screenshots1', './screenshots2'] },
});
assert.strictEqual(paths.length, 2);
assert.ok(paths[0].includes('screenshots1'));
assert.ok(paths[1].includes('screenshots2'));
});
it('resolves paths relative to cwd', () => {
let paths = getScreenshotPaths({
upload: { screenshotsDir: './relative' },
});
assert.ok(paths[0].startsWith(process.cwd()));
});
});
describe('loadConfig', () => {
let testDir = join(process.cwd(), '.test-config-loader');
let originalCwd = process.cwd();
let originalEnv = {};
beforeEach(() => {
// Save original env vars
originalEnv = {
VIZZLY_TOKEN: process.env.VIZZLY_TOKEN,
VIZZLY_API_URL: process.env.VIZZLY_API_URL,
VIZZLY_BUILD_NAME: process.env.VIZZLY_BUILD_NAME,
VIZZLY_PARALLEL_ID: process.env.VIZZLY_PARALLEL_ID,
VIZZLY_HOME: process.env.VIZZLY_HOME,
};
// Clean env
delete process.env.VIZZLY_TOKEN;
delete process.env.VIZZLY_BUILD_NAME;
delete process.env.VIZZLY_PARALLEL_ID;
// Create test directory
if (existsSync(testDir)) {
rmSync(testDir, { recursive: true, force: true });
}
mkdirSync(testDir, { recursive: true });
process.chdir(testDir);
// Set VIZZLY_HOME to avoid reading real global config
process.env.VIZZLY_HOME = join(testDir, '.vizzly-home');
});
afterEach(() => {
process.chdir(originalCwd);
if (existsSync(testDir)) {
rmSync(testDir, { recursive: true, force: true });
}
// Restore original env vars
for (let [key, value] of Object.entries(originalEnv)) {
if (value !== undefined) {
process.env[key] = value;
} else {
delete process.env[key];
}
}
});
it('returns default config when no config file exists', async () => {
let config = await loadConfig();
assert.strictEqual(config.server.port, 47392);
assert.strictEqual(config.comparison.threshold, 2.0);
assert.strictEqual(config.upload.screenshotsDir, './screenshots');
});
it('loads config from vizzly.config.js', async () => {
writeFileSync(
join(testDir, 'vizzly.config.js'),
'export default { server: { port: 3000 }, comparison: { threshold: 1.5 } };'
);
let config = await loadConfig();
assert.strictEqual(config.server.port, 3000);
assert.strictEqual(config.comparison.threshold, 1.5);
});
it('applies CLI overrides', async () => {
let config = await loadConfig(null, {
token: 'cli-token',
port: '5000',
threshold: 0.5,
buildName: 'My Build',
environment: 'production',
});
assert.strictEqual(config.apiKey, 'cli-token');
assert.strictEqual(config.server.port, 5000);
assert.strictEqual(config.comparison.threshold, 0.5);
assert.strictEqual(config.build.name, 'My Build');
assert.strictEqual(config.build.environment, 'production');
});
it('applies environment variable overrides', async () => {
process.env.VIZZLY_TOKEN = 'env-token';
process.env.VIZZLY_PARALLEL_ID = 'parallel-123';
let config = await loadConfig();
assert.strictEqual(config.apiKey, 'env-token');
assert.strictEqual(config.parallelId, 'parallel-123');
});
it('applies VIZZLY_BUILD_NAME environment variable', async () => {
process.env.VIZZLY_BUILD_NAME = 'CI Build #123';
let config = await loadConfig();
assert.strictEqual(config.build.name, 'CI Build #123');
});
it('CLI buildName overrides VIZZLY_BUILD_NAME', async () => {
process.env.VIZZLY_BUILD_NAME = 'env-build-name';
let config = await loadConfig(null, { buildName: 'cli-build-name' });
assert.strictEqual(config.build.name, 'cli-build-name');
});
it('CLI token overrides env token', async () => {
process.env.VIZZLY_TOKEN = 'env-token';
let config = await loadConfig(null, { token: 'cli-token' });
assert.strictEqual(config.apiKey, 'cli-token');
});
it('applies branch/commit/message overrides', async () => {
let config = await loadConfig(null, {
branch: 'feature-branch',
commit: 'abc123',
message: 'Test commit',
});
assert.strictEqual(config.build.branch, 'feature-branch');
assert.strictEqual(config.build.commit, 'abc123');
assert.strictEqual(config.build.message, 'Test commit');
});
it('applies timeout and batch size overrides', async () => {
let config = await loadConfig(null, {
timeout: '60000',
batchSize: 20,
uploadTimeout: 45000,
});
assert.strictEqual(config.server.timeout, 60000);
assert.strictEqual(config.upload.batchSize, 20);
assert.strictEqual(config.upload.timeout, 45000);
});
it('applies baseline overrides', async () => {
let config = await loadConfig(null, {
baselineBuild: 'build-123',
baselineComparison: 'comparison-456',
});
assert.strictEqual(config.baselineBuildId, 'build-123');
assert.strictEqual(config.baselineComparisonId, 'comparison-456');
});
it('applies behavior flags', async () => {
let config = await loadConfig(null, {
eager: true,
wait: true,
allowNoToken: true,
});
assert.strictEqual(config.eager, true);
assert.strictEqual(config.wait, true);
assert.strictEqual(config.allowNoToken, true);
});
it('applies parallelId override', async () => {
let config = await loadConfig(null, {
parallelId: 'parallel-from-cli',
});
assert.strictEqual(config.parallelId, 'parallel-from-cli');
});
});
});