-
Notifications
You must be signed in to change notification settings - Fork 70
/
getConfiguration.test.ts
174 lines (148 loc) · 5.14 KB
/
getConfiguration.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
import { readFileSync } from 'fs';
import { beforeEach, expect, it, vi } from 'vitest';
import { getConfiguration } from './getConfiguration';
vi.mock('fs');
const mockedReadFile = vi.mocked(readFileSync);
beforeEach(() => {
mockedReadFile.mockReset();
});
it('reads configuration successfully', async () => {
mockedReadFile.mockReturnValue(
JSON.stringify({
projectId: 'project-id',
projectToken: 'project-token',
onlyChanged: 'only-changed',
traceChanged: 'expanded',
onlyStoryFiles: ['only-story-files'],
onlyStoryNames: ['only-story-names'],
untraced: ['untraced'],
externals: ['externals'],
debug: true,
diagnosticsFile: 'diagnostics-file',
fileHashing: true,
junitReport: 'junit-report',
zip: true,
autoAcceptChanges: 'auto-accept-changes',
exitZeroOnChanges: 'exit-zero-on-changes',
exitOnceUploaded: 'exit-once-uploaded',
ignoreLastBuildOnBranch: 'ignore-last-build-on-branch',
buildScriptName: 'build-script-name',
outputDir: 'output-dir',
skip: 'skip',
storybookBuildDir: 'storybook-build-dir',
storybookBaseDir: 'storybook-base-dir',
storybookConfigDir: 'storybook-config-dir',
storybookLogFile: 'storybook-log-file',
logFile: 'log-file',
uploadMetadata: true,
})
);
expect(await getConfiguration()).toEqual({
configFile: 'chromatic.config.json',
projectId: 'project-id',
projectToken: 'project-token',
onlyChanged: 'only-changed',
traceChanged: 'expanded',
onlyStoryFiles: ['only-story-files'],
onlyStoryNames: ['only-story-names'],
untraced: ['untraced'],
externals: ['externals'],
debug: true,
diagnosticsFile: 'diagnostics-file',
fileHashing: true,
junitReport: 'junit-report',
zip: true,
autoAcceptChanges: 'auto-accept-changes',
exitZeroOnChanges: 'exit-zero-on-changes',
exitOnceUploaded: 'exit-once-uploaded',
ignoreLastBuildOnBranch: 'ignore-last-build-on-branch',
buildScriptName: 'build-script-name',
outputDir: 'output-dir',
skip: 'skip',
storybookBuildDir: 'storybook-build-dir',
storybookBaseDir: 'storybook-base-dir',
storybookConfigDir: 'storybook-config-dir',
storybookLogFile: 'storybook-log-file',
logFile: 'log-file',
uploadMetadata: true,
});
});
it('handles other side of union options', async () => {
mockedReadFile.mockReturnValue(
JSON.stringify({
onlyChanged: true,
traceChanged: true,
diagnosticsFile: true,
junitReport: true,
autoAcceptChanges: true,
exitZeroOnChanges: true,
exitOnceUploaded: true,
skip: true,
storybookLogFile: true,
logFile: true,
})
);
expect(await getConfiguration()).toEqual({
configFile: 'chromatic.config.json',
onlyChanged: true,
traceChanged: true,
diagnosticsFile: true,
junitReport: true,
autoAcceptChanges: true,
exitZeroOnChanges: true,
exitOnceUploaded: true,
skip: true,
storybookLogFile: true,
logFile: true,
});
});
it('reads from chromatic.config.json by default', async () => {
mockedReadFile.mockReturnValue(JSON.stringify({ projectToken: 'json-file-token' })).mockClear();
await getConfiguration();
expect(mockedReadFile).toHaveBeenCalledWith('chromatic.config.json', 'utf8');
});
it('can read from a different location', async () => {
mockedReadFile.mockReturnValue(JSON.stringify({ projectToken: 'json-file-token' })).mockClear();
await getConfiguration('test.file');
expect(mockedReadFile).toHaveBeenCalledWith('test.file', 'utf8');
});
it('returns nothing if there is no config file and it was not specified', async () => {
mockedReadFile.mockImplementation(() => {
throw new Error('ENOENT');
});
expect(await getConfiguration()).toEqual({});
});
it('returns nothing if there is no config file and it was specified', async () => {
mockedReadFile.mockImplementation(() => {
throw new Error('ENOENT');
});
await expect(getConfiguration('test.file')).rejects.toThrow(/could not be found/);
});
it('errors if config file contains invalid data', async () => {
mockedReadFile.mockReturnValue(JSON.stringify({ projectToken: 1 }));
await expect(getConfiguration('test.file')).rejects.toThrow(/projectToken/);
});
it('errors if config file contains unknown keys', async () => {
mockedReadFile.mockReturnValue(JSON.stringify({ random: 1 }));
await expect(getConfiguration('test.file')).rejects.toThrow(/random/);
});
it('errors if config file is unparseable', async () => {
{
mockedReadFile.mockReturnValue('invalid json');
await expect(getConfiguration('test.file')).rejects.toThrow(
/Configuration file .+ could not be parsed(.|\n)*Unexpected token/
);
}
{
mockedReadFile.mockReturnValue('{ "foo": 1 "unexpectedString": 2 }');
await expect(getConfiguration('test.file')).rejects.toThrow(
/Configuration file .+ could not be parsed(.|\n)*Unexpected string/m
);
}
{
mockedReadFile.mockReturnValue('{ "unexpectedEnd": ');
await expect(getConfiguration('test.file')).rejects.toThrow(
/Configuration file .+ could not be parsed(.|\n)*Unexpected end/m
);
}
});