From 024a92246c6e3af78c0ffd5feb31023890568ccd Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Wed, 27 Dec 2023 03:44:30 -0500 Subject: [PATCH] Use idiomatic Jest patterns to assert exceptions Avoid unnecessary assertions such as `expect(true).toBe(false)`. Instead, assert the exception using the `toThrow()` matcher. Where that is insufficient, use `expect.assertions()`. Unwrap exceptions in promises using `expect.rejects`. These patterns are documented at: - https://jestjs.io/docs/expect#tothrowerror - https://jestjs.io/docs/tutorial-async#rejects - https://jestjs.io/docs/expect#expectassertionsnumber --- lib/svgo-node.test.js | 61 +++++++++++++---------------------------- lib/svgo.test.js | 8 +++--- test/coa/_index.test.js | 30 ++++++++------------ 3 files changed, 34 insertions(+), 65 deletions(-) diff --git a/lib/svgo-node.test.js b/lib/svgo-node.test.js index 1b474e4af..2cf23dff0 100644 --- a/lib/svgo-node.test.js +++ b/lib/svgo-node.test.js @@ -156,56 +156,33 @@ describe('loadConfig', () => { }); test('fails when specified config does not exist', async () => { - try { - await loadConfig('{}'); - expect.fail('Config is loaded successfully'); - } catch (error) { - expect(error.message).toMatch(/Cannot find module/); - } + await expect(loadConfig('{}')).rejects.toThrow(/Cannot find module/); }); test('fails when exported config not an object', async () => { - try { - await loadConfig(path.join(fixtures, 'invalid-null.js')); - expect.fail('Config is loaded successfully'); - } catch (error) { - expect(error.message).toMatch(/Invalid config file/); - } - try { - await loadConfig(path.join(fixtures, 'invalid-array.js')); - expect.fail('Config is loaded successfully'); - } catch (error) { - expect(error.message).toMatch(/Invalid config file/); - } - try { - await loadConfig(path.join(fixtures, 'invalid-string.js')); - expect.fail('Config is loaded successfully'); - } catch (error) { - expect(error.message).toMatch(/Invalid config file/); - } + await expect( + loadConfig(path.join(fixtures, 'invalid-null.js')), + ).rejects.toThrow(/Invalid config file/); + await expect( + loadConfig(path.join(fixtures, 'invalid-array.js')), + ).rejects.toThrow(/Invalid config file/); + await expect( + loadConfig(path.join(fixtures, 'invalid-string.js')), + ).rejects.toThrow(/Invalid config file/); }); test('handles runtime errors properly', async () => { - try { - await loadConfig(path.join(fixtures, 'invalid-runtime.js')); - expect.fail('Config is loaded successfully'); - } catch (error) { - expect(error.message).toMatch(/plugins is not defined/); - } - try { - await loadConfig(path.join(fixtures, 'invalid-runtime.mjs')); - expect.fail('Config is loaded successfully'); - } catch (error) { - expect(error.message).toMatch(/plugins is not defined/); - } + await expect( + loadConfig(path.join(fixtures, 'invalid-runtime.js')), + ).rejects.toThrow(/plugins is not defined/); + await expect( + loadConfig(path.join(fixtures, 'invalid-runtime.mjs')), + ).rejects.toThrow(/plugins is not defined/); }); test('handles MODULE_NOT_FOUND properly', async () => { - try { - await loadConfig(path.join(fixtures, 'module-not-found.js')); - expect.fail('Config is loaded successfully'); - } catch (error) { - expect(error.message).toMatch(/Cannot find module 'unknown-module'/); - } + await expect( + loadConfig(path.join(fixtures, 'module-not-found.js')), + ).rejects.toThrow(/Cannot find module 'unknown-module'/); }); }); diff --git a/lib/svgo.test.js b/lib/svgo.test.js index 68fb8d7ea..468a894ab 100644 --- a/lib/svgo.test.js +++ b/lib/svgo.test.js @@ -267,13 +267,13 @@ test('plugin precision should override preset precision', () => { }); test('provides informative error in result', () => { + expect.assertions(6); const svg = ` `; try { optimize(svg, { path: 'test.svg' }); - expect(true).toEqual(false); } catch (error) { expect(error.name).toEqual('SvgoParserError'); expect(error.message).toEqual('test.svg:2:33: Unquoted attribute value'); @@ -285,13 +285,13 @@ test('provides informative error in result', () => { }); test('provides code snippet in rendered error', () => { + expect.assertions(1); const svg = ` `; try { optimize(svg, { path: 'test.svg' }); - expect(true).toEqual(false); } catch (error) { expect(error.toString()) .toEqual(`SvgoParserError: test.svg:2:29: Unquoted attribute value @@ -306,6 +306,7 @@ test('provides code snippet in rendered error', () => { }); test('supports errors without path', () => { + expect.assertions(1); const svg = ` @@ -321,7 +322,6 @@ test('supports errors without path', () => { `; try { optimize(svg); - expect(true).toEqual(false); } catch (error) { expect(error.toString()) .toEqual(`SvgoParserError: