mirror of
https://github.com/svg/svgo.git
synced 2025-08-06 04:22:39 +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:
58
test/plugins/_index.test.js
Normal file
58
test/plugins/_index.test.js
Normal file
@@ -0,0 +1,58 @@
|
||||
'use strict';
|
||||
|
||||
const FS = require('fs');
|
||||
const PATH = require('path');
|
||||
const EOL = require('os').EOL;
|
||||
const regEOL = new RegExp(EOL, 'g');
|
||||
const regFilename = /^(.*)\.(\d+)\.svg$/;
|
||||
const { optimize } = require('../../lib/svgo.js');
|
||||
|
||||
describe('plugins tests', function () {
|
||||
FS.readdirSync(__dirname).forEach(function (file) {
|
||||
var match = file.match(regFilename),
|
||||
index,
|
||||
name;
|
||||
|
||||
if (match) {
|
||||
name = match[1];
|
||||
index = match[2];
|
||||
|
||||
file = PATH.resolve(__dirname, file);
|
||||
|
||||
it(name + '.' + index, function () {
|
||||
return readFile(file).then(function (data) {
|
||||
// remove description
|
||||
const items = normalize(data).split(/\s*===\s*/);
|
||||
const test = items.length === 2 ? items[1] : items[0];
|
||||
// extract test case
|
||||
const [original, should, params] = test.split(/\s*@@@\s*/);
|
||||
const plugin = {
|
||||
name,
|
||||
params: params ? JSON.parse(params) : {},
|
||||
};
|
||||
const result = optimize(original, {
|
||||
path: file,
|
||||
plugins: [plugin],
|
||||
js2svg: { pretty: true },
|
||||
});
|
||||
expect(result.error).not.toEqual(expect.anything());
|
||||
//FIXME: results.data has a '\n' at the end while it should not
|
||||
expect(normalize(result.data)).toEqual(should);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function normalize(file) {
|
||||
return file.trim().replace(regEOL, '\n');
|
||||
}
|
||||
|
||||
function readFile(file) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
FS.readFile(file, 'utf8', function (err, data) {
|
||||
if (err) return reject(err);
|
||||
resolve(data);
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user