Skip to content

Commit a8484c8

Browse files
committed
refactor(tests): Switch to Jest, remove custom helpers
1 parent e463851 commit a8484c8

13 files changed

Lines changed: 7565 additions & 4539 deletions

.mocharc.json

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

.travis.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
language: node_js
22
node_js:
33
- lts/*
4-
script:
5-
- npm run lcov
6-
- npm run lint
7-
after_success: npm run coveralls
4+
after_success: npm run coverage

package-lock.json

Lines changed: 5580 additions & 1920 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,38 @@
2323
"nth-check": "^1.0.2"
2424
},
2525
"devDependencies": {
26-
"@types/expect.js": "^0.3.29",
27-
"@types/mocha": "^8.0.3",
26+
"@types/jest": "^26.0.14",
2827
"@types/node": "^14.0.5",
2928
"@typescript-eslint/eslint-plugin": "^4.1.0",
3029
"@typescript-eslint/parser": "^4.1.0",
3130
"cheerio-soupselect": "^0.1.1",
3231
"coveralls": "^3.0.2",
3332
"eslint": "^7.9.0",
3433
"eslint-config-prettier": "^6.0.0",
35-
"expect.js": "^0.3.1",
3634
"htmlparser2": "^4.0.0",
37-
"mocha": "^8.0.1",
38-
"nyc": "^15.1.0",
35+
"jest": "^26.4.2",
3936
"prettier": "^2.1.2",
40-
"ts-node": "^9.0.0",
37+
"ts-jest": "^26.4.0",
4138
"typescript": "^4.0.2"
4239
},
4340
"scripts": {
44-
"test": "mocha --parallel && npm run lint",
45-
"lint": "eslint .",
46-
"lcov": "nyc mocha",
47-
"coveralls": "(nyc report --reporter=text-lcov | coveralls)",
41+
"test": "jest --coverage && npm run lint",
42+
"coverage": "cat coverage/lcov.info | coveralls",
43+
"lint": "eslint src",
44+
"format": "prettier --write '**/*.{ts,md,json}'",
4845
"build": "tsc",
4946
"prepare": "npm run build"
5047
},
5148
"license": "BSD-2-Clause",
5249
"types": "index.d.ts",
5350
"prettier": {
5451
"tabWidth": 4
52+
},
53+
"jest": {
54+
"preset": "ts-jest",
55+
"testEnvironment": "node",
56+
"testMatch": [
57+
"<rootDir>/test/*.ts"
58+
]
5559
}
5660
}

test/.eslintrc.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
{
2-
"env": {
3-
"mocha": true
4-
},
5-
"rules": {
6-
"capitalized-comments": 0,
7-
"multiline-comment-style": 0
8-
}
2+
"env": { "jest": true }
93
}

test/api.ts

Lines changed: 54 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as CSSselect from "../src";
22
import { parseDOM as makeDom } from "htmlparser2";
33
import { trueFunc, falseFunc } from "boolbase";
4-
import assert from "assert";
54
import type { Element } from "domhandler";
65

76
const [dom] = makeDom("<div id=foo><p>foo</p></div>") as Element[];
@@ -12,174 +11,167 @@ const [xmlDom] = makeDom("<DiV id=foo><P>foo</P></DiV>", {
1211
describe("API", () => {
1312
describe("removes duplicates", () => {
1413
it("between identical trees", () => {
15-
const matches = CSSselect.selectAll("div", [dom, dom]);
16-
assert.strictEqual(matches.length, 1, "Removes duplicate matches");
14+
expect(CSSselect.selectAll("div", [dom, dom])).toHaveLength(1);
1715
});
1816
it("between a superset and subset", () => {
19-
const matches = CSSselect.selectAll("p", [dom, dom.children[0]]);
20-
assert.strictEqual(matches.length, 1, "Removes duplicate matches");
17+
expect(
18+
CSSselect.selectAll("p", [dom, dom.children[0]])
19+
).toHaveLength(1);
2120
});
2221
it("betweeen a subset and superset", () => {
23-
const matches = CSSselect.selectAll("p", [dom.children[0], dom]);
24-
assert.strictEqual(matches.length, 1, "Removes duplicate matches");
22+
expect(
23+
CSSselect.selectAll("p", [dom.children[0], dom])
24+
).toHaveLength(1);
2525
});
2626
});
2727

2828
describe("can be queried by function", () => {
2929
it("in `is`", () => {
30-
assert(CSSselect.is(dom, (elem) => elem.attribs.id === "foo"));
30+
expect(CSSselect.is(dom, (elem) => elem.attribs.id === "foo")).toBe(
31+
true
32+
);
3133
});
3234
// Probably more cases should be added here
3335
});
3436

3537
describe("selectAll", () => {
3638
it("should query array elements directly when they have no parents", () => {
3739
const divs = [dom];
38-
assert.deepEqual(CSSselect.selectAll("div", divs), divs);
40+
expect(CSSselect.selectAll("div", divs)).toStrictEqual(divs);
3941
});
4042
it("should query array elements directly when they have parents", () => {
4143
const ps = CSSselect.selectAll("p", [dom]);
42-
assert.deepEqual(CSSselect.selectAll("p", ps), ps);
44+
expect(CSSselect.selectAll("p", ps)).toStrictEqual(ps);
4345
});
4446
it("should support pseudos led by a traversal (#111)", () => {
4547
const [dom] = makeDom(
4648
'<div><div class="foo">a</div><div class="bar">b</div></div>'
4749
) as Element[];
4850
const a = CSSselect.selectAll(".foo:has(+.bar)", dom);
49-
assert.strictEqual(a.length, 1);
50-
assert.strictEqual(a[0], dom.children[0]);
51+
expect(a).toHaveLength(1);
52+
expect(a[0]).toStrictEqual(dom.children[0]);
5153
});
5254
});
5355

5456
describe("unsatisfiable and universally valid selectors", () => {
5557
it("in :not", () => {
5658
let func = CSSselect._compileUnsafe(":not(*)");
57-
assert.strictEqual(func, falseFunc);
59+
expect(func).toBe(falseFunc);
5860
func = CSSselect._compileUnsafe(":not(:nth-child(-1n-1))");
59-
assert.strictEqual(func, trueFunc);
61+
expect(func).toBe(trueFunc);
6062
func = CSSselect._compileUnsafe(":not(:not(:not(*)))");
61-
assert.strictEqual(func, falseFunc);
63+
expect(func).toBe(falseFunc);
6264
});
6365

6466
it("in :has", () => {
6567
const matches = CSSselect.selectAll(":has(*)", [dom]);
66-
assert.strictEqual(matches.length, 1);
67-
assert.strictEqual(matches[0], dom);
68+
expect(matches).toHaveLength(1);
69+
expect(matches[0]).toBe(dom);
6870
const func = CSSselect._compileUnsafe(":has(:nth-child(-1n-1))");
69-
assert.strictEqual(func, falseFunc);
71+
expect(func).toBe(falseFunc);
7072
});
7173

7274
it("should skip unsatisfiable", () => {
7375
const func = CSSselect._compileUnsafe("* :not(*) foo");
74-
assert.strictEqual(func, falseFunc);
76+
expect(func).toBe(falseFunc);
7577
});
7678

7779
it("should promote universally valid", () => {
7880
const func = CSSselect._compileUnsafe("*, foo");
79-
assert.strictEqual(func, trueFunc);
81+
expect(func).toBe(trueFunc);
8082
});
8183
});
8284

8385
describe(":matches", () => {
8486
it("should select multiple elements", () => {
8587
let matches = CSSselect.selectAll(":matches(p, div)", [dom]);
86-
assert.strictEqual(matches.length, 2);
88+
expect(matches).toHaveLength(2);
8789
matches = CSSselect.selectAll(":matches(div, :not(div))", [dom]);
88-
assert.strictEqual(matches.length, 2);
90+
expect(matches).toHaveLength(2);
8991
matches = CSSselect.selectAll(
9092
":matches(boo, baa, tag, div, foo, bar, baz)",
9193
[dom]
9294
);
93-
assert.strictEqual(matches.length, 1);
94-
assert.strictEqual(matches[0], dom);
95+
expect(matches).toHaveLength(1);
96+
expect(matches[0]).toBe(dom);
9597
});
9698

9799
it("should strip quotes", () => {
98100
let matches = CSSselect.selectAll(":matches('p, div')", [dom]);
99-
assert.strictEqual(matches.length, 2);
101+
expect(matches).toHaveLength(2);
100102
matches = CSSselect.selectAll(':matches("p, div")', [dom]);
101-
assert.strictEqual(matches.length, 2);
103+
expect(matches).toHaveLength(2);
102104
});
103105
});
104106

105107
describe("parent selector (<)", () => {
106108
it("should select the right element", () => {
107109
const matches = CSSselect.selectAll("p < div", [dom]);
108-
assert.strictEqual(matches.length, 1);
109-
assert.strictEqual(matches[0], dom);
110+
expect(matches).toHaveLength(1);
111+
expect(matches[0]).toBe(dom);
110112
});
111113
it("should not select nodes without children", () => {
112114
const matches = CSSselect.selectAll("p < div", [dom]);
113-
assert.deepStrictEqual(
114-
matches,
115-
CSSselect.selectAll("* < *", [dom])
116-
);
115+
expect(matches).toStrictEqual(CSSselect.selectAll("* < *", [dom]));
117116
});
118117
});
119118

120119
describe("selectOne", () => {
121120
it("should select elements in traversal order", () => {
122-
let match = CSSselect.selectOne("p", [dom]);
123-
assert.strictEqual(match, dom.children[0]);
124-
match = CSSselect.selectOne(":contains(foo)", [dom]);
125-
assert.strictEqual(match, dom);
121+
expect(CSSselect.selectOne("p", [dom])).toBe(dom.children[0]);
122+
expect(CSSselect.selectOne(":contains(foo)", [dom])).toBe(dom);
126123
});
127124
it("should take shortcuts when applicable", () => {
128125
let match = CSSselect.selectOne(falseFunc, {
129126
get length() {
130127
throw new Error("Did not take shortcut");
131128
},
132129
});
133-
assert.strictEqual(match, null);
130+
expect(match).toBeNull();
134131
match = CSSselect.selectOne("*", []);
135-
assert.strictEqual(match, null);
132+
expect(match).toBeNull();
136133
});
137134
});
138135

139136
describe("options", () => {
140137
const opts = { xmlMode: true };
141138
it("should recognize xmlMode in :has and :not", () => {
142-
assert(CSSselect.is(xmlDom, "DiV:has(P)", opts));
143-
assert(CSSselect.is(xmlDom, "DiV:not(div)", opts));
144-
assert(
139+
expect(CSSselect.is(xmlDom, "DiV:has(P)", opts)).toBe(true);
140+
expect(CSSselect.is(xmlDom, "DiV:not(div)", opts)).toBe(true);
141+
expect(
145142
CSSselect.is(xmlDom.children[0], "DiV:has(P) :not(p)", opts)
146-
);
143+
).toBe(true);
147144
});
148145

149146
it("should be strict", () => {
150147
const opts = { strict: true };
151-
assert.throws(() => CSSselect.compile(":checkbox", opts), Error);
152-
assert.throws(() => CSSselect.compile("[attr=val i]", opts), Error);
153-
assert.throws(() => CSSselect.compile("[attr!=val]", opts), Error);
154-
assert.throws(
155-
() => CSSselect.compile("[attr!=val i]", opts),
148+
expect(() => CSSselect.compile(":checkbox", opts)).toThrow(Error);
149+
expect(() => CSSselect.compile("[attr=val i]", opts)).toThrow(
156150
Error
157151
);
158-
assert.throws(() => CSSselect.compile("foo < bar", opts), Error);
159-
assert.throws(
160-
() => CSSselect.compile(":not(:parent)", opts),
152+
expect(() => CSSselect.compile("[attr!=val]", opts)).toThrow(Error);
153+
expect(() => CSSselect.compile("[attr!=val i]", opts)).toThrow(
161154
Error
162155
);
163-
assert.throws(() => CSSselect.compile(":not(a > b)", opts), Error);
164-
assert.throws(() => CSSselect.compile(":not(a, b)", opts), Error);
156+
expect(() => CSSselect.compile("foo < bar", opts)).toThrow(Error);
157+
expect(() => CSSselect.compile(":not(:parent)", opts)).toThrow(
158+
Error
159+
);
160+
expect(() => CSSselect.compile(":not(a > b)", opts)).toThrow(Error);
161+
expect(() => CSSselect.compile(":not(a, b)", opts)).toThrow(Error);
165162
});
166163

167164
it("should recognize contexts", () => {
168165
const div = CSSselect.selectAll("div", [dom]);
169166
const p = CSSselect.selectAll("p", [dom]);
170167

171-
assert.strictEqual(
172-
CSSselect.selectOne("div", div, { context: div }),
168+
expect(CSSselect.selectOne("div", div, { context: div })).toBe(
173169
div[0]
174170
);
175-
assert.strictEqual(
176-
CSSselect.selectOne("div", div, { context: p }),
177-
null
178-
);
179-
assert.deepStrictEqual(
180-
CSSselect.selectAll("p", div, { context: div }),
181-
p
182-
);
171+
expect(CSSselect.selectOne("div", div, { context: p })).toBe(null);
172+
expect(
173+
CSSselect.selectAll("p", div, { context: div })
174+
).toStrictEqual(p);
183175
});
184176
});
185177
});

0 commit comments

Comments
 (0)