Skip to content

Commit 05410ba

Browse files
authored
chore: use vitest for unit testing (#55)
* test: replace ts-node-dev with vitest * chore: use vitest for unit testing * ci: replace Travis CI with GitHub Workflow
1 parent ffafd43 commit 05410ba

6 files changed

Lines changed: 340 additions & 600 deletions

File tree

.github/workflows/ci.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [master]
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- uses: actions/cache@v2
14+
id: yarn-cache
15+
with:
16+
path: "**/node_modules"
17+
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
18+
19+
- name: Run unit tests
20+
run: |
21+
yarn
22+
yarn test

.travis.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
"main": "./lib/plugin.js",
88
"types": "./lib/plugin.d.ts",
99
"scripts": {
10-
"test": "tsnd src/tests",
11-
"test:tdd": "tsnd --respawn src/tests",
10+
"test": "vitest",
1211
"prepack": "tsc"
1312
},
1413
"dependencies": {
@@ -17,8 +16,8 @@
1716
},
1817
"devDependencies": {
1918
"@types/node": "^14.14.9",
20-
"ts-node-dev": "^1.0.0",
21-
"typescript": "^4.1.2"
19+
"typescript": "^4.1.2",
20+
"vitest": "^0.18.1"
2221
},
2322
"repository": {
2423
"type": "git",

src/tests/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/tests/plugin.test.ts

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { strict as test } from "assert";
1+
import { test, expect } from "vitest";
22
import { hash, replaceHash } from "../plugin";
33
import fs from "fs";
44
import path from "path";
@@ -13,38 +13,37 @@ const buffer = fs.readFileSync(path.resolve(__dirname, "__fixtures__/original/bu
1313
const DEFAULT_HASH_LENGTH = 20;
1414
const DEFAULT_PATTERN = new RegExp(/\[hash.*]/g);
1515

16-
test.strictEqual(replaceHash("[hash].js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH), "b0dcc67ffc1fd562f212.js");
17-
test.strictEqual(
18-
replaceHash("script.[hash].js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH),
19-
"script.b0dcc67ffc1fd562f212.js"
20-
);
21-
test.strictEqual(
22-
replaceHash("script.[hash:20].js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH),
23-
"script.b0dcc67ffc1fd562f212.js"
24-
);
25-
test.strictEqual(replaceHash("script.[hash:8].js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH), "script.b0dcc67f.js");
26-
test.throws(() => replaceHash("script.js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH));
27-
test.throws(() => replaceHash("script[].js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH));
28-
test.throws(() => replaceHash("script.[has:8].js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH));
29-
test.throws(() => replaceHash("script.js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH));
16+
test("posthtml-hash", () => {
17+
expect(replaceHash("[hash].js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH)).toEqual("b0dcc67ffc1fd562f212.js");
18+
expect(replaceHash("script.[hash].js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH)).toEqual(
19+
"script.b0dcc67ffc1fd562f212.js"
20+
);
21+
expect(replaceHash("script.[hash:20].js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH)).toEqual(
22+
"script.b0dcc67ffc1fd562f212.js"
23+
);
24+
expect(replaceHash("script.[hash:8].js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH)).toEqual("script.b0dcc67f.js");
25+
expect(() => replaceHash("script[].js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH)).toThrow();
26+
expect(() => replaceHash("script.[has:8].js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH)).toThrow();
27+
expect(() => replaceHash("script.js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH)).toThrow();
28+
expect(() => replaceHash("script.js", buffer, DEFAULT_PATTERN, DEFAULT_HASH_LENGTH)).toThrow();
3029

31-
const CUSTOM_EXP = new RegExp(/\[oh-my-hash.*]/g);
30+
const CUSTOM_EXP = new RegExp(/\[oh-my-hash.*]/g);
3231

33-
test.strictEqual(replaceHash("[oh-my-hash].js", buffer, CUSTOM_EXP, DEFAULT_HASH_LENGTH), "b0dcc67ffc1fd562f212.js");
34-
test.strictEqual(replaceHash("script.[oh-my-hash].js", buffer, CUSTOM_EXP, 8), "script.b0dcc67f.js");
32+
expect(replaceHash("[oh-my-hash].js", buffer, CUSTOM_EXP, DEFAULT_HASH_LENGTH), "b0dcc67ffc1fd562f212.js");
33+
expect(replaceHash("script.[oh-my-hash].js", buffer, CUSTOM_EXP, 8), "script.b0dcc67f.js");
3534

36-
const CUSTOM_EXP_2 = new RegExp(/\oh-my-hash/);
35+
const CUSTOM_EXP_2 = new RegExp(/\oh-my-hash/);
3736

38-
test.strictEqual(replaceHash("oh-my-hash.js", buffer, CUSTOM_EXP_2, DEFAULT_HASH_LENGTH), "b0dcc67ffc1fd562f212.js");
39-
test.strictEqual(replaceHash("script.oh-my-hash.js", buffer, CUSTOM_EXP_2, 8), "script.b0dcc67f.js");
37+
expect(replaceHash("oh-my-hash.js", buffer, CUSTOM_EXP_2, DEFAULT_HASH_LENGTH), "b0dcc67ffc1fd562f212.js");
38+
expect(replaceHash("script.oh-my-hash.js", buffer, CUSTOM_EXP_2, 8), "script.b0dcc67f.js");
4039

41-
const CUSTOM_EXP_3 = new RegExp(/custom-hash/);
40+
const CUSTOM_EXP_3 = new RegExp(/custom-hash/);
4241

43-
test.strictEqual(
44-
replaceHash("script.custom-hash.js", buffer, CUSTOM_EXP_3, DEFAULT_HASH_LENGTH),
45-
"script.b0dcc67ffc1fd562f212.js"
46-
);
47-
test.strictEqual(replaceHash("script.custom-hash.js", buffer, CUSTOM_EXP_3, 4), "script.b0dc.js");
42+
expect(replaceHash("script.custom-hash.js", buffer, CUSTOM_EXP_3, DEFAULT_HASH_LENGTH)).toEqual(
43+
"script.b0dcc67ffc1fd562f212.js"
44+
);
45+
expect(replaceHash("script.custom-hash.js", buffer, CUSTOM_EXP_3, 4)).toEqual("script.b0dc.js");
46+
});
4847

4948
function copyFixture(fileName: string) {
5049
const file = path.join(__dirname, "__fixtures__/original", fileName);
@@ -69,7 +68,7 @@ async function fixture() {
6968
</html>`
7069
);
7170

72-
test.strictEqual(
71+
expect(
7372
result.html.replace(/\n|\s+/g, ""),
7473
'<html><head><linkrel="stylesheet"href="https://fonts.googleapis.com/css?family=Open+Sans:400,400i,600,600i,700,700i"><linkrel="stylesheet"href="bundle.min.9a6cf95c41e87b9dc102.css"></head><body><scriptsrc="bundle.min.b0dcc67ffc1fd562f212.js"></script></body></html>'
7574
);
@@ -96,7 +95,7 @@ async function fixture() {
9695
</html>`
9796
);
9897

99-
test.strictEqual(
98+
expect(
10099
result2.html.replace(/\n|\s+/g, ""),
101100
'<html><body><scriptsrc="script.b0dcc67f.js"></script><scriptsrc="script.b0dcc67f.js"></script></body></html>'
102101
);

0 commit comments

Comments
 (0)