1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-29 20:21:14 +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';
const { expect } = require('chai');
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')).to.deep.equal([
expect(parsePathData('M0 10 L \n\r\t20 30')).toEqual([
{ 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')).to.deep.equal([
expect(parsePathData('M0 , 10 L 20 \n\r\t30,40,50')).toEqual([
{ 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')).to.deep.equal([]);
expect(parsePathData(', M0 10')).toEqual([]);
});
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] },
]);
});
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] },
]);
});
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', () => {
expect(parsePathData('M0 10 , L 20 #40')).to.deep.equal([
expect(parsePathData('M0 10 , L 20 #40')).toEqual([
{ command: 'M', args: [0, 10] },
]);
});
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] },
]);
});
it('should stop if moveto not the first command', () => {
expect(parsePathData('L 10 20')).to.deep.equal([]);
expect(parsePathData('10 20')).to.deep.equal([]);
expect(parsePathData('L 10 20')).toEqual([]);
expect(parsePathData('10 20')).toEqual([]);
});
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] },
]);
});
it('should stop on invalid numbers', () => {
expect(parsePathData('M ...')).to.deep.equal([]);
expect(parsePathData('M ...')).toEqual([]);
});
it('should handle arcs', () => {
expect(
@ -68,7 +67,7 @@ describe('parse path data', () => {
l 50,-25
`
)
).to.deep.equal([
).toEqual([
{ command: 'M', args: [600, 350] },
{ command: 'l', args: [50, -25] },
{ command: 'a', args: [25, 25, -30, 0, 1, 50, -25] },
@ -93,7 +92,7 @@ describe('stringify path data', () => {
{ command: 'H', args: [50] },
],
})
).to.equal('M0 0h10 20 30H40 50');
).toEqual('M0 0h10 20 30H40 50');
});
it('should not combine sequence of moveto', () => {
expect(
@ -105,7 +104,7 @@ describe('stringify path data', () => {
{ 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', () => {
expect(
@ -119,7 +118,7 @@ describe('stringify path data', () => {
{ 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(
stringifyPathData({
pathData: [
@ -127,7 +126,7 @@ describe('stringify path data', () => {
{ 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', () => {
expect(
@ -139,7 +138,7 @@ describe('stringify path data', () => {
{ 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', () => {
const pathData = [
@ -153,13 +152,13 @@ describe('stringify path data', () => {
pathData,
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(
stringifyPathData({
pathData,
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', () => {
const pathData = [
@ -172,12 +171,12 @@ describe('stringify path data', () => {
pathData,
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(
stringifyPathData({
pathData,
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');
});
});