Skip to content

Commit db05c57

Browse files
authored
chore: Use idiomatic Jest patterns to assert exceptions (#1909)
1 parent 433dcef commit db05c57

File tree

3 files changed

+34
-65
lines changed

3 files changed

+34
-65
lines changed

lib/svgo-node.test.js

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -156,56 +156,33 @@ describe('loadConfig', () => {
156156
});
157157

158158
test('fails when specified config does not exist', async () => {
159-
try {
160-
await loadConfig('{}');
161-
expect.fail('Config is loaded successfully');
162-
} catch (error) {
163-
expect(error.message).toMatch(/Cannot find module/);
164-
}
159+
await expect(loadConfig('{}')).rejects.toThrow(/Cannot find module/);
165160
});
166161

167162
test('fails when exported config not an object', async () => {
168-
try {
169-
await loadConfig(path.join(fixtures, 'invalid-null.js'));
170-
expect.fail('Config is loaded successfully');
171-
} catch (error) {
172-
expect(error.message).toMatch(/Invalid config file/);
173-
}
174-
try {
175-
await loadConfig(path.join(fixtures, 'invalid-array.js'));
176-
expect.fail('Config is loaded successfully');
177-
} catch (error) {
178-
expect(error.message).toMatch(/Invalid config file/);
179-
}
180-
try {
181-
await loadConfig(path.join(fixtures, 'invalid-string.js'));
182-
expect.fail('Config is loaded successfully');
183-
} catch (error) {
184-
expect(error.message).toMatch(/Invalid config file/);
185-
}
163+
await expect(
164+
loadConfig(path.join(fixtures, 'invalid-null.js')),
165+
).rejects.toThrow(/Invalid config file/);
166+
await expect(
167+
loadConfig(path.join(fixtures, 'invalid-array.js')),
168+
).rejects.toThrow(/Invalid config file/);
169+
await expect(
170+
loadConfig(path.join(fixtures, 'invalid-string.js')),
171+
).rejects.toThrow(/Invalid config file/);
186172
});
187173

188174
test('handles runtime errors properly', async () => {
189-
try {
190-
await loadConfig(path.join(fixtures, 'invalid-runtime.js'));
191-
expect.fail('Config is loaded successfully');
192-
} catch (error) {
193-
expect(error.message).toMatch(/plugins is not defined/);
194-
}
195-
try {
196-
await loadConfig(path.join(fixtures, 'invalid-runtime.mjs'));
197-
expect.fail('Config is loaded successfully');
198-
} catch (error) {
199-
expect(error.message).toMatch(/plugins is not defined/);
200-
}
175+
await expect(
176+
loadConfig(path.join(fixtures, 'invalid-runtime.js')),
177+
).rejects.toThrow(/plugins is not defined/);
178+
await expect(
179+
loadConfig(path.join(fixtures, 'invalid-runtime.mjs')),
180+
).rejects.toThrow(/plugins is not defined/);
201181
});
202182

203183
test('handles MODULE_NOT_FOUND properly', async () => {
204-
try {
205-
await loadConfig(path.join(fixtures, 'module-not-found.js'));
206-
expect.fail('Config is loaded successfully');
207-
} catch (error) {
208-
expect(error.message).toMatch(/Cannot find module 'unknown-module'/);
209-
}
184+
await expect(
185+
loadConfig(path.join(fixtures, 'module-not-found.js')),
186+
).rejects.toThrow(/Cannot find module 'unknown-module'/);
210187
});
211188
});

lib/svgo.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,13 @@ test('plugin precision should override preset precision', () => {
267267
});
268268

269269
test('provides informative error in result', () => {
270+
expect.assertions(6);
270271
const svg = `<svg viewBox="0 0 120 120">
271272
<circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
272273
</svg>
273274
`;
274275
try {
275276
optimize(svg, { path: 'test.svg' });
276-
expect(true).toEqual(false);
277277
} catch (error) {
278278
expect(error.name).toEqual('SvgoParserError');
279279
expect(error.message).toEqual('test.svg:2:33: Unquoted attribute value');
@@ -285,13 +285,13 @@ test('provides informative error in result', () => {
285285
});
286286

287287
test('provides code snippet in rendered error', () => {
288+
expect.assertions(1);
288289
const svg = `<svg viewBox="0 0 120 120">
289290
<circle fill="#ff0000" cx=60.444444" cy="60" r="50"/>
290291
</svg>
291292
`;
292293
try {
293294
optimize(svg, { path: 'test.svg' });
294-
expect(true).toEqual(false);
295295
} catch (error) {
296296
expect(error.toString())
297297
.toEqual(`SvgoParserError: test.svg:2:29: Unquoted attribute value
@@ -306,6 +306,7 @@ test('provides code snippet in rendered error', () => {
306306
});
307307

308308
test('supports errors without path', () => {
309+
expect.assertions(1);
309310
const svg = `<svg viewBox="0 0 120 120">
310311
<circle/>
311312
<circle/>
@@ -321,7 +322,6 @@ test('supports errors without path', () => {
321322
`;
322323
try {
323324
optimize(svg);
324-
expect(true).toEqual(false);
325325
} catch (error) {
326326
expect(error.toString())
327327
.toEqual(`SvgoParserError: <input>:11:29: Unquoted attribute value
@@ -337,13 +337,13 @@ test('supports errors without path', () => {
337337
});
338338

339339
test('slices long line in error code snippet', () => {
340+
expect.assertions(1);
340341
const svg = `<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" viewBox="0 0 230 120">
341342
<path d="M318.198 551.135 530.33 918.56l-289.778-77.646 38.823-144.889c77.646-289.778 294.98-231.543 256.156-86.655s178.51 203.124 217.334 58.235q58.234-217.334 250.955 222.534t579.555 155.292z stroke-width="1.5" fill="red" stroke="red" />
342343
</svg>
343344
`;
344345
try {
345346
optimize(svg);
346-
expect(true).toEqual(false);
347347
} catch (error) {
348348
expect(error.toString())
349349
.toEqual(`SvgoParserError: <input>:2:211: Invalid attribute name

test/coa/_index.test.js

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,11 @@ describe('coa', function () {
124124
it('should throw error when stated in input folder does not exist', async () => {
125125
replaceConsoleError();
126126
try {
127-
await runProgram([
128-
'--input',
129-
svgFolderPath + 'temp',
130-
'--output',
131-
tempFolder,
132-
]);
133-
} catch (error) {
127+
await expect(
128+
runProgram(['--input', svgFolderPath + 'temp', '--output', tempFolder]),
129+
).rejects.toThrow(/no such file or directory/);
130+
} finally {
134131
restoreConsoleError();
135-
expect(error.message).toMatch(/no such file or directory/);
136132
}
137133
});
138134

@@ -142,23 +138,19 @@ describe('coa', function () {
142138
if (!fs.existsSync(emptyFolderPath)) {
143139
fs.mkdirSync(emptyFolderPath);
144140
}
145-
try {
146-
await runProgram(['--folder', emptyFolderPath, '--quiet']);
147-
} catch (error) {
148-
expect(error.message).toMatch(/No SVG files/);
149-
}
141+
await expect(
142+
runProgram(['--folder', emptyFolderPath, '--quiet']),
143+
).rejects.toThrow(/No SVG files/);
150144
});
151145

152146
it('should show message when folder does not consists any svg files', async () => {
153-
try {
154-
await runProgram([
147+
await expect(
148+
runProgram([
155149
'--folder',
156150
path.resolve(__dirname, 'testFolderWithNoSvg'),
157151
'--quiet',
158-
]);
159-
} catch (error) {
160-
expect(error.message).toMatch(/No SVG files have been found/);
161-
}
152+
]),
153+
).rejects.toThrow(/No SVG files have been found/);
162154
});
163155
});
164156
});

0 commit comments

Comments
 (0)