Skip to content

Commit

Permalink
test(scripts): add svgo plugin test
Browse files Browse the repository at this point in the history
  • Loading branch information
SevereCloud committed Jun 23, 2022
1 parent 8cfaa27 commit 961fba3
Show file tree
Hide file tree
Showing 5 changed files with 2,098 additions and 8 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,19 @@ jobs:
# only affects current branch
skip_step: install
build_script: "icons-build"

test_scripts:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./packages/icons-scripts

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 14
cache: "yarn"
- run: yarn install --frozen-lockfile --ignore-scripts
- name: Run tests
run: yarn test
7 changes: 6 additions & 1 deletion packages/icons-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
"yarn": "^1.21.1",
"node": ">12.0.0"
},
"devDependencies": {},
"scripts": {
"test": "jest"
},
"devDependencies": {
"jest": "^28.1.1"
},
"dependencies": {
"@swc/cli": "^0.1.57",
"@swc/core": "^1.2.204",
Expand Down
60 changes: 60 additions & 0 deletions packages/icons-scripts/scripts/plugins/tests/_index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"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("svgo");

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 = {
params: params ? JSON.parse(params) : {},
...require("../" + name),
};

const result = optimize(original, {
path: file,
plugins: [plugin],
js2svg: { pretty: true },
});

expect(result.error).not.toEqual(expect.anything());
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);
});
});
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 961fba3

Please sign in to comment.