Skip to content

Commit 454f679

Browse files
committed
Move to Vitest for testing
Vitest can test all the files, including testing the types.
1 parent c2c7a79 commit 454f679

38 files changed

+1495
-1328
lines changed

package.json

+5-20
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
"description": "Rules Engine expressed in simple json",
55
"main": "dist/index.js",
66
"types": "types/index.d.ts",
7+
"type": "module",
78
"engines": {
89
"node": ">=18.0.0"
910
},
1011
"scripts": {
11-
"test": "mocha && npm run lint --silent && npm run test:types",
12-
"test:types": "tsd",
12+
"test": "vitest --typecheck",
1313
"lint": "eslint",
1414
"format": "prettier -w .",
1515
"prepublishOnly": "npm run build",
@@ -29,18 +29,6 @@
2929
"publishConfig": {
3030
"tag": "next"
3131
},
32-
"mocha": {
33-
"require": [
34-
"babel-core/register",
35-
"babel-polyfill"
36-
],
37-
"file": "./test/support/bootstrap.js",
38-
"checkLeaks": true,
39-
"recursive": true,
40-
"globals": [
41-
"expect"
42-
]
43-
},
4432
"author": "Cache Hamm <[email protected]>",
4533
"contributors": [
4634
"Chris Pardy <[email protected]>"
@@ -60,20 +48,17 @@
6048
"babel-preset-es2015": "~6.24.1",
6149
"babel-preset-stage-0": "~6.24.1",
6250
"babel-register": "6.26.0",
63-
"chai": "^4.3.4",
64-
"chai-as-promised": "^7.1.1",
6551
"colors": "~1.4.0",
6652
"dirty-chai": "2.0.1",
6753
"eslint": "^9.13.0",
6854
"globals": "^15.11.0",
6955
"lodash": "4.17.21",
70-
"mocha": "^8.4.0",
7156
"perfy": "^1.1.5",
7257
"prettier": "^3.3.3",
73-
"sinon": "^11.1.1",
74-
"sinon-chai": "^3.7.0",
7558
"tsd": "^0.17.0",
76-
"typescript-eslint": "^8.11.0"
59+
"typescript": "^5.6.3",
60+
"typescript-eslint": "^8.11.0",
61+
"vitest": "^2.1.3"
7762
},
7863
"dependencies": {
7964
"clone": "^2.1.2",

test/acceptance/acceptance.js renamed to test/acceptance/acceptance.test.mjs

+53-44
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
1-
"use strict";
2-
3-
import sinon from "sinon";
4-
import { expect } from "chai";
5-
import { Engine } from "../../src/index";
6-
1+
import { Engine } from "../../src/index.mjs";
2+
import { describe, it, expect, vi } from "vitest";
73
/**
84
* acceptance tests are intended to use features that, when used in combination,
95
* could cause integration bugs not caught by the rest of the test suite
106
*/
117
describe("Acceptance", () => {
12-
let sandbox;
13-
before(() => {
14-
sandbox = sinon.createSandbox();
15-
});
16-
afterEach(() => {
17-
sandbox.restore();
18-
});
198
const factParam = 1;
209
const event1 = {
2110
type: "event-1",
@@ -61,8 +50,8 @@ describe("Acceptance", () => {
6150

6251
function setup(options = {}) {
6352
const engine = new Engine();
64-
highPrioritySpy = sandbox.spy();
65-
lowPrioritySpy = sandbox.spy();
53+
highPrioritySpy = vi.fn();
54+
lowPrioritySpy = vi.fn();
6655

6756
engine.addRule({
6857
name: "first",
@@ -87,10 +76,10 @@ describe("Acceptance", () => {
8776
},
8877
event: event1,
8978
onSuccess: async (event, almanac, ruleResults) => {
90-
expect(ruleResults.name).to.equal("first");
91-
expect(ruleResults.event).to.deep.equal(event1);
92-
expect(ruleResults.priority).to.equal(10);
93-
expect(ruleResults.conditions).to.deep.equal(expectedFirstRuleResult);
79+
expect(ruleResults.name).toBe("first");
80+
expect(ruleResults.event).toEqual(event1);
81+
expect(ruleResults.priority).toBe(10);
82+
expect(ruleResults.conditions).toEqual(expectedFirstRuleResult);
9483

9584
return delay(
9685
almanac.addRuntimeFact("rule-created-fact", {
@@ -150,8 +139,8 @@ describe("Acceptance", () => {
150139
const baseIndex = await almanac.factValue("baseIndex");
151140
return delay(baseIndex);
152141
});
153-
successSpy = sandbox.spy();
154-
failureSpy = sandbox.spy();
142+
successSpy = vi.fn();
143+
failureSpy = vi.fn();
155144
engine.on("success", successSpy);
156145
engine.on("failure", failureSpy);
157146

@@ -169,8 +158,8 @@ describe("Acceptance", () => {
169158
);
170159

171160
// results
172-
expect(results.length).to.equal(2);
173-
expect(results[0]).to.deep.equal({
161+
expect(results.length).toBe(2);
162+
expect(results[0]).toEqual({
174163
conditions: {
175164
all: [
176165
{
@@ -205,7 +194,7 @@ describe("Acceptance", () => {
205194
priority: 10,
206195
result: true,
207196
});
208-
expect(results[1]).to.deep.equal({
197+
expect(results[1]).toEqual({
209198
conditions: {
210199
all: [
211200
{
@@ -233,20 +222,30 @@ describe("Acceptance", () => {
233222
priority: 1,
234223
result: true,
235224
});
236-
expect(failureResults).to.be.empty();
225+
expect(failureResults).toHaveLength(0);
237226

238227
// events
239-
expect(failureEvents.length).to.equal(0);
240-
expect(events.length).to.equal(2);
241-
expect(events[0]).to.deep.equal(event1);
242-
expect(events[1]).to.deep.equal(event2);
228+
expect(failureEvents.length).toBe(0);
229+
expect(events.length).toBe(2);
230+
expect(events[0]).toEqual(event1);
231+
expect(events[1]).toEqual(event2);
243232

244233
// callbacks
245-
expect(successSpy).to.have.been.calledTwice();
246-
expect(successSpy).to.have.been.calledWith(event1);
247-
expect(successSpy).to.have.been.calledWith(event2);
248-
expect(highPrioritySpy).to.have.been.calledBefore(lowPrioritySpy);
249-
expect(failureSpy).to.not.have.been.called();
234+
expect(successSpy).toHaveBeenCalledTimes(2);
235+
expect(successSpy).toHaveBeenCalledWith(
236+
event1,
237+
expect.anything(),
238+
expect.anything(),
239+
);
240+
expect(successSpy).toHaveBeenCalledWith(
241+
event2,
242+
expect.anything(),
243+
expect.anything(),
244+
);
245+
expect(Math.min(...highPrioritySpy.mock.invocationCallOrder)).toBeLessThan(
246+
Math.min(...lowPrioritySpy.mock.invocationCallOrder),
247+
);
248+
expect(failureSpy).not.toHaveBeenCalled();
250249
});
251250

252251
it("fails", async () => {
@@ -259,16 +258,26 @@ describe("Acceptance", () => {
259258
{ baseIndex: 1, "rule-created-fact": "" },
260259
);
261260

262-
expect(results.length).to.equal(0);
263-
expect(failureResults.length).to.equal(2);
264-
expect(failureResults.every((rr) => rr.result === false)).to.be.true();
261+
expect(results.length).toBe(0);
262+
expect(failureResults.length).toBe(2);
263+
expect(failureResults.every((rr) => rr.result === false)).toBe(true);
265264

266-
expect(events.length).to.equal(0);
267-
expect(failureEvents.length).to.equal(2);
268-
expect(failureSpy).to.have.been.calledTwice();
269-
expect(failureSpy).to.have.been.calledWith(event1);
270-
expect(failureSpy).to.have.been.calledWith(event2);
271-
expect(highPrioritySpy).to.have.been.calledBefore(lowPrioritySpy);
272-
expect(successSpy).to.not.have.been.called();
265+
expect(events.length).toBe(0);
266+
expect(failureEvents.length).toBe(2);
267+
expect(failureSpy).toHaveBeenCalledTimes(2);
268+
expect(failureSpy).toHaveBeenCalledWith(
269+
event1,
270+
expect.anything(),
271+
expect.anything(),
272+
);
273+
expect(failureSpy).toHaveBeenCalledWith(
274+
event2,
275+
expect.anything(),
276+
expect.anything(),
277+
);
278+
expect(Math.min(...highPrioritySpy.mock.invocationCallOrder)).toBeLessThan(
279+
Math.min(...lowPrioritySpy.mock.invocationCallOrder),
280+
);
281+
expect(successSpy).not.toHaveBeenCalled();
273282
});
274283
});

0 commit comments

Comments
 (0)