1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-31 07:44:22 +03:00

Migrate to jest (#1520)

Mocha doesn't have a lot of features provided by jest.
There is a great assertion library out of the box.
And the most cool feature is inline snapshots.
Mocha also hides errors which makes debugging a nightmare sometimes.
This commit is contained in:
Bogdan Chadkin
2021-08-12 18:06:10 +03:00
committed by GitHub
parent 862c43ec64
commit c3695ae533
12 changed files with 2979 additions and 621 deletions

View File

@ -1,59 +1,58 @@
'use strict'; 'use strict';
const { expect } = require('chai');
const { parsePathData, stringifyPathData } = require('./path.js'); const { parsePathData, stringifyPathData } = require('./path.js');
describe('parse path data', () => { describe('parse path data', () => {
it('should allow spaces between commands', () => { it('should allow spaces between commands', () => {
expect(parsePathData('M0 10 L \n\r\t20 30')).to.deep.equal([ expect(parsePathData('M0 10 L \n\r\t20 30')).toEqual([
{ command: 'M', args: [0, 10] }, { command: 'M', args: [0, 10] },
{ command: 'L', args: [20, 30] }, { command: 'L', args: [20, 30] },
]); ]);
}); });
it('should allow spaces and commas between arguments', () => { it('should allow spaces and commas between arguments', () => {
expect(parsePathData('M0 , 10 L 20 \n\r\t30,40,50')).to.deep.equal([ expect(parsePathData('M0 , 10 L 20 \n\r\t30,40,50')).toEqual([
{ command: 'M', args: [0, 10] }, { command: 'M', args: [0, 10] },
{ command: 'L', args: [20, 30] }, { command: 'L', args: [20, 30] },
{ command: 'L', args: [40, 50] }, { command: 'L', args: [40, 50] },
]); ]);
}); });
it('should forbid commas before commands', () => { it('should forbid commas before commands', () => {
expect(parsePathData(', M0 10')).to.deep.equal([]); expect(parsePathData(', M0 10')).toEqual([]);
}); });
it('should forbid commas between commands', () => { it('should forbid commas between commands', () => {
expect(parsePathData('M0,10 , L 20,30')).to.deep.equal([ expect(parsePathData('M0,10 , L 20,30')).toEqual([
{ command: 'M', args: [0, 10] }, { command: 'M', args: [0, 10] },
]); ]);
}); });
it('should forbid commas between command name and argument', () => { it('should forbid commas between command name and argument', () => {
expect(parsePathData('M0,10 L,20,30')).to.deep.equal([ expect(parsePathData('M0,10 L,20,30')).toEqual([
{ command: 'M', args: [0, 10] }, { command: 'M', args: [0, 10] },
]); ]);
}); });
it('should forbid multipe commas in a row', () => { it('should forbid multipe commas in a row', () => {
expect(parsePathData('M0 , , 10')).to.deep.equal([]); expect(parsePathData('M0 , , 10')).toEqual([]);
}); });
it('should stop when unknown char appears', () => { it('should stop when unknown char appears', () => {
expect(parsePathData('M0 10 , L 20 #40')).to.deep.equal([ expect(parsePathData('M0 10 , L 20 #40')).toEqual([
{ command: 'M', args: [0, 10] }, { command: 'M', args: [0, 10] },
]); ]);
}); });
it('should stop when not enough arguments', () => { it('should stop when not enough arguments', () => {
expect(parsePathData('M0 10 L 20 L 30 40')).to.deep.equal([ expect(parsePathData('M0 10 L 20 L 30 40')).toEqual([
{ command: 'M', args: [0, 10] }, { command: 'M', args: [0, 10] },
]); ]);
}); });
it('should stop if moveto not the first command', () => { it('should stop if moveto not the first command', () => {
expect(parsePathData('L 10 20')).to.deep.equal([]); expect(parsePathData('L 10 20')).toEqual([]);
expect(parsePathData('10 20')).to.deep.equal([]); expect(parsePathData('10 20')).toEqual([]);
}); });
it('should stop on invalid scientific notation', () => { it('should stop on invalid scientific notation', () => {
expect(parsePathData('M 0 5e++1 L 0 0')).to.deep.equal([ expect(parsePathData('M 0 5e++1 L 0 0')).toEqual([
{ command: 'M', args: [0, 5] }, { command: 'M', args: [0, 5] },
]); ]);
}); });
it('should stop on invalid numbers', () => { it('should stop on invalid numbers', () => {
expect(parsePathData('M ...')).to.deep.equal([]); expect(parsePathData('M ...')).toEqual([]);
}); });
it('should handle arcs', () => { it('should handle arcs', () => {
expect( expect(
@ -68,7 +67,7 @@ describe('parse path data', () => {
l 50,-25 l 50,-25
` `
) )
).to.deep.equal([ ).toEqual([
{ command: 'M', args: [600, 350] }, { command: 'M', args: [600, 350] },
{ command: 'l', args: [50, -25] }, { command: 'l', args: [50, -25] },
{ command: 'a', args: [25, 25, -30, 0, 1, 50, -25] }, { command: 'a', args: [25, 25, -30, 0, 1, 50, -25] },
@ -93,7 +92,7 @@ describe('stringify path data', () => {
{ command: 'H', args: [50] }, { command: 'H', args: [50] },
], ],
}) })
).to.equal('M0 0h10 20 30H40 50'); ).toEqual('M0 0h10 20 30H40 50');
}); });
it('should not combine sequence of moveto', () => { it('should not combine sequence of moveto', () => {
expect( expect(
@ -105,7 +104,7 @@ describe('stringify path data', () => {
{ command: 'm', args: [40, 50] }, { command: 'm', args: [40, 50] },
], ],
}) })
).to.equal('M0 0M10 10m20 30m40 50'); ).toEqual('M0 0M10 10m20 30m40 50');
}); });
it('should combine moveto and sequence of lineto', () => { it('should combine moveto and sequence of lineto', () => {
expect( expect(
@ -119,7 +118,7 @@ describe('stringify path data', () => {
{ command: 'L', args: [10, 10] }, { command: 'L', args: [10, 10] },
], ],
}) })
).to.equal('m0 0 10 10M0 0l10 10M0 0 10 10'); ).toEqual('m0 0 10 10M0 0l10 10M0 0 10 10');
expect( expect(
stringifyPathData({ stringifyPathData({
pathData: [ pathData: [
@ -127,7 +126,7 @@ describe('stringify path data', () => {
{ command: 'L', args: [10, 10] }, { command: 'L', args: [10, 10] },
], ],
}) })
).to.equal('M0 0 10 10'); ).toEqual('M0 0 10 10');
}); });
it('should avoid space before first, negative and decimals', () => { it('should avoid space before first, negative and decimals', () => {
expect( expect(
@ -139,7 +138,7 @@ describe('stringify path data', () => {
{ command: 'L', args: [7, 0.8] }, { command: 'L', args: [7, 0.8] },
], ],
}) })
).to.equal('M0-1.2.3 4 5-.6 7 .8'); ).toEqual('M0-1.2.3 4 5-.6 7 .8');
}); });
it('should configure precision', () => { it('should configure precision', () => {
const pathData = [ const pathData = [
@ -153,13 +152,13 @@ describe('stringify path data', () => {
pathData, pathData,
precision: 3, precision: 3,
}) })
).to.equal('M0-1.988.3 3.142-.3-3.142 100 200'); ).toEqual('M0-1.988.3 3.142-.3-3.142 100 200');
expect( expect(
stringifyPathData({ stringifyPathData({
pathData, pathData,
precision: 0, precision: 0,
}) })
).to.equal('M0-2 0 3 0-3 100 200'); ).toEqual('M0-2 0 3 0-3 100 200');
}); });
it('allows to avoid spaces after arc flags', () => { it('allows to avoid spaces after arc flags', () => {
const pathData = [ const pathData = [
@ -172,12 +171,12 @@ describe('stringify path data', () => {
pathData, pathData,
disableSpaceAfterFlags: false, disableSpaceAfterFlags: false,
}) })
).to.equal('M0 0A50 50 10 1 0 .2 20a50 50 10 1 0 .2 20'); ).toEqual('M0 0A50 50 10 1 0 .2 20a50 50 10 1 0 .2 20');
expect( expect(
stringifyPathData({ stringifyPathData({
pathData, pathData,
disableSpaceAfterFlags: true, disableSpaceAfterFlags: true,
}) })
).to.equal('M0 0A50 50 10 10.2 20a50 50 10 10.2 20'); ).toEqual('M0 0A50 50 10 10.2 20a50 50 10 10.2 20');
}); });
}); });

View File

@ -1,6 +1,5 @@
'use strict'; 'use strict';
const { expect } = require('chai');
const { collectStylesheet, computeStyle } = require('./style.js'); const { collectStylesheet, computeStyle } = require('./style.js');
const { querySelector } = require('./xast.js'); const { querySelector } = require('./xast.js');
const svg2js = require('./svgo/svg2js.js'); const svg2js = require('./svgo/svg2js.js');
@ -32,35 +31,33 @@ describe('computeStyle', () => {
</svg> </svg>
`); `);
const stylesheet = collectStylesheet(root); const stylesheet = collectStylesheet(root);
expect( expect(computeStyle(stylesheet, querySelector(root, '#class'))).toEqual({
computeStyle(stylesheet, querySelector(root, '#class'))
).to.deep.equal({
fill: { type: 'static', inherited: false, value: 'red' }, fill: { type: 'static', inherited: false, value: 'red' },
}); });
expect( expect(
computeStyle(stylesheet, querySelector(root, '#two-classes')) computeStyle(stylesheet, querySelector(root, '#two-classes'))
).to.deep.equal({ ).toEqual({
fill: { type: 'static', inherited: false, value: 'green' }, fill: { type: 'static', inherited: false, value: 'green' },
stroke: { type: 'static', inherited: false, value: 'black' }, stroke: { type: 'static', inherited: false, value: 'black' },
}); });
expect( expect(computeStyle(stylesheet, querySelector(root, '#attribute'))).toEqual(
computeStyle(stylesheet, querySelector(root, '#attribute')) {
).to.deep.equal({
fill: { type: 'static', inherited: false, value: 'purple' }, fill: { type: 'static', inherited: false, value: 'purple' },
}); }
);
expect( expect(
computeStyle(stylesheet, querySelector(root, '#inline-style')) computeStyle(stylesheet, querySelector(root, '#inline-style'))
).to.deep.equal({ ).toEqual({
fill: { type: 'static', inherited: false, value: 'grey' }, fill: { type: 'static', inherited: false, value: 'grey' },
}); });
expect( expect(
computeStyle(stylesheet, querySelector(root, '#inheritance')) computeStyle(stylesheet, querySelector(root, '#inheritance'))
).to.deep.equal({ ).toEqual({
fill: { type: 'static', inherited: true, value: 'yellow' }, fill: { type: 'static', inherited: true, value: 'yellow' },
}); });
expect( expect(
computeStyle(stylesheet, querySelector(root, '#nested-inheritance')) computeStyle(stylesheet, querySelector(root, '#nested-inheritance'))
).to.deep.equal({ ).toEqual({
fill: { type: 'static', inherited: true, value: 'blue' }, fill: { type: 'static', inherited: true, value: 'blue' },
}); });
}); });
@ -84,7 +81,7 @@ describe('computeStyle', () => {
const stylesheet = collectStylesheet(root); const stylesheet = collectStylesheet(root);
expect( expect(
computeStyle(stylesheet, querySelector(root, '#complex-selector')) computeStyle(stylesheet, querySelector(root, '#complex-selector'))
).to.deep.equal({ ).toEqual({
fill: { type: 'static', inherited: false, value: 'red' }, fill: { type: 'static', inherited: false, value: 'red' },
}); });
expect( expect(
@ -92,7 +89,7 @@ describe('computeStyle', () => {
stylesheet, stylesheet,
querySelector(root, '#attribute-over-inheritance') querySelector(root, '#attribute-over-inheritance')
) )
).to.deep.equal({ ).toEqual({
fill: { type: 'static', inherited: false, value: 'orange' }, fill: { type: 'static', inherited: false, value: 'orange' },
}); });
expect( expect(
@ -100,7 +97,7 @@ describe('computeStyle', () => {
stylesheet, stylesheet,
querySelector(root, '#style-rule-over-attribute') querySelector(root, '#style-rule-over-attribute')
) )
).to.deep.equal({ ).toEqual({
fill: { type: 'static', inherited: false, value: 'blue' }, fill: { type: 'static', inherited: false, value: 'blue' },
}); });
expect( expect(
@ -108,7 +105,7 @@ describe('computeStyle', () => {
stylesheet, stylesheet,
querySelector(root, '#inline-style-over-style-rule') querySelector(root, '#inline-style-over-style-rule')
) )
).to.deep.equal({ ).toEqual({
fill: { type: 'static', inherited: false, value: 'purple' }, fill: { type: 'static', inherited: false, value: 'purple' },
}); });
}); });
@ -128,7 +125,7 @@ describe('computeStyle', () => {
const stylesheet = collectStylesheet(root); const stylesheet = collectStylesheet(root);
expect( expect(
computeStyle(stylesheet, querySelector(root, '#complex-selector')) computeStyle(stylesheet, querySelector(root, '#complex-selector'))
).to.deep.equal({ ).toEqual({
fill: { type: 'static', inherited: false, value: 'green' }, fill: { type: 'static', inherited: false, value: 'green' },
}); });
expect( expect(
@ -136,7 +133,7 @@ describe('computeStyle', () => {
stylesheet, stylesheet,
querySelector(root, '#style-rule-over-inline-style') querySelector(root, '#style-rule-over-inline-style')
) )
).to.deep.equal({ ).toEqual({
fill: { type: 'static', inherited: false, value: 'green' }, fill: { type: 'static', inherited: false, value: 'green' },
}); });
expect( expect(
@ -144,7 +141,7 @@ describe('computeStyle', () => {
stylesheet, stylesheet,
querySelector(root, '#inline-style-over-style-rule') querySelector(root, '#inline-style-over-style-rule')
) )
).to.deep.equal({ ).toEqual({
fill: { type: 'static', inherited: false, value: 'purple' }, fill: { type: 'static', inherited: false, value: 'purple' },
}); });
}); });
@ -172,27 +169,23 @@ describe('computeStyle', () => {
const stylesheet = collectStylesheet(root); const stylesheet = collectStylesheet(root);
expect( expect(
computeStyle(stylesheet, querySelector(root, '#media-query')) computeStyle(stylesheet, querySelector(root, '#media-query'))
).to.deep.equal({ ).toEqual({
fill: { type: 'dynamic', inherited: false }, fill: { type: 'dynamic', inherited: false },
}); });
expect( expect(computeStyle(stylesheet, querySelector(root, '#hover'))).toEqual({
computeStyle(stylesheet, querySelector(root, '#hover'))
).to.deep.equal({
fill: { type: 'dynamic', inherited: false }, fill: { type: 'dynamic', inherited: false },
}); });
expect( expect(computeStyle(stylesheet, querySelector(root, '#inherited'))).toEqual(
computeStyle(stylesheet, querySelector(root, '#inherited')) {
).to.deep.equal({
fill: { type: 'dynamic', inherited: true }, fill: { type: 'dynamic', inherited: true },
}); }
);
expect( expect(
computeStyle(stylesheet, querySelector(root, '#inherited-overriden')) computeStyle(stylesheet, querySelector(root, '#inherited-overriden'))
).to.deep.equal({ ).toEqual({
fill: { type: 'static', inherited: false, value: 'blue' }, fill: { type: 'static', inherited: false, value: 'blue' },
}); });
expect( expect(computeStyle(stylesheet, querySelector(root, '#static'))).toEqual({
computeStyle(stylesheet, querySelector(root, '#static'))
).to.deep.equal({
fill: { type: 'static', inherited: false, value: 'black' }, fill: { type: 'static', inherited: false, value: 'black' },
}); });
}); });
@ -217,17 +210,15 @@ describe('computeStyle', () => {
const stylesheet = collectStylesheet(root); const stylesheet = collectStylesheet(root);
expect( expect(
computeStyle(stylesheet, querySelector(root, '#media-query')) computeStyle(stylesheet, querySelector(root, '#media-query'))
).to.deep.equal({ ).toEqual({
fill: { type: 'dynamic', inherited: false }, fill: { type: 'dynamic', inherited: false },
}); });
expect( expect(
computeStyle(stylesheet, querySelector(root, '#kinda-static')) computeStyle(stylesheet, querySelector(root, '#kinda-static'))
).to.deep.equal({ ).toEqual({
fill: { type: 'dynamic', inherited: false }, fill: { type: 'dynamic', inherited: false },
}); });
expect( expect(computeStyle(stylesheet, querySelector(root, '#static'))).toEqual({
computeStyle(stylesheet, querySelector(root, '#static'))
).to.deep.equal({
fill: { type: 'static', inherited: false, value: 'blue' }, fill: { type: 'static', inherited: false, value: 'blue' },
}); });
}); });
@ -252,17 +243,17 @@ describe('computeStyle', () => {
const stylesheet = collectStylesheet(root); const stylesheet = collectStylesheet(root);
expect( expect(
computeStyle(stylesheet, querySelector(root, '#valid-type')) computeStyle(stylesheet, querySelector(root, '#valid-type'))
).to.deep.equal({ ).toEqual({
fill: { type: 'static', inherited: false, value: 'red' }, fill: { type: 'static', inherited: false, value: 'red' },
}); });
expect( expect(
computeStyle(stylesheet, querySelector(root, '#empty-type')) computeStyle(stylesheet, querySelector(root, '#empty-type'))
).to.deep.equal({ ).toEqual({
fill: { type: 'static', inherited: false, value: 'green' }, fill: { type: 'static', inherited: false, value: 'green' },
}); });
expect( expect(
computeStyle(stylesheet, querySelector(root, '#invalid-type')) computeStyle(stylesheet, querySelector(root, '#invalid-type'))
).to.deep.equal({}); ).toEqual({});
}); });
it('ignores keyframes atrule', () => { it('ignores keyframes atrule', () => {
@ -288,9 +279,7 @@ describe('computeStyle', () => {
</svg> </svg>
`); `);
const stylesheet = collectStylesheet(root); const stylesheet = collectStylesheet(root);
expect( expect(computeStyle(stylesheet, querySelector(root, '#element'))).toEqual({
computeStyle(stylesheet, querySelector(root, '#element'))
).to.deep.equal({
animation: { animation: {
type: 'static', type: 'static',
inherited: false, inherited: false,

View File

@ -1,6 +1,5 @@
'use strict'; 'use strict';
const { expect } = require('chai');
const { visit, detachNodeFromParent } = require('./xast.js'); const { visit, detachNodeFromParent } = require('./xast.js');
const getAst = () => { const getAst = () => {
@ -57,7 +56,7 @@ describe('xast', () => {
}, },
}, },
}); });
expect(entered).to.deep.equal([ expect(entered).toEqual([
'root', 'root',
'element:g', 'element:g',
'element:rect', 'element:rect',
@ -81,7 +80,7 @@ describe('xast', () => {
}, },
}, },
}); });
expect(exited).to.deep.equal([ expect(exited).toEqual([
'element:rect', 'element:rect',
'element:circle', 'element:circle',
'element:g', 'element:g',
@ -103,6 +102,6 @@ describe('xast', () => {
}, },
}, },
}); });
expect(entered).to.deep.equal(['g', 'ellipse']); expect(entered).toEqual(['g', 'ellipse']);
}); });
}); });

3130
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -50,8 +50,8 @@
"!**/**.test.js" "!**/**.test.js"
], ],
"scripts": { "scripts": {
"test": "c8 --reporter=html --reporter=text mocha \"test/*/_index.js\" \"**/*.test.js\" --ignore=\"node_modules/**\"", "test": "jest --coverage",
"lint": "eslint --ignore-path .gitignore . && prettier --list-different \"**/*.js\" --ignore-path .gitignore", "lint": "eslint --ignore-path .gitignore . && prettier --check \"**/*.js\" --ignore-path .gitignore",
"fix": "eslint --ignore-path .gitignore --fix . && prettier --write \"**/*.js\" --ignore-path .gitignore", "fix": "eslint --ignore-path .gitignore --fix . && prettier --write \"**/*.js\" --ignore-path .gitignore",
"typecheck": "tsc", "typecheck": "tsc",
"test-browser": "rollup -c && node ./test/browser.js", "test-browser": "rollup -c && node ./test/browser.js",
@ -83,11 +83,10 @@
}, },
{ {
"files": [ "files": [
"test/**/*.js",
"**/*.test.js" "**/*.test.js"
], ],
"env": { "env": {
"mocha": true "jest": true
} }
} }
] ]
@ -105,12 +104,10 @@
"@rollup/plugin-commonjs": "^17.1.0", "@rollup/plugin-commonjs": "^17.1.0",
"@rollup/plugin-json": "^4.1.0", "@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^11.2.0", "@rollup/plugin-node-resolve": "^11.2.0",
"@types/mocha": "^8.2.2", "@types/jest": "^27.0.0",
"c8": "^7.6.0",
"chai": "^4.3.4",
"del": "^6.0.0", "del": "^6.0.0",
"eslint": "^7.22.0", "eslint": "^7.22.0",
"mocha": "^8.3.2", "jest": "^27.0.6",
"mock-stdin": "^1.0.0", "mock-stdin": "^1.0.0",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"pixelmatch": "^5.2.1", "pixelmatch": "^5.2.1",

View File

@ -3,7 +3,6 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const del = require('del'); const del = require('del');
const { expect } = require('chai');
const { Command } = require('commander'); const { Command } = require('commander');
const svgo = require('../../lib/svgo/coa.js'); const svgo = require('../../lib/svgo/coa.js');
@ -37,7 +36,7 @@ describe('coa', function () {
await fs.promises.mkdir(tempFolder); await fs.promises.mkdir(tempFolder);
}); });
after(async () => { afterAll(async () => {
await del(tempFolder); await del(tempFolder);
}); });
@ -105,8 +104,8 @@ describe('coa', function () {
'--quiet', '--quiet',
]); ]);
const optimizedWeight = calcFolderSvgWeight(svgFolderPath); const optimizedWeight = calcFolderSvgWeight(svgFolderPath);
expect(optimizedWeight).gt(0); expect(optimizedWeight).toBeGreaterThan(0);
expect(initWeight).lte(optimizedWeight); expect(initWeight).toBeLessThanOrEqual(optimizedWeight);
}); });
it('should optimize folder recursively', async () => { it('should optimize folder recursively', async () => {
@ -120,8 +119,8 @@ describe('coa', function () {
'--recursive', '--recursive',
]); ]);
const optimizedWeight = calcFolderSvgWeight(svgFolderPathRecursively); const optimizedWeight = calcFolderSvgWeight(svgFolderPathRecursively);
expect(optimizedWeight).gt(0); expect(optimizedWeight).toBeGreaterThan(0);
expect(initWeight).lte(optimizedWeight); expect(initWeight).toBeLessThanOrEqual(optimizedWeight);
}); });
it('should optimize file', async () => { it('should optimize file', async () => {
@ -130,7 +129,7 @@ describe('coa', function () {
).length; ).length;
await runProgram(['--input', svgPath, '--output', 'temp.svg', '--quiet']); await runProgram(['--input', svgPath, '--output', 'temp.svg', '--quiet']);
const optimizedFileLength = fs.readFileSync('temp.svg').length; const optimizedFileLength = fs.readFileSync('temp.svg').length;
expect(optimizedFileLength).lte(initialFileLength); expect(optimizedFileLength).toBeLessThanOrEqual(initialFileLength);
await del('temp.svg'); await del('temp.svg');
}); });
@ -144,8 +143,8 @@ describe('coa', function () {
'--quiet', '--quiet',
]); ]);
const optimizedWeight = calcFolderSvgWeight(tempFolder); const optimizedWeight = calcFolderSvgWeight(tempFolder);
expect(optimizedWeight).gt(0); expect(optimizedWeight).toBeGreaterThan(0);
expect(optimizedWeight).lte(initWeight); expect(optimizedWeight).toBeLessThanOrEqual(initWeight);
await del('temp.svg'); await del('temp.svg');
}); });
@ -167,7 +166,7 @@ describe('coa', function () {
]); ]);
} finally { } finally {
const optimizedFileLength = fs.readFileSync('temp.svg').length; const optimizedFileLength = fs.readFileSync('temp.svg').length;
expect(optimizedFileLength).lte(initialFile.length); expect(optimizedFileLength).toBeLessThanOrEqual(initialFile.length);
await del('temp.svg'); await del('temp.svg');
} }
}); });
@ -182,7 +181,7 @@ describe('coa', function () {
'--quiet', '--quiet',
]); ]);
let optimizedWeight = calcFolderSvgWeight(svgFolderPath); let optimizedWeight = calcFolderSvgWeight(svgFolderPath);
expect(optimizedWeight).lte(initWeight); expect(optimizedWeight).toBeLessThanOrEqual(initWeight);
}); });
it('should throw error when stated in input folder does not exist', async () => { it('should throw error when stated in input folder does not exist', async () => {
@ -196,7 +195,7 @@ describe('coa', function () {
]); ]);
} catch (error) { } catch (error) {
restoreConsoleError(); restoreConsoleError();
expect(error.message).to.match(/no such file or directory/); expect(error.message).toMatch(/no such file or directory/);
} }
}); });
@ -214,7 +213,7 @@ describe('coa', function () {
]); ]);
} finally { } finally {
restoreConsoleLog(); restoreConsoleLog();
expect(output).to.match(/www\.w3\.org\/2000\/svg/); expect(output).toMatch(/www\.w3\.org\/2000\/svg/);
} }
}); });
@ -226,7 +225,7 @@ describe('coa', function () {
try { try {
await runProgram(['--folder', emptyFolderPath, '--quiet']); await runProgram(['--folder', emptyFolderPath, '--quiet']);
} catch (error) { } catch (error) {
expect(error.message).to.match(/No SVG files/); expect(error.message).toMatch(/No SVG files/);
} }
}); });
@ -238,7 +237,7 @@ describe('coa', function () {
'--quiet', '--quiet',
]); ]);
} catch (error) { } catch (error) {
expect(error.message).to.match(/No SVG files have been found/); expect(error.message).toMatch(/No SVG files have been found/);
} }
}); });
@ -248,7 +247,7 @@ describe('coa', function () {
await runProgram(['--show-plugins']); await runProgram(['--show-plugins']);
} finally { } finally {
restoreConsoleLog(); restoreConsoleLog();
expect(output).to.match(/Currently available plugins:/); expect(output).toMatch(/Currently available plugins:/);
} }
}); });
}); });

View File

@ -1,7 +1,6 @@
'use strict'; 'use strict';
const path = require('path'); const path = require('path');
const { expect } = require('chai');
const { loadConfig } = require('../../lib/svgo-node.js'); const { loadConfig } = require('../../lib/svgo-node.js');
const { const {
resolvePluginConfig, resolvePluginConfig,
@ -20,42 +19,42 @@ describe('config', function () {
const removeRasterImages = getPlugin('removeRasterImages', plugins); const removeRasterImages = getPlugin('removeRasterImages', plugins);
it('removeDoctype plugin should be disabled', function () { it('removeDoctype plugin should be disabled', function () {
expect(removeDoctype.active).to.be.false; expect(removeDoctype.active).toEqual(false);
}); });
describe('enable plugin with params object', function () { describe('enable plugin with params object', function () {
it('removeRasterImages plugin should be enabled', function () { it('removeRasterImages plugin should be enabled', function () {
expect(removeRasterImages.active).to.be.true; expect(removeRasterImages.active).toEqual(true);
}); });
it('removeRasterImages plugin should have property "params"', function () { it('removeRasterImages plugin should have property "params"', function () {
expect(removeRasterImages).to.have.property('params'); expect(removeRasterImages).toHaveProperty('params');
}); });
it('"params" should be an instance of Object', function () { it('"params" should be an instance of Object', function () {
expect(removeRasterImages.params).to.be.an.instanceOf(Object); expect(removeRasterImages.params).toBeInstanceOf(Object);
}); });
it('"params" should have property "param" with value of true', function () { it('"params" should have property "param" with value of true', function () {
expect(removeRasterImages.params).to.have.property('param', true); expect(removeRasterImages.params).toHaveProperty('param', true);
}); });
}); });
describe('extend plugin params with object', function () { describe('extend plugin params with object', function () {
it('convertColors plugin should have property "params"', function () { it('convertColors plugin should have property "params"', function () {
expect(convertColors).to.have.property('params'); expect(convertColors).toHaveProperty('params');
}); });
it('"params" should be an instance of Object', function () { it('"params" should be an instance of Object', function () {
expect(convertColors.params).to.be.an.instanceOf(Object); expect(convertColors.params).toBeInstanceOf(Object);
}); });
it('"params" should have property "shorthex" with value of false', function () { it('"params" should have property "shorthex" with value of false', function () {
expect(convertColors.params).to.have.property('shorthex', false); expect(convertColors.params).toHaveProperty('shorthex', false);
}); });
it('"params" should have property "rgb2hex" with value of true', function () { it('"params" should have property "rgb2hex" with value of true', function () {
expect(convertColors.params).to.have.property('rgb2hex', true); expect(convertColors.params).toHaveProperty('rgb2hex', true);
}); });
}); });
}); });
@ -78,22 +77,22 @@ describe('config', function () {
const customPlugin = getPlugin('customPlugin', plugins); const customPlugin = getPlugin('customPlugin', plugins);
it('should have "multipass"', function () { it('should have "multipass"', function () {
expect(config.multipass).to.be.true; expect(config.multipass).toEqual(true);
}); });
it('config.plugins should have length 3', function () { it('config.plugins should have length 3', function () {
expect(plugins).to.have.length(3); expect(plugins).toHaveLength(3);
}); });
it('specified plugins should be enabled', function () { it('specified plugins should be enabled', function () {
expect(cleanupNumericValues.active).to.equal(true); expect(cleanupNumericValues.active).toEqual(true);
expect(convertPathData.active).to.equal(true); expect(convertPathData.active).toEqual(true);
}); });
it('plugins should inherit floatPrecision top level config', function () { it('plugins should inherit floatPrecision top level config', function () {
expect(cleanupNumericValues.params.floatPrecision).to.be.equal(2); expect(cleanupNumericValues.params.floatPrecision).toEqual(2);
expect(convertPathData.params.floatPrecision).to.be.equal(2); expect(convertPathData.params.floatPrecision).toEqual(2);
expect(customPlugin.params.floatPrecision).to.be.equal(2); expect(customPlugin.params.floatPrecision).toEqual(2);
}); });
}); });
@ -109,11 +108,11 @@ describe('config', function () {
const customPlugin = getPlugin('aCustomPlugin', plugins); const customPlugin = getPlugin('aCustomPlugin', plugins);
it('custom plugin should be enabled', function () { it('custom plugin should be enabled', function () {
expect(customPlugin.active).to.be.true; expect(customPlugin.active).toEqual(true);
}); });
it('custom plugin should have been given a name', function () { it('custom plugin should have been given a name', function () {
expect(customPlugin.name).to.equal('aCustomPlugin'); expect(customPlugin.name).toEqual('aCustomPlugin');
}); });
}); });
@ -128,15 +127,15 @@ describe('config', function () {
const customPlugin = getPlugin('aCustomPlugin', plugins); const customPlugin = getPlugin('aCustomPlugin', plugins);
it('config.plugins should have length 1', function () { it('config.plugins should have length 1', function () {
expect(plugins).to.have.length(1); expect(plugins).toHaveLength(1);
}); });
it('custom plugin should be enabled', function () { it('custom plugin should be enabled', function () {
expect(customPlugin.active).to.be.true; expect(customPlugin.active).toEqual(true);
}); });
it('custom plugin should have been given a name', function () { it('custom plugin should have been given a name', function () {
expect(customPlugin.name).to.equal('aCustomPlugin'); expect(customPlugin.name).toEqual('aCustomPlugin');
}); });
}); });
}); });
@ -163,8 +162,8 @@ describe('config', function () {
(item) => item.name === 'cleanupIDs' (item) => item.name === 'cleanupIDs'
); );
it('should preserve internal plugins order', () => { it('should preserve internal plugins order', () => {
expect(removeAttrsIndex).to.equal(41); expect(removeAttrsIndex).toEqual(41);
expect(cleanupIDsIndex).to.equal(11); expect(cleanupIDsIndex).toEqual(11);
}); });
it('should activate inactive by default plugins', () => { it('should activate inactive by default plugins', () => {
const removeAttrsPlugin = resolvePluginConfig( const removeAttrsPlugin = resolvePluginConfig(
@ -175,18 +174,18 @@ describe('config', function () {
extendedPlugins[cleanupIDsIndex], extendedPlugins[cleanupIDsIndex],
{} {}
); );
expect(removeAttrsPlugin.active).to.equal(true); expect(removeAttrsPlugin.active).toEqual(true);
expect(cleanupIDsPlugin.active).to.equal(true); expect(cleanupIDsPlugin.active).toEqual(true);
}); });
it('should leave not extended inactive plugins to be inactive', () => { it('should leave not extended inactive plugins to be inactive', () => {
const inactivePlugin = resolvePluginConfig( const inactivePlugin = resolvePluginConfig(
extendedPlugins.find((item) => item.name === 'addClassesToSVGElement'), extendedPlugins.find((item) => item.name === 'addClassesToSVGElement'),
{} {}
); );
expect(inactivePlugin.active).to.equal(false); expect(inactivePlugin.active).toEqual(false);
}); });
it('should put custom plugins in the end', () => { it('should put custom plugins in the end', () => {
expect(extendedPlugins[extendedPlugins.length - 1].name).to.equal( expect(extendedPlugins[extendedPlugins.length - 1].name).toEqual(
'customPlugin' 'customPlugin'
); );
}); });
@ -199,8 +198,8 @@ describe('config', function () {
extendedPlugins.find((item) => item.name === 'convertTransform'), extendedPlugins.find((item) => item.name === 'convertTransform'),
{} {}
); );
expect(convertPathDataPlugin.params.floatPrecision).to.equal(1); expect(convertPathDataPlugin.params.floatPrecision).toEqual(1);
expect(convertTransformPlugin.params.floatPrecision).to.equal(3); expect(convertTransformPlugin.params.floatPrecision).toEqual(3);
}); });
}); });
@ -209,35 +208,35 @@ describe('config', function () {
const config = await loadConfig( const config = await loadConfig(
path.join(process.cwd(), './test/config/fixtures/one/two/config.js') path.join(process.cwd(), './test/config/fixtures/one/two/config.js')
); );
expect(config).to.deep.equal({ plugins: [] }); expect(config).toEqual({ plugins: [] });
}); });
it('is loaded by relative path to cwd', async () => { it('is loaded by relative path to cwd', async () => {
const config = await loadConfig( const config = await loadConfig(
'one/two/config.js', 'one/two/config.js',
path.join(process.cwd(), './test/config/fixtures') path.join(process.cwd(), './test/config/fixtures')
); );
expect(config).to.deep.equal({ plugins: [] }); expect(config).toEqual({ plugins: [] });
}); });
it('is searched in cwd and up', async () => { it('is searched in cwd and up', async () => {
const config = await loadConfig( const config = await loadConfig(
null, null,
path.join(process.cwd(), './test/config/fixtures/one/two') path.join(process.cwd(), './test/config/fixtures/one/two')
); );
expect(config).to.deep.equal({ plugins: [] }); expect(config).toEqual({ plugins: [] });
}); });
it('gives null when config is not found', async () => { it('gives null when config is not found', async () => {
const config = await loadConfig( const config = await loadConfig(
null, null,
path.join(process.cwd(), './test/config') path.join(process.cwd(), './test/config')
); );
expect(config).to.equal(null); expect(config).toEqual(null);
}); });
it('is failed when specified config does not exist', async () => { it('is failed when specified config does not exist', async () => {
try { try {
await loadConfig('{}'); await loadConfig('{}');
expect.fail('Config is loaded successfully'); expect.fail('Config is loaded successfully');
} catch (error) { } catch (error) {
expect(error.message).to.match(/Cannot find module/); expect(error.message).toMatch(/Cannot find module/);
} }
}); });
it('is failed to load when module exports not an object', async () => { it('is failed to load when module exports not an object', async () => {
@ -247,7 +246,7 @@ describe('config', function () {
); );
expect.fail('Config is loaded successfully'); expect.fail('Config is loaded successfully');
} catch (error) { } catch (error) {
expect(error.message).to.match(/Invalid config file/); expect(error.message).toMatch(/Invalid config file/);
} }
try { try {
await loadConfig( await loadConfig(
@ -255,7 +254,7 @@ describe('config', function () {
); );
expect.fail('Config is loaded successfully'); expect.fail('Config is loaded successfully');
} catch (error) { } catch (error) {
expect(error.message).to.match(/Invalid config file/); expect(error.message).toMatch(/Invalid config file/);
} }
try { try {
await loadConfig( await loadConfig(
@ -263,7 +262,7 @@ describe('config', function () {
); );
expect.fail('Config is loaded successfully'); expect.fail('Config is loaded successfully');
} catch (error) { } catch (error) {
expect(error.message).to.match(/Invalid config file/); expect(error.message).toMatch(/Invalid config file/);
} }
}); });
it('handles config errors properly', async () => { it('handles config errors properly', async () => {
@ -274,7 +273,7 @@ describe('config', function () {
); );
expect.fail('Config is loaded successfully'); expect.fail('Config is loaded successfully');
} catch (error) { } catch (error) {
expect(error.message).to.match(/plugins is not defined/); expect(error.message).toMatch(/plugins is not defined/);
} }
}); });
it('handles MODULE_NOT_FOUND properly', async () => { it('handles MODULE_NOT_FOUND properly', async () => {
@ -284,7 +283,7 @@ describe('config', function () {
); );
expect.fail('Config is loaded successfully'); expect.fail('Config is loaded successfully');
} catch (error) { } catch (error) {
expect(error.message).to.match(/Cannot find module 'unknown-module'/); expect(error.message).toMatch(/Cannot find module 'unknown-module'/);
} }
}); });
}); });

View File

@ -1,25 +1,24 @@
'use strict'; 'use strict';
const { expect } = require('chai');
const { createContentItem } = require('../../lib/svgo.js'); const { createContentItem } = require('../../lib/svgo.js');
const JSAPI = require('../../lib/svgo/jsAPI.js'); const JSAPI = require('../../lib/svgo/jsAPI.js');
describe('svgo api', function () { describe('svgo api', function () {
it('should has createContentItem method', function () { it('should has createContentItem method', function () {
expect(createContentItem).to.be.instanceOf(Function); expect(createContentItem).toBeInstanceOf(Function);
}); });
it('should be able to create content item', function () { it('should be able to create content item', function () {
var item = createContentItem({ var item = createContentItem({
elem: 'elementName', elem: 'elementName',
}); });
expect(item).to.be.instanceOf(JSAPI); expect(item).toBeInstanceOf(JSAPI);
expect(item).to.have.ownProperty('elem').equal('elementName'); expect(item.elem).toEqual('elementName');
}); });
it('should be able create content item without argument', function () { it('should be able create content item without argument', function () {
var item = createContentItem(); var item = createContentItem();
expect(item).to.be.instanceOf(JSAPI); expect(item).toBeInstanceOf(JSAPI);
expect(item).to.be.empty; expect(item).toEqual({});
}); });
}); });

View File

@ -1,6 +1,5 @@
'use strict'; 'use strict';
const { expect } = require('chai');
const FS = require('fs'); const FS = require('fs');
const PATH = require('path'); const PATH = require('path');
const EOL = require('os').EOL; const EOL = require('os').EOL;
@ -36,11 +35,9 @@ describe('plugins tests', function () {
plugins: [plugin], plugins: [plugin],
js2svg: { pretty: true }, js2svg: { pretty: true },
}); });
if (result.error != null) { expect(result.error).not.toEqual(expect.anything());
expect.fail(result.error);
}
//FIXME: results.data has a '\n' at the end while it should not //FIXME: results.data has a '\n' at the end while it should not
expect(normalize(result.data)).to.equal(should); expect(normalize(result.data)).toEqual(should);
}); });
}); });
} }

View File

@ -1,20 +1,18 @@
'use strict'; 'use strict';
const { expect } = require('chai'); const FS = require('fs');
const PATH = require('path');
var FS = require('fs'), const JSAPI = require('../../lib/svgo/jsAPI');
PATH = require('path'), const CSSClassList = require('../../lib/svgo/css-class-list');
JSAPI = require('../../lib/svgo/jsAPI'), const CSSStyleDeclaration = require('../../lib/svgo/css-style-declaration');
CSSClassList = require('../../lib/svgo/css-class-list'), const SVG2JS = require('../../lib/svgo/svg2js');
CSSStyleDeclaration = require('../../lib/svgo/css-style-declaration'),
SVG2JS = require('../../lib/svgo/svg2js');
describe('svg2js', function () { describe('svg2js', function () {
describe('working svg', function () { describe('working svg', function () {
var filepath = PATH.resolve(__dirname, './test.svg'), var filepath = PATH.resolve(__dirname, './test.svg'),
root; root;
before(function (done) { beforeAll(function (done) {
FS.readFile(filepath, 'utf8', function (err, data) { FS.readFile(filepath, 'utf8', function (err, data) {
if (err) { if (err) {
throw err; throw err;
@ -27,30 +25,30 @@ describe('svg2js', function () {
describe('root', function () { describe('root', function () {
it('should exist', function () { it('should exist', function () {
expect(root).to.exist; expect(root).toEqual(expect.anything());
}); });
it('should be an instance of Object', function () { it('should be an instance of Object', function () {
expect(root).to.be.an.instanceOf(Object); expect(root).toBeInstanceOf(Object);
}); });
it('should have property "children"', function () { it('should have property "children"', function () {
expect(root).to.have.property('children'); expect(root).toHaveProperty('children');
}); });
}); });
describe('root.children', function () { describe('root.children', function () {
it('should be an instance of Array', function () { it('should be an instance of Array', function () {
expect(root.children).to.be.an.instanceOf(Array); expect(root.children).toBeInstanceOf(Array);
}); });
it('should have length 4', function () { it('should have length 4', function () {
expect(root.children).to.have.lengthOf(4); expect(root.children).toHaveLength(4);
}); });
}); });
it('the first node should be instruction', () => { it('the first node should be instruction', () => {
expect(root.children[0]).to.include({ expect(root.children[0]).toEqual({
type: 'instruction', type: 'instruction',
name: 'xml', name: 'xml',
value: 'version="1.0" encoding="utf-8"', value: 'version="1.0" encoding="utf-8"',
@ -58,7 +56,7 @@ describe('svg2js', function () {
}); });
it('the second node should be comment', () => { it('the second node should be comment', () => {
expect(root.children[1]).to.include({ expect(root.children[1]).toEqual({
type: 'comment', type: 'comment',
value: value:
'Generator: Adobe Illustrator 15.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)', 'Generator: Adobe Illustrator 15.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)',
@ -66,7 +64,7 @@ describe('svg2js', function () {
}); });
it('the third node should be doctype', () => { it('the third node should be doctype', () => {
expect(root.children[2]).to.deep.include({ expect(root.children[2]).toEqual({
type: 'doctype', type: 'doctype',
name: 'svg', name: 'svg',
data: { data: {
@ -78,87 +76,86 @@ describe('svg2js', function () {
describe('name', function () { describe('name', function () {
it('should have property name: "svg"', function () { it('should have property name: "svg"', function () {
expect(root.children[3]).to.include({ expect(root.children[3]).toEqual(
expect.objectContaining({
name: 'svg', name: 'svg',
}); })
);
}); });
}); });
describe('attributes', function () { describe('attributes', function () {
describe('root.children[3].attrs', function () { describe('root.children[3].attrs', function () {
it('should exist', function () { it('should exist', function () {
expect(root.children[3].attrs).to.exist; expect(root.children[3].attrs).toEqual(expect.anything());
}); });
it('should be an instance of Object', function () { it('should be an instance of Object', function () {
expect(root.children[3].attrs).to.be.an.instanceOf(Object); expect(root.children[3].attrs).toBeInstanceOf(Object);
}); });
}); });
describe('root.children[3].attrs.version', function () { describe('root.children[3].attrs.version', function () {
it('should exist', function () { it('should exist', function () {
expect(root.children[3].attrs.version).to.exist; expect(root.children[3].attrs.version).toEqual(expect.anything());
}); });
it('should be an instance of Object', function () { it('should be an instance of Object', function () {
expect(root.children[3].attrs.version).to.be.an.instanceOf(Object); expect(root.children[3].attrs.version).toBeInstanceOf(Object);
}); });
it('should have property name: "version"', function () { it('should have property name: "version"', function () {
expect(root.children[3].attrs.version).to.have.property( expect(root.children[3].attrs.version).toHaveProperty(
'name', 'name',
'version' 'version'
); );
}); });
it('should have property value: "1.1"', function () { it('should have property value: "1.1"', function () {
expect(root.children[3].attrs.version).to.have.property( expect(root.children[3].attrs.version).toHaveProperty('value', '1.1');
'value',
'1.1'
);
}); });
}); });
}); });
describe('children', function () { describe('children', function () {
it('should exist', function () { it('should exist', function () {
expect(root.children[3].children).to.exist; expect(root.children[3].children).toEqual(expect.anything());
}); });
it('should be an instance of Array', function () { it('should be an instance of Array', function () {
expect(root.children[3].children).to.be.an.instanceOf(Array); expect(root.children[3].children).toBeInstanceOf(Array);
}); });
it('should eventually have length 3', function () { it('should eventually have length 3', function () {
expect(root.children[3].children).to.have.lengthOf(3); expect(root.children[3].children).toHaveLength(3);
}); });
}); });
describe('text nodes', function () { describe('text nodes', function () {
it('should contain preserved whitespace', function () { it('should contain preserved whitespace', function () {
const textNode = root.children[3].children[1].children[0].children[1]; const textNode = root.children[3].children[1].children[0].children[1];
return expect(textNode.children[0].value).to.equal(' test '); expect(textNode.children[0].value).toEqual(' test ');
}); });
}); });
describe('API', function () { describe('API', function () {
describe('clone()', function () { describe('clone()', function () {
it('svg should have property "clone"', function () { it('svg should have property "clone"', function () {
expect(root.children[3]).to.have.property('clone'); expect(root.children[3]).toHaveProperty('clone');
}); });
it('svg.clone() should be an instance of JSAPI', function () { it('svg.clone() should be an instance of JSAPI', function () {
expect(root.children[3].clone()).to.be.instanceOf(JSAPI); expect(root.children[3].clone()).toBeInstanceOf(JSAPI);
}); });
it('root.children[3].children[0].clone() has a valid style property', function () { it('root.children[3].children[0].clone() has a valid style property', function () {
expect(root.children[3].children[0].clone().style).to.be.instanceof( expect(root.children[3].children[0].clone().style).toBeInstanceOf(
CSSStyleDeclaration CSSStyleDeclaration
); );
}); });
it('root.children[3].children[2].clone() has a valid class property', function () { it('root.children[3].children[2].clone() has a valid class property', function () {
expect(root.children[3].children[2].clone().class).to.be.instanceof( expect(root.children[3].children[2].clone().class).toBeInstanceOf(
CSSClassList CSSClassList
); );
}); });
@ -166,129 +163,135 @@ describe('svg2js', function () {
describe('isElem()', function () { describe('isElem()', function () {
it('svg should have property "isElem"', function () { it('svg should have property "isElem"', function () {
expect(root.children[3]).to.have.property('isElem'); expect(root.children[3]).toHaveProperty('isElem');
}); });
it('svg.isElem() should be true', function () { it('svg.isElem() should be true', function () {
expect(root.children[3].isElem()).to.be.true; expect(root.children[3].isElem()).toEqual(true);
}); });
it('svg.isElem("svg") should be true', function () { it('svg.isElem("svg") should be true', function () {
expect(root.children[3].isElem('svg')).to.be.true; expect(root.children[3].isElem('svg')).toEqual(true);
}); });
it('svg.isElem("trololo") should be false', function () { it('svg.isElem("trololo") should be false', function () {
expect(root.children[3].isElem('trololo')).to.be.false; expect(root.children[3].isElem('trololo')).toEqual(false);
}); });
it('svg.isElem(["svg", "trololo"]) should be true', function () { it('svg.isElem(["svg", "trololo"]) should be true', function () {
expect(root.children[3].isElem(['svg', 'trololo'])).to.be.true; expect(root.children[3].isElem(['svg', 'trololo'])).toEqual(true);
}); });
}); });
describe('isEmpty()', function () { describe('isEmpty()', function () {
it('svg should have property "isEmpty"', function () { it('svg should have property "isEmpty"', function () {
expect(root.children[3]).to.have.property('isEmpty'); expect(root.children[3]).toHaveProperty('isEmpty');
}); });
it('svg.isEmpty() should be false', function () { it('svg.isEmpty() should be false', function () {
expect(root.children[3].isEmpty()).to.be.false; expect(root.children[3].isEmpty()).toEqual(false);
}); });
it('svg.children[0].children[0].isEmpty() should be true', function () { it('svg.children[0].children[0].isEmpty() should be true', function () {
expect(root.children[3].children[0].children[0].isEmpty()).to.be.true; expect(root.children[3].children[0].children[0].isEmpty()).toEqual(
true
);
}); });
}); });
describe('hasAttr()', function () { describe('hasAttr()', function () {
it('svg should have property "hasAttr"', function () { it('svg should have property "hasAttr"', function () {
expect(root.children[3]).to.have.property('hasAttr'); expect(root.children[3]).toHaveProperty('hasAttr');
}); });
it('svg.hasAttr() should be true', function () { it('svg.hasAttr() should be true', function () {
expect(root.children[3].hasAttr()).to.be.true; expect(root.children[3].hasAttr()).toEqual(true);
}); });
it('svg.hasAttr("xmlns") should be true', function () { it('svg.hasAttr("xmlns") should be true', function () {
expect(root.children[3].hasAttr('xmlns')).to.be.true; expect(root.children[3].hasAttr('xmlns')).toEqual(true);
}); });
it('svg.hasAttr("xmlns", "http://www.w3.org/2000/svg") should be true', function () { it('svg.hasAttr("xmlns", "http://www.w3.org/2000/svg") should be true', function () {
expect( expect(
root.children[3].hasAttr('xmlns', 'http://www.w3.org/2000/svg') root.children[3].hasAttr('xmlns', 'http://www.w3.org/2000/svg')
).to.be.true; ).toEqual(true);
}); });
it('svg.hasAttr("xmlns", "trololo") should be false', function () { it('svg.hasAttr("xmlns", "trololo") should be false', function () {
expect(root.children[3].hasAttr('xmlns', 'trololo')).to.be.false; expect(root.children[3].hasAttr('xmlns', 'trololo')).toEqual(false);
}); });
it('svg.hasAttr("trololo") should be false', function () { it('svg.hasAttr("trololo") should be false', function () {
expect(root.children[3].hasAttr('trololo')).to.be.false; expect(root.children[3].hasAttr('trololo')).toEqual(false);
}); });
it('svg.children[1].hasAttr() should be false', function () { it('svg.children[1].hasAttr() should be false', function () {
expect(root.children[3].children[1].hasAttr()).to.be.false; expect(root.children[3].children[1].hasAttr()).toEqual(false);
}); });
}); });
describe('attr()', function () { describe('attr()', function () {
it('svg should have property "attr"', function () { it('svg should have property "attr"', function () {
expect(root.children[3]).to.have.property('attr'); expect(root.children[3]).toHaveProperty('attr');
}); });
it('svg.attr("xmlns") should be an instance of Object', function () { it('svg.attr("xmlns") should be an instance of Object', function () {
expect(root.children[3].attr('xmlns')).to.be.an.instanceOf(Object); expect(root.children[3].attr('xmlns')).toBeInstanceOf(Object);
}); });
it('svg.attr("xmlns", "http://www.w3.org/2000/svg") should be an instance of Object', function () { it('svg.attr("xmlns", "http://www.w3.org/2000/svg") should be an instance of Object', function () {
expect( expect(
root.children[3].attr('xmlns', 'http://www.w3.org/2000/svg') root.children[3].attr('xmlns', 'http://www.w3.org/2000/svg')
).to.be.an.instanceOf(Object); ).toBeInstanceOf(Object);
}); });
it('svg.attr("xmlns", "trololo") should be an undefined', function () { it('svg.attr("xmlns", "trololo") should be an undefined', function () {
expect(root.children[3].attr('xmlns', 'trololo')).to.not.exist; expect(root.children[3].attr('xmlns', 'trololo')).not.toEqual(
expect.anything()
);
}); });
it('svg.attr("trololo") should be an undefined', function () { it('svg.attr("trololo") should be an undefined', function () {
expect(root.children[3].attr('trololo')).to.not.exist; expect(root.children[3].attr('trololo')).not.toEqual(
expect.anything()
);
}); });
it('svg.attr() should be undefined', function () { it('svg.attr() should be undefined', function () {
expect(root.children[3].attr()).to.not.exist; expect(root.children[3].attr()).not.toEqual(expect.anything());
}); });
}); });
describe('removeAttr()', function () { describe('removeAttr()', function () {
it('svg should have property "removeAttr"', function () { it('svg should have property "removeAttr"', function () {
expect(root.children[3]).to.have.property('removeAttr'); expect(root.children[3]).toHaveProperty('removeAttr');
}); });
it('svg.removeAttr("width") should be true', function () { it('svg.removeAttr("width") should be true', function () {
expect(root.children[3].removeAttr('width')).to.be.true; expect(root.children[3].removeAttr('width')).toEqual(true);
expect(root.children[3].hasAttr('width')).to.be.false; expect(root.children[3].hasAttr('width')).toEqual(false);
}); });
it('svg.removeAttr("height", "120px") should be true', function () { it('svg.removeAttr("height", "120px") should be true', function () {
expect(root.children[3].removeAttr('height', '120px')).to.be.true; expect(root.children[3].removeAttr('height', '120px')).toEqual(true);
expect(root.children[3].hasAttr('height')).to.be.false; expect(root.children[3].hasAttr('height')).toEqual(false);
}); });
it('svg.removeAttr("x", "1px") should be false', function () { it('svg.removeAttr("x", "1px") should be false', function () {
expect(root.children[3].removeAttr('x', '1px')).to.be.false; expect(root.children[3].removeAttr('x', '1px')).toEqual(false);
expect(root.children[3].hasAttr('x')).to.be.true; expect(root.children[3].hasAttr('x')).toEqual(true);
}); });
it('svg.removeAttr("z") should be false', function () { it('svg.removeAttr("z") should be false', function () {
expect(root.children[3].removeAttr('z')).to.be.false; expect(root.children[3].removeAttr('z')).toEqual(false);
}); });
it('svg.removeAttr() should be false', function () { it('svg.removeAttr() should be false', function () {
expect(root.children[3].removeAttr()).to.be.false; expect(root.children[3].removeAttr()).toEqual(false);
}); });
}); });
@ -299,27 +302,27 @@ describe('svg2js', function () {
}; };
it('svg should have property "addAttr"', function () { it('svg should have property "addAttr"', function () {
expect(root.children[3]).to.have.property('addAttr'); expect(root.children[3]).toHaveProperty('addAttr');
}); });
it('svg.addAttr(attr) should be an instance of Object', function () { it('svg.addAttr(attr) should be an instance of Object', function () {
expect(root.children[3].addAttr(attr)).to.be.an.instanceOf(Object); expect(root.children[3].addAttr(attr)).toBeInstanceOf(Object);
}); });
it('svg.children[1].children[0].addAttr(attr) should be an instance of Object', function () { it('svg.children[1].children[0].addAttr(attr) should be an instance of Object', function () {
expect( expect(
root.children[3].children[1].children[0].addAttr(attr) root.children[3].children[1].children[0].addAttr(attr)
).to.be.an.instanceOf(Object); ).toBeInstanceOf(Object);
}); });
it('svg.addAttr() should be false', function () { it('svg.addAttr() should be false', function () {
expect(root.children[3].addAttr()).to.be.false; expect(root.children[3].addAttr()).toEqual(false);
}); });
}); });
describe('eachAttr()', function () { describe('eachAttr()', function () {
it('svg should have property "eachAttr"', function () { it('svg should have property "eachAttr"', function () {
expect(root.children[3]).to.have.property('eachAttr'); expect(root.children[3]).toHaveProperty('eachAttr');
}); });
it('svg.children[0].eachAttr(function() {}) should be true', function () { it('svg.children[0].eachAttr(function() {}) should be true', function () {
@ -327,13 +330,13 @@ describe('svg2js', function () {
root.children[3].children[0].eachAttr(function (attr) { root.children[3].children[0].eachAttr(function (attr) {
attr.value = '1'; attr.value = '1';
}) })
).to.be.true; ).toEqual(true);
expect(root.children[3].children[0].attr('type').value).to.equal('1'); expect(root.children[3].children[0].attr('type').value).toEqual('1');
}); });
it('svg.children[1].eachAttr(function() {}) should be false', function () { it('svg.children[1].eachAttr(function() {}) should be false', function () {
expect(root.children[3].children[1].eachAttr()).to.be.false; expect(root.children[3].children[1].eachAttr()).toEqual(false);
}); });
}); });
}); });
@ -344,7 +347,7 @@ describe('svg2js', function () {
root, root,
error; error;
before(function (done) { beforeAll(function (done) {
FS.readFile(filepath, 'utf8', function (err, data) { FS.readFile(filepath, 'utf8', function (err, data) {
if (err) { if (err) {
throw err; throw err;
@ -362,13 +365,13 @@ describe('svg2js', function () {
describe('root', function () { describe('root', function () {
it('should have property "error"', function () { it('should have property "error"', function () {
expect(root).to.have.property('error'); expect(root).toHaveProperty('error');
}); });
}); });
describe('root.error', function () { describe('root.error', function () {
it('should be "Error in parsing SVG: Unexpected close tag"', function () { it('should be "Error in parsing SVG: Unexpected close tag"', function () {
expect(root.error).to.equal( expect(root.error).toEqual(
'Error in parsing SVG: Unexpected close tag\nLine: 10\nColumn: 15\nChar: >' 'Error in parsing SVG: Unexpected close tag\nLine: 10\nColumn: 15\nChar: >'
); );
}); });
@ -376,7 +379,7 @@ describe('svg2js', function () {
describe('error', function () { describe('error', function () {
it('should not be thrown', function () { it('should not be thrown', function () {
expect(error).to.not.exist; expect(error).not.toEqual(expect.anything());
}); });
}); });
}); });

View File

@ -1,6 +1,5 @@
'use strict'; 'use strict';
const { expect } = require('chai');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const { EOL } = require('os'); const { EOL } = require('os');
@ -25,14 +24,14 @@ describe('svgo', () => {
plugins: [], plugins: [],
js2svg: { pretty: true, indent: 2 }, js2svg: { pretty: true, indent: 2 },
}); });
expect(normalize(result.data)).to.equal(expected); expect(normalize(result.data)).toEqual(expected);
}); });
it('should run multiple times', async () => { it('should run multiple times', async () => {
const [original, expected] = await parseFixture('multipass.svg'); const [original, expected] = await parseFixture('multipass.svg');
const result = optimize(original, { const result = optimize(original, {
multipass: true, multipass: true,
}); });
expect(normalize(result.data)).to.equal(expected); expect(normalize(result.data)).toEqual(expected);
}); });
it('should pass multipass count to plugins', async () => { it('should pass multipass count to plugins', async () => {
const [original, expected] = await parseFixture('multipass-prefix-ids.svg'); const [original, expected] = await parseFixture('multipass-prefix-ids.svg');
@ -44,24 +43,24 @@ describe('svgo', () => {
}, },
]), ]),
}); });
expect(normalize(result.data)).to.equal(expected); expect(normalize(result.data)).toEqual(expected);
}); });
it('should handle plugins order properly', async () => { it('should handle plugins order properly', async () => {
const [original, expected] = await parseFixture('plugins-order.svg'); const [original, expected] = await parseFixture('plugins-order.svg');
const result = optimize(original, { input: 'file', path: 'input.svg' }); const result = optimize(original, { input: 'file', path: 'input.svg' });
expect(normalize(result.data)).to.equal(expected); expect(normalize(result.data)).toEqual(expected);
}); });
it('should handle parse error', async () => { it('should handle parse error', async () => {
const fixture = await fs.promises.readFile( const fixture = await fs.promises.readFile(
path.resolve(__dirname, 'invalid.svg') path.resolve(__dirname, 'invalid.svg')
); );
const result = optimize(fixture, { input: 'file', path: 'input.svg' }); const result = optimize(fixture, { input: 'file', path: 'input.svg' });
expect(result.error).to.match(/Error in parsing SVG/); expect(result.error).toMatch(/Error in parsing SVG/);
expect(result.path).to.equal('input.svg'); expect(result.path).toEqual('input.svg');
}); });
it('should handle empty svg tag', async () => { it('should handle empty svg tag', async () => {
const result = optimize('<svg />', { input: 'file', path: 'input.svg' }); const result = optimize('<svg />', { input: 'file', path: 'input.svg' });
expect(result.data).to.equal('<svg/>'); expect(result.data).toEqual('<svg/>');
}); });
it('should preserve style specifity over attributes', async () => { it('should preserve style specifity over attributes', async () => {
const [original, expected] = await parseFixture('style-specifity.svg'); const [original, expected] = await parseFixture('style-specifity.svg');
@ -70,7 +69,7 @@ describe('svgo', () => {
path: 'input.svg', path: 'input.svg',
js2svg: { pretty: true }, js2svg: { pretty: true },
}); });
expect(normalize(result.data)).to.equal(expected); expect(normalize(result.data)).toEqual(expected);
}); });
it('should inline entities', async () => { it('should inline entities', async () => {
const [original, expected] = await parseFixture('entities.svg'); const [original, expected] = await parseFixture('entities.svg');
@ -79,7 +78,7 @@ describe('svgo', () => {
plugins: [], plugins: [],
js2svg: { pretty: true }, js2svg: { pretty: true },
}); });
expect(normalize(result.data)).to.equal(expected); expect(normalize(result.data)).toEqual(expected);
}); });
it('should preserve whitespaces between tspan tags', async () => { it('should preserve whitespaces between tspan tags', async () => {
const [original, expected] = await parseFixture('whitespaces.svg'); const [original, expected] = await parseFixture('whitespaces.svg');
@ -87,6 +86,6 @@ describe('svgo', () => {
path: 'input.svg', path: 'input.svg',
js2svg: { pretty: true }, js2svg: { pretty: true },
}); });
expect(normalize(result.data)).to.equal(expected); expect(normalize(result.data)).toEqual(expected);
}); });
}); });

View File

@ -64,18 +64,18 @@
"plugins/sortDefsChildren.js", "plugins/sortDefsChildren.js",
"lib/svgo/jsAPI.js", "lib/svgo/jsAPI.js",
"test/browser.js", "test/browser.js",
"test/coa/_index.js", "test/coa/_index.test.js",
"test/config/_index.js", "test/config/_index.test.js",
"test/config/fixtures/invalid-array.js", "test/config/fixtures/invalid-array.js",
"test/config/fixtures/invalid-null.js", "test/config/fixtures/invalid-null.js",
"test/config/fixtures/invalid-string.js", "test/config/fixtures/invalid-string.js",
"test/config/fixtures/invalid/svgo.config.js", "test/config/fixtures/invalid/svgo.config.js",
"test/config/fixtures/module-not-found.js", "test/config/fixtures/module-not-found.js",
"test/config/fixtures/one/two/config.js", "test/config/fixtures/one/two/config.js",
"test/jsapi/_index.js", "test/jsapi/_index.test.js",
"test/plugins/_index.js", "test/plugins/_index.test.js",
"test/regression.js", "test/regression.js",
"test/svg2js/_index.js", "test/svg2js/_index.test.js",
"test/svgo/_index.js" "test/svgo/_index.test.js"
] ]
} }