-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.test.js
42 lines (36 loc) · 9.32 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const assert = require('node:assert/strict');
const fs = require('node:fs/promises');
const path = require('node:path');
const { test } = require('node:test');
const webpack = require('webpack');
const basic = require('./example/basic/webpack.config.js');
const externalConfig = require('./example/external-config/webpack.config.js');
const svgoError = require('./example/svgo-error/webpack.config.js');
function buildWebpack(config) {
const compiler = webpack(config);
return new Promise((resolve, reject) => {
compiler.run((error, result) => {
if(error) {
reject(error);
} else {
resolve(result);
}
});
});
}
test('basic', async () => {
const result = await buildWebpack(basic);
assert.equal(result.hasErrors(), false);
const output = await fs.readFile(path.join(__dirname, 'example', 'basic', 'dist', 'SVG_logo.svg'), 'utf8');
assert.equal(output, '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100"><a xlink:href="http://www.w3.org/Graphics/SVG/" xlink:title="W3C SVG Working Group home page" target="_parent"><rect width="100" height="100" fill="#F90" rx="4" ry="4"/><rect width="50" height="50" fill="#FFB13B" rx="4" ry="4"/><rect width="50" height="50" x="50" y="50" fill="#DE8500" rx="4" ry="4"/><g fill="#F90"><circle cx="50" cy="18.4" r="18.4"/><circle cx="72.4" cy="27.6" r="18.4"/><circle cx="81.6" cy="50" r="18.4"/><circle cx="72.4" cy="72.4" r="18.4"/><circle cx="50" cy="81.6" r="18.4"/><circle cx="27.6" cy="72.4" r="18.4"/><circle cx="18.4" cy="50" r="18.4"/><circle cx="27.6" cy="27.6" r="18.4"/></g><path d="M63.086 18.385c0-7.227-5.859-13.086-13.1-13.086-7.235 0-13.096 5.859-13.096 13.086-5.1-5.11-13.395-5.11-18.497 0-5.119 5.12-5.119 13.408 0 18.524-7.234 0-13.103 5.859-13.103 13.085 0 7.23 5.87 13.098 13.103 13.098-5.119 5.11-5.119 13.395 0 18.515 5.102 5.104 13.397 5.104 18.497 0 0 7.228 5.86 13.083 13.096 13.083 7.24 0 13.1-5.855 13.1-13.083 5.118 5.104 13.416 5.104 18.513 0 5.101-5.12 5.101-13.41 0-18.515 7.216 0 13.081-5.869 13.081-13.098 0-7.227-5.865-13.085-13.081-13.085 5.101-5.119 5.101-13.406 0-18.524-5.097-5.11-13.393-5.11-18.513 0z"/><path fill="#FFF" d="M55.003 23.405v14.488L65.26 27.64c0-1.812.691-3.618 2.066-5.005 2.78-2.771 7.275-2.771 10.024 0a7.086 7.086 0 0 1-5.015 12.099L62.101 44.982H76.59a7.088 7.088 0 0 1 5.011-2.076c3.9 0 7.078 3.179 7.078 7.087 0 3.906-3.178 7.088-7.078 7.088a7.11 7.11 0 0 1-5.011-2.072H62.1l10.229 10.244c1.824 0 3.642.694 5.015 2.086a7.06 7.06 0 0 1 0 10.01c-2.75 2.774-7.239 2.774-10.025 0a7.068 7.068 0 0 1-2.064-5.003L55 62.094v14.499a7.11 7.11 0 0 1 2.084 5.013c0 3.906-3.177 7.077-7.098 7.077-3.919 0-7.094-3.167-7.094-7.077a7.12 7.12 0 0 1 2.081-5.013V62.094L34.738 72.346a7.065 7.065 0 0 1-2.084 5.003 7.08 7.08 0 0 1-10.024 0 7.061 7.061 0 0 1 0-10.012 7.067 7.067 0 0 1 5.012-2.086l10.257-10.242H23.414a7.134 7.134 0 0 1-5.015 2.072c-3.917 0-7.096-3.18-7.096-7.088s3.177-7.087 7.096-7.087c1.94 0 3.725.796 5.015 2.076h14.488L27.646 34.736a7.125 7.125 0 0 1-5.012-2.071 7.08 7.08 0 0 1 0-10.027 7.086 7.086 0 0 1 10.027 0 7.106 7.106 0 0 1 2.083 5.005l10.235 10.252V23.407c-1.27-1.287-2.082-3.053-2.082-5.023 0-3.908 3.175-7.079 7.096-7.079 3.919 0 7.097 3.168 7.097 7.079-.002 1.972-.816 3.735-2.087 5.021z"/><path d="M5.3 50h89.38v40q0 5-5 5H10.3q-5 0-5-5Z"/><path fill="#3F3F3F" d="M14.657 54.211h71.394c2.908 0 5.312 2.385 5.312 5.315v17.91c-27.584-3.403-54.926-8.125-82.011-7.683V59.526c.001-2.93 2.391-5.315 5.305-5.315z"/><path fill="#FFF" stroke="#000" stroke-width=".503" d="M18.312 72.927a11.65 11.65 0 0 1-3.407-8.253c0-6.445 5.223-11.672 11.666-11.672 6.446 0 11.667 5.225 11.667 11.672h-6.832a4.836 4.836 0 1 0-9.673 0 4.87 4.87 0 0 0 1.415 3.42 4.861 4.861 0 0 0 3.423 1.405v.012a11.59 11.59 0 0 1 8.243 3.416 11.617 11.617 0 0 1 3.424 8.248c0 6.454-5.221 11.68-11.667 11.68-6.442 0-11.666-5.222-11.666-11.68h6.828a4.837 4.837 0 0 0 9.673 0 4.786 4.786 0 0 0-1.429-3.407 4.77 4.77 0 0 0-3.406-1.418c-3.23 0-6.142-1.314-8.259-3.423zM61.588 53.005l-8.244 39.849h-6.85l-8.258-39.849h6.846l4.838 23.337 4.835-23.337zM73.255 69.513h11.683v11.664c0 6.452-5.226 11.678-11.669 11.678-6.441 0-11.666-5.226-11.666-11.678V64.676h-.017C61.586 58.229 66.827 53 73.253 53c6.459 0 11.683 5.225 11.683 11.676h-6.849a4.826 4.826 0 0 0-4.834-4.837c-2.647 0-4.82 2.163-4.82 4.837v16.501c0 2.675 2.173 4.837 4.82 4.837a4.823 4.823 0 0 0 4.834-4.827V76.348h-4.834l.002-6.835z"/></a></svg>');
});
test('external-config', async () => {
const result = await buildWebpack(externalConfig);
assert.equal(result.hasErrors(), false);
const output = await fs.readFile(path.join(__dirname, 'example', 'external-config', 'dist', 'SVG_logo.svg'), 'utf8');
assert.equal(output, '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100"><title>SVG Logo</title><a xlink:href="http://www.w3.org/Graphics/SVG/" xlink:title="W3C SVG Working Group home page" target="_parent"><rect width="100" height="100" fill="#FF9900" rx="4" ry="4"/><rect width="50" height="50" fill="#FFB13B" rx="4" ry="4"/><rect width="50" height="50" x="50" y="50" fill="#DE8500" rx="4" ry="4"/><g fill="#FF9900"><circle cx="50" cy="18.4" r="18.4"/><circle cx="72.4" cy="27.6" r="18.4"/><circle cx="81.6" cy="50" r="18.4"/><circle cx="72.4" cy="72.4" r="18.4"/><circle cx="50" cy="81.6" r="18.4"/><circle cx="27.6" cy="72.4" r="18.4"/><circle cx="18.4" cy="50" r="18.4"/><circle cx="27.6" cy="27.6" r="18.4"/></g><path d="M 63.086, 18.385 c 0.000, -7.227 -5.859,-13.086 -13.100,-13.086 c -7.235, 0.000 -13.096, 5.859 -13.096, 13.086 c -5.100, -5.110 -13.395, -5.110 -18.497, 0.000 c -5.119, 5.120 -5.119, 13.408 0.000, 18.524 c -7.234, 0.000 -13.103, 5.859 -13.103, 13.085 c 0.000, 7.230 5.870, 13.098 13.103, 13.098 c -5.119, 5.110 -5.119, 13.395 0.000, 18.515 c 5.102, 5.104 13.397, 5.104 18.497, 0.000 c 0.000, 7.228 5.860, 13.083 13.096, 13.083 c 7.240, 0.000 13.100, -5.855 13.100,-13.083 c 5.118, 5.104 13.416, 5.104 18.513, 0.000 c 5.101, -5.120 5.101,-13.410 0.000,-18.515 c 7.216, 0.000 13.081, -5.869 13.081,-13.098 c 0.000, -7.227 -5.865,-13.085 -13.081,-13.085 c 5.101, -5.119 5.101,-13.406 0.000,-18.524 C 76.502, 13.275 68.206, 13.275 63.086, 18.385 z"/><path fill="#FFFFFF" d="M 55.003, 23.405 v 14.488 L 65.260, 27.640 c 0.000, -1.812 0.691,-3.618 2.066, -5.005 c 2.780, -2.771 7.275,-2.771 10.024, 0.000 c 2.771, 2.766 2.771, 7.255 0.000, 10.027 c -1.377, 1.375 -3.195, 2.072 -5.015, 2.072 L 62.101, 44.982 H 76.590 c 1.290, -1.280 3.054,-2.076 5.011, -2.076 c 3.900, 0.000 7.078, 3.179 7.078, 7.087 c 0.000, 3.906 -3.178, 7.088 -7.078, 7.088 c -1.957, 0.000 -3.721,-0.798 -5.011, -2.072 H 62.100 l 10.229, 10.244 c 1.824, 0.000 3.642, 0.694 5.015, 2.086 c 2.774, 2.759 2.774, 7.250 0.000, 10.010 c -2.750, 2.774 -7.239, 2.774 -10.025, 0.000 c -1.372, -1.372 -2.064,-3.192 -2.064, -5.003 L 55.000, 62.094 v 14.499 c 1.271, 1.276 2.084, 3.054 2.084, 5.013 c 0.000, 3.906 -3.177, 7.077 -7.098, 7.077 c -3.919, 0.000 -7.094,-3.167 -7.094, -7.077 c 0.000, -1.959 0.811,-3.732 2.081, -5.013 V 62.094 L 34.738, 72.346 c 0.000, 1.812 -0.705, 3.627 -2.084, 5.003 c -2.769, 2.772 -7.251, 2.772 -10.024, 0.000 c -2.775, -2.764 -2.775,-7.253 0.000,-10.012 c 1.377, -1.390 3.214,-2.086 5.012, -2.086 l 10.257,-10.242 H 23.414 c -1.289, 1.276 -3.072, 2.072 -5.015, 2.072 c -3.917, 0.000 -7.096,-3.180 -7.096, -7.088 s 3.177, -7.087 7.096,-7.087 c 1.940, 0.000 3.725, 0.796 5.015, 2.076 h 14.488 L 27.646, 34.736 c -1.797, 0.000 -3.632,-0.697 -5.012, -2.071 c -2.775, -2.772 -2.775,-7.260 0.000,-10.027 c 2.773, -2.771 7.256,-2.771 10.027, 0.000 c 1.375, 1.386 2.083, 3.195 2.083, 5.005 l 10.235, 10.252 V 23.407 c -1.270, -1.287 -2.082,-3.053 -2.082, -5.023 c 0.000, -3.908 3.175,-7.079 7.096, -7.079 c 3.919, 0.000 7.097, 3.168 7.097, 7.079 C 57.088, 20.356 56.274,22.119 55.003, 23.405 z"/><path d="M 5.30,50.00 H 94.68 V 90.00 Q 94.68,95.00 89.68,95.00 H 10.30 Q 5.30,95.00 5.30,90.00 Z"/><path fill="#3F3F3F" d="M 14.657,54.211 h 71.394 c 2.908, 0.000 5.312, 2.385 5.312, 5.315 v 17.910 c -27.584,-3.403 -54.926,-8.125 -82.011,-7.683 V 59.526 C 9.353,56.596 11.743,54.211 14.657,54.211 L 14.657,54.211 z"/><title>SVG</title><path fill="#FFFFFF" stroke="#000000" stroke-width=".503" d="M18.312 72.927c-2.103-2.107-3.407-5.028-3.407-8.253 0-6.445 5.223-11.672 11.666-11.672 6.446 0 11.667 5.225 11.667 11.672h-6.832c0-2.674-2.168-4.837-4.835-4.837-2.663 0-4.838 2.163-4.838 4.837 0 1.338.549 2.536 1.415 3.42l0 0c.883.874 2.101 1.405 3.423 1.405v.012c3.232 0 6.145 1.309 8.243 3.416l0 0c2.118 2.111 3.424 5.034 3.424 8.248 0 6.454-5.221 11.68-11.667 11.68-6.442 0-11.666-5.222-11.666-11.68h6.828c0 2.679 2.175 4.835 4.838 4.835 2.667 0 4.835-2.156 4.835-4.835 0-1.329-.545-2.527-1.429-3.407l0 0c-.864-.88-2.082-1.418-3.406-1.418l0 0C23.341 76.35 20.429 75.036 18.312 72.927L18.312 72.927 18.312 72.927zM61.588 53.005 53.344 92.854 46.494 92.854 38.236 53.005 45.082 53.005 49.92 76.342 54.755 53.005zM73.255 69.513h11.683v11.664l0 0c0 6.452-5.226 11.678-11.669 11.678-6.441 0-11.666-5.226-11.666-11.678l0 0V64.676h-.017C61.586 58.229 66.827 53 73.253 53c6.459 0 11.683 5.225 11.683 11.676h-6.849c0-2.674-2.152-4.837-4.834-4.837-2.647 0-4.82 2.163-4.82 4.837v16.501l0 0c0 2.675 2.173 4.837 4.82 4.837 2.682 0 4.834-2.162 4.834-4.827v-.012-4.827h-4.834L73.255 69.513 73.255 69.513z"/></a></svg>');
});
test('svgo-error', async () => {
const result = await buildWebpack(svgoError);
assert.equal(result.hasErrors(), true);
});