From f6a2ca20d6472cb0f2b18dadd626d6db092f8767 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Thu, 28 Dec 2023 14:14:47 -0500 Subject: [PATCH] chore: improve jest tests with more precise assertions (#1912) --- lib/path.test.js | 44 +++++++------- lib/style.test.js | 104 ++++++++++++++++++++------------- lib/svgo-node.test.js | 34 +++++------ lib/svgo.test.js | 40 ++++++------- lib/xast.test.js | 12 ++-- test/cli/cli.test.js | 12 ++-- test/plugins/_index.test.js | 4 +- test/plugins/prefixIds.test.js | 6 +- test/svg2js/_index.test.js | 14 ++--- test/svgo/_index.test.js | 18 +++--- 10 files changed, 150 insertions(+), 138 deletions(-) diff --git a/lib/path.test.js b/lib/path.test.js index 91e6eaa9..7fe7c515 100644 --- a/lib/path.test.js +++ b/lib/path.test.js @@ -8,55 +8,55 @@ const { parsePathData, stringifyPathData } = require('./path.js'); describe('parse path data', () => { it('should allow spaces between commands', () => { - expect(parsePathData('M0 10 L \n\r\t20 30')).toEqual([ + expect(parsePathData('M0 10 L \n\r\t20 30')).toStrictEqual([ { command: 'M', args: [0, 10] }, { command: 'L', args: [20, 30] }, ]); }); it('should allow spaces and commas between arguments', () => { - expect(parsePathData('M0 , 10 L 20 \n\r\t30,40,50')).toEqual([ + expect(parsePathData('M0 , 10 L 20 \n\r\t30,40,50')).toStrictEqual([ { command: 'M', args: [0, 10] }, { command: 'L', args: [20, 30] }, { command: 'L', args: [40, 50] }, ]); }); it('should forbid commas before commands', () => { - expect(parsePathData(', M0 10')).toEqual([]); + expect(parsePathData(', M0 10')).toStrictEqual([]); }); it('should forbid commas between commands', () => { - expect(parsePathData('M0,10 , L 20,30')).toEqual([ + expect(parsePathData('M0,10 , L 20,30')).toStrictEqual([ { command: 'M', args: [0, 10] }, ]); }); it('should forbid commas between command name and argument', () => { - expect(parsePathData('M0,10 L,20,30')).toEqual([ + expect(parsePathData('M0,10 L,20,30')).toStrictEqual([ { command: 'M', args: [0, 10] }, ]); }); it('should forbid multiple commas in a row', () => { - expect(parsePathData('M0 , , 10')).toEqual([]); + expect(parsePathData('M0 , , 10')).toStrictEqual([]); }); it('should stop when unknown char appears', () => { - expect(parsePathData('M0 10 , L 20 #40')).toEqual([ + expect(parsePathData('M0 10 , L 20 #40')).toStrictEqual([ { command: 'M', args: [0, 10] }, ]); }); it('should stop when not enough arguments', () => { - expect(parsePathData('M0 10 L 20 L 30 40')).toEqual([ + expect(parsePathData('M0 10 L 20 L 30 40')).toStrictEqual([ { command: 'M', args: [0, 10] }, ]); }); it('should stop if moveto not the first command', () => { - expect(parsePathData('L 10 20')).toEqual([]); - expect(parsePathData('10 20')).toEqual([]); + expect(parsePathData('L 10 20')).toStrictEqual([]); + expect(parsePathData('10 20')).toStrictEqual([]); }); it('should stop on invalid scientific notation', () => { - expect(parsePathData('M 0 5e++1 L 0 0')).toEqual([ + expect(parsePathData('M 0 5e++1 L 0 0')).toStrictEqual([ { command: 'M', args: [0, 5] }, ]); }); it('should stop on invalid numbers', () => { - expect(parsePathData('M ...')).toEqual([]); + expect(parsePathData('M ...')).toStrictEqual([]); }); it('should handle arcs', () => { expect( @@ -71,7 +71,7 @@ describe('parse path data', () => { l 50,-25 `, ), - ).toEqual([ + ).toStrictEqual([ { command: 'M', args: [600, 350] }, { command: 'l', args: [50, -25] }, { command: 'a', args: [25, 25, -30, 0, 1, 50, -25] }, @@ -96,7 +96,7 @@ describe('stringify path data', () => { { command: 'H', args: [50] }, ], }), - ).toEqual('M0 0h10 20 30H40 50'); + ).toBe('M0 0h10 20 30H40 50'); }); it('should not combine sequence of moveto', () => { expect( @@ -108,7 +108,7 @@ describe('stringify path data', () => { { command: 'm', args: [40, 50] }, ], }), - ).toEqual('M0 0M10 10m20 30m40 50'); + ).toBe('M0 0M10 10m20 30m40 50'); }); it('should combine moveto and sequence of lineto', () => { expect( @@ -122,7 +122,7 @@ describe('stringify path data', () => { { command: 'L', args: [10, 10] }, ], }), - ).toEqual('m0 0 10 10M0 0l10 10M0 0 10 10'); + ).toBe('m0 0 10 10M0 0l10 10M0 0 10 10'); expect( stringifyPathData({ pathData: [ @@ -130,7 +130,7 @@ describe('stringify path data', () => { { command: 'L', args: [10, 10] }, ], }), - ).toEqual('M0 0 10 10'); + ).toBe('M0 0 10 10'); }); it('should avoid space before first, negative and decimals', () => { expect( @@ -142,7 +142,7 @@ describe('stringify path data', () => { { command: 'L', args: [7, 0.8] }, ], }), - ).toEqual('M0-1.2.3 4 5-.6 7 .8'); + ).toBe('M0-1.2.3 4 5-.6 7 .8'); }); it('should configure precision', () => { /** @@ -159,13 +159,13 @@ describe('stringify path data', () => { pathData, precision: 3, }), - ).toEqual('M0-1.988.3 3.142-.3-3.142 100 200'); + ).toBe('M0-1.988.3 3.142-.3-3.142 100 200'); expect( stringifyPathData({ pathData, precision: 0, }), - ).toEqual('M0-2 0 3 0-3 100 200'); + ).toBe('M0-2 0 3 0-3 100 200'); }); it('allows to avoid spaces after arc flags', () => { /** @@ -182,12 +182,12 @@ describe('stringify path data', () => { pathData, disableSpaceAfterFlags: false, }), - ).toEqual('M0 0A50 50 10 1 0 .2 20a50 50 10 1 0 .2 20 50 50 10 1 0 .2 20'); + ).toBe('M0 0A50 50 10 1 0 .2 20a50 50 10 1 0 .2 20 50 50 10 1 0 .2 20'); expect( stringifyPathData({ pathData, disableSpaceAfterFlags: true, }), - ).toEqual('M0 0A50 50 10 10.2 20a50 50 10 10.2 20 50 50 10 10.2 20'); + ).toBe('M0 0A50 50 10 10.2 20a50 50 10 10.2 20 50 50 10 10.2 20'); }); }); diff --git a/lib/style.test.js b/lib/style.test.js index b5e1b2c7..48a0779b 100644 --- a/lib/style.test.js +++ b/lib/style.test.js @@ -58,31 +58,35 @@ it('collects styles', () => { `); const stylesheet = collectStylesheet(root); - expect(computeStyle(stylesheet, getElementById(root, 'class'))).toEqual({ - fill: { type: 'static', inherited: false, value: 'red' }, - }); - expect(computeStyle(stylesheet, getElementById(root, 'two-classes'))).toEqual( + expect(computeStyle(stylesheet, getElementById(root, 'class'))).toStrictEqual( { - fill: { type: 'static', inherited: false, value: 'green' }, - stroke: { type: 'static', inherited: false, value: 'black' }, + fill: { type: 'static', inherited: false, value: 'red' }, }, ); - expect(computeStyle(stylesheet, getElementById(root, 'attribute'))).toEqual({ + expect( + computeStyle(stylesheet, getElementById(root, 'two-classes')), + ).toStrictEqual({ + fill: { type: 'static', inherited: false, value: 'green' }, + stroke: { type: 'static', inherited: false, value: 'black' }, + }); + expect( + computeStyle(stylesheet, getElementById(root, 'attribute')), + ).toStrictEqual({ fill: { type: 'static', inherited: false, value: 'purple' }, }); expect( computeStyle(stylesheet, getElementById(root, 'inline-style')), - ).toEqual({ + ).toStrictEqual({ fill: { type: 'static', inherited: false, value: 'grey' }, }); - expect(computeStyle(stylesheet, getElementById(root, 'inheritance'))).toEqual( - { - fill: { type: 'static', inherited: true, value: 'yellow' }, - }, - ); + expect( + computeStyle(stylesheet, getElementById(root, 'inheritance')), + ).toStrictEqual({ + fill: { type: 'static', inherited: true, value: 'yellow' }, + }); expect( computeStyle(stylesheet, getElementById(root, 'nested-inheritance')), - ).toEqual({ + ).toStrictEqual({ fill: { type: 'static', inherited: true, value: 'blue' }, }); }); @@ -107,12 +111,12 @@ it('prioritizes different kinds of styles', () => { const stylesheet = collectStylesheet(root); expect( computeStyle(stylesheet, getElementById(root, 'complex-selector')), - ).toEqual({ + ).toStrictEqual({ fill: { type: 'static', inherited: false, value: 'red' }, }); expect( computeStyle(stylesheet, getElementById(root, 'override-selector')), - ).toEqual({ + ).toStrictEqual({ fill: { type: 'static', inherited: false, value: 'blue' }, }); expect( @@ -120,12 +124,12 @@ it('prioritizes different kinds of styles', () => { stylesheet, getElementById(root, 'attribute-over-inheritance'), ), - ).toEqual({ + ).toStrictEqual({ fill: { type: 'static', inherited: false, value: 'orange' }, }); expect( computeStyle(stylesheet, getElementById(root, 'style-rule-over-attribute')), - ).toEqual({ + ).toStrictEqual({ fill: { type: 'static', inherited: false, value: 'blue' }, }); expect( @@ -133,7 +137,7 @@ it('prioritizes different kinds of styles', () => { stylesheet, getElementById(root, 'inline-style-over-style-rule'), ), - ).toEqual({ + ).toStrictEqual({ fill: { type: 'static', inherited: false, value: 'purple' }, }); }); @@ -153,7 +157,7 @@ it('prioritizes important styles', () => { const stylesheet = collectStylesheet(root); expect( computeStyle(stylesheet, getElementById(root, 'complex-selector')), - ).toEqual({ + ).toStrictEqual({ fill: { type: 'static', inherited: false, value: 'green' }, }); expect( @@ -161,7 +165,7 @@ it('prioritizes important styles', () => { stylesheet, getElementById(root, 'style-rule-over-inline-style'), ), - ).toEqual({ + ).toStrictEqual({ fill: { type: 'static', inherited: false, value: 'green' }, }); expect( @@ -169,7 +173,7 @@ it('prioritizes important styles', () => { stylesheet, getElementById(root, 'inline-style-over-style-rule'), ), - ).toEqual({ + ).toStrictEqual({ fill: { type: 'static', inherited: false, value: 'purple' }, }); }); @@ -195,23 +199,29 @@ it('treats at-rules and pseudo-classes as dynamic styles', () => { `); const stylesheet = collectStylesheet(root); - expect(computeStyle(stylesheet, getElementById(root, 'media-query'))).toEqual( + expect( + computeStyle(stylesheet, getElementById(root, 'media-query')), + ).toStrictEqual({ + fill: { type: 'dynamic', inherited: false }, + }); + expect(computeStyle(stylesheet, getElementById(root, 'hover'))).toStrictEqual( { fill: { type: 'dynamic', inherited: false }, }, ); - expect(computeStyle(stylesheet, getElementById(root, 'hover'))).toEqual({ - fill: { type: 'dynamic', inherited: false }, - }); - expect(computeStyle(stylesheet, getElementById(root, 'inherited'))).toEqual({ + expect( + computeStyle(stylesheet, getElementById(root, 'inherited')), + ).toStrictEqual({ fill: { type: 'dynamic', inherited: true }, }); expect( computeStyle(stylesheet, getElementById(root, 'inherited-overriden')), - ).toEqual({ + ).toStrictEqual({ fill: { type: 'static', inherited: false, value: 'blue' }, }); - expect(computeStyle(stylesheet, getElementById(root, 'static'))).toEqual({ + expect( + computeStyle(stylesheet, getElementById(root, 'static')), + ).toStrictEqual({ fill: { type: 'static', inherited: false, value: 'black' }, }); }); @@ -234,17 +244,19 @@ it('considers