Skip to content

Commit feb4220

Browse files
committed
Add Example Testing
Test the examples and use a snapshot verify the console output. This allows us to make breaking changes while knowing if the usage of the library has changed significantly.
1 parent 1318649 commit feb4220

File tree

2 files changed

+253
-0
lines changed

2 files changed

+253
-0
lines changed
+230
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`examples > example 01-hello-world.mts > expected consistent console errors 1`] = `[]`;
4+
5+
exports[`examples > example 01-hello-world.mts > expected consistent console logs 1`] = `
6+
[
7+
[
8+
"hello-world!",
9+
],
10+
]
11+
`;
12+
13+
exports[`examples > example 02-nested-boolean-logic.mts > expected consistent console errors 1`] = `[]`;
14+
15+
exports[`examples > example 02-nested-boolean-logic.mts > expected consistent console logs 1`] = `
16+
[
17+
[
18+
"Player has fouled out!",
19+
],
20+
]
21+
`;
22+
23+
exports[`examples > example 03-dynamic-facts.mts > expected consistent console errors 1`] = `[]`;
24+
25+
exports[`examples > example 03-dynamic-facts.mts > expected consistent console logs 1`] = `
26+
[
27+
[
28+
"loading account information for "lincoln"",
29+
],
30+
[
31+
"lincoln is a current microsoft employee taking christmas day off",
32+
],
33+
]
34+
`;
35+
36+
exports[`examples > example 04-fact-dependency.mts > expected consistent console errors 1`] = `[]`;
37+
38+
exports[`examples > example 04-fact-dependency.mts > expected consistent console logs 1`] = `
39+
[
40+
[
41+
"-- FIRST RUN --",
42+
],
43+
[
44+
"loading account information for "washington"",
45+
],
46+
[
47+
"washington DID meet conditions for the five-year-tenure rule.",
48+
],
49+
[
50+
"washington DID meet conditions for the microsoft-terminated-employees rule.",
51+
],
52+
[
53+
"-- SECOND RUN --",
54+
],
55+
[
56+
"loading account information for "jefferson"",
57+
],
58+
[
59+
"jefferson DID meet conditions for the five-year-tenure rule.",
60+
],
61+
[
62+
"jefferson did NOT meet conditions for the microsoft-terminated-employees rule.",
63+
],
64+
]
65+
`;
66+
67+
exports[`examples > example 05-optimizing-runtime-with-fact-priorities.mts > expected consistent console errors 1`] = `[]`;
68+
69+
exports[`examples > example 05-optimizing-runtime-with-fact-priorities.mts > expected consistent console logs 1`] = `
70+
[
71+
[
72+
"Checking the "date" fact...",
73+
],
74+
[
75+
"washington did NOT meet conditions for the microsoft-employees rule.",
76+
],
77+
]
78+
`;
79+
80+
exports[`examples > example 06-custom-operators.mts > expected consistent console errors 1`] = `[]`;
81+
82+
exports[`examples > example 06-custom-operators.mts > expected consistent console logs 1`] = `
83+
[
84+
[
85+
"bacon did NOT start with "a"",
86+
],
87+
[
88+
"bacon DID start with "b"",
89+
],
90+
[
91+
"antelope DID start with "a"",
92+
],
93+
[
94+
"antelope did NOT start with "b"",
95+
],
96+
]
97+
`;
98+
99+
exports[`examples > example 07-rule-chaining.mts > expected consistent console errors 1`] = `[]`;
100+
101+
exports[`examples > example 07-rule-chaining.mts > expected consistent console logs 1`] = `
102+
[
103+
[
104+
"loading account information for "washington"",
105+
],
106+
[
107+
"washington(microsoft) DID meet conditions for the drinks-screwdrivers rule.",
108+
],
109+
[
110+
"washington(microsoft) DID meet conditions for the invite-to-screwdriver-social rule.",
111+
],
112+
[
113+
"washington IS a screwdriver aficionado",
114+
],
115+
[
116+
"jefferson did NOT meet conditions for the drinks-screwdrivers rule.",
117+
],
118+
[
119+
"jefferson did NOT meet conditions for the invite-to-screwdriver-social rule.",
120+
],
121+
[
122+
"jefferson IS NOT a screwdriver aficionado",
123+
],
124+
]
125+
`;
126+
127+
exports[`examples > example 08-fact-comparison.mts > expected consistent console errors 1`] = `[]`;
128+
129+
exports[`examples > example 08-fact-comparison.mts > expected consistent console logs 1`] = `
130+
[
131+
[
132+
"washington DID meet conditions for the customer-can-afford-gift-card rule.",
133+
],
134+
[
135+
"jefferson did NOT meet conditions for the customer-can-afford-gift-card rule.",
136+
],
137+
]
138+
`;
139+
140+
exports[`examples > example 09-rule-results.mts > expected consistent console errors 1`] = `[]`;
141+
142+
exports[`examples > example 09-rule-results.mts > expected consistent console logs 1`] = `
143+
[
144+
[
145+
"joe failed Athlete GPA Rule - was not an athlete",
146+
],
147+
[
148+
"larry succeeded Athlete GPA Rule! Student made the athletics honor-roll",
149+
],
150+
[
151+
"jane failed Athlete GPA Rule - was not an athlete and GPA of 3.1 was too low",
152+
],
153+
[
154+
"janet succeeded Athlete GPA Rule! Student made the athletics honor-roll",
155+
],
156+
[
157+
"sarah failed Athlete GPA Rule - GPA of 1.1 was too low",
158+
],
159+
]
160+
`;
161+
162+
exports[`examples > example 10-condition-sharing.mts > expected consistent console errors 1`] = `[]`;
163+
164+
exports[`examples > example 10-condition-sharing.mts > expected consistent console logs 1`] = `
165+
[
166+
[
167+
"washingtonDID meet conditions for the invite-to-screwdriver-social rule.",
168+
],
169+
[
170+
"washington did NOT meet conditions for the invite-to-other-social rule.",
171+
],
172+
[
173+
"jefferson did NOT meet conditions for the invite-to-screwdriver-social rule.",
174+
],
175+
[
176+
"jeffersonDID meet conditions for the invite-to-other-social rule.",
177+
],
178+
]
179+
`;
180+
181+
exports[`examples > example 11-using-facts-in-events.mts > expected consistent console errors 1`] = `[]`;
182+
183+
exports[`examples > example 11-using-facts-in-events.mts > expected consistent console logs 1`] = `[]`;
184+
185+
exports[`examples > example 12-using-custom-almanac.mts > expected consistent console errors 1`] = `[]`;
186+
187+
exports[`examples > example 12-using-custom-almanac.mts > expected consistent console logs 1`] = `
188+
[
189+
[
190+
"HIGH SCORE
191+
DOG - 968",
192+
],
193+
[
194+
"
195+
",
196+
],
197+
[
198+
"HIGH SCORE
199+
AAA - 1000",
200+
],
201+
[
202+
"Bob is 19 years old and is not Over 21(ish)",
203+
],
204+
[
205+
"Alice is 21 years old and is Over 21(ish)",
206+
],
207+
[
208+
"Chad is 20 years old and is Over 21(ish)",
209+
],
210+
]
211+
`;
212+
213+
exports[`examples > example 13-using-operator-decorators.mts > expected consistent console errors 1`] = `[]`;
214+
215+
exports[`examples > example 13-using-operator-decorators.mts > expected consistent console logs 1`] = `
216+
[
217+
[
218+
"dev, prod WERE all valid tags",
219+
],
220+
[
221+
"dev, deleted WERE NOT all valid tags",
222+
],
223+
[
224+
"dev, PROD WERE NOT all valid tags",
225+
],
226+
[
227+
"dev, PROD WERE all valid tags (case insensitive)",
228+
],
229+
]
230+
`;

test/examples.test.mts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { beforeAll, beforeEach, describe, expect, it, Mock, vi } from 'vitest';
2+
import { readdirSync } from 'node:fs';
3+
import { resolve } from 'node:path';
4+
import rulesEngineDefault, * as ruleEngine from '../src/index.mjs';
5+
6+
const exampleDir = resolve(__dirname, '../examples');
7+
8+
describe('examples', () => {
9+
beforeAll(() => {
10+
vi.mock('json-rules-engine', () => ({...ruleEngine, default: rulesEngineDefault }));
11+
});
12+
13+
beforeEach(() => {
14+
vi.spyOn(console, 'log');
15+
vi.spyOn(console, 'error');
16+
})
17+
18+
it.each(readdirSync(exampleDir).filter(fileName => fileName.endsWith(".mts")))('example %s', async (filename) => {
19+
await (await import(resolve(exampleDir, filename))).default;
20+
expect.soft((console.log as Mock).mock.calls).toMatchSnapshot("expected consistent console logs");
21+
expect.soft((console.error as Mock).mock.calls).toMatchSnapshot("expected consistent console errors");
22+
})
23+
})

0 commit comments

Comments
 (0)