Skip to content

Commit ed3cdbb

Browse files
committed
Fix Typing in Tests
1 parent f68a446 commit ed3cdbb

File tree

4 files changed

+21
-40
lines changed

4 files changed

+21
-40
lines changed

examples/01-hello-world.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async function start() {
5050
// engine.run() evaluates the rule using the facts provided
5151
const { events } = await engine.run(facts);
5252

53-
events.map((event) => console.log(event.params!.data.green));
53+
events.map((event) => console.log(`${event.params!.data}`.green));
5454
}
5555

5656
start();

examples/02-nested-boolean-logic.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async function start() {
7676

7777
const { events } = await engine.run(facts);
7878

79-
events.map((event) => console.log(event.params!.message.red));
79+
events.map((event) => console.log(`${event.params!.message}`.red));
8080
}
8181
start();
8282
/*

examples/09-rule-results.mts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
* DEBUG=json-rules-engine node ./examples/09-rule-results.js
99
*/
1010
import "colors";
11-
import { Engine, NestedCondition, RuleResult } from "json-rules-engine";
11+
import {
12+
Engine,
13+
NestedCondition,
14+
NestedConditionResult,
15+
RuleResult,
16+
} from "json-rules-engine";
1217

1318
async function start() {
1419
/**
@@ -48,14 +53,16 @@ async function start() {
4853
return console.log(`${message}`.green);
4954
}
5055
// if rule failed, iterate over each failed condition to determine why the student didn't qualify for athletics honor roll
51-
const detail = (ruleResult.conditions as { all: NestedCondition[] }).all
52-
.filter((condition) => !(condition as { result?: boolean }).result)
56+
const detail = (
57+
ruleResult.conditions as { all: NestedConditionResult[] }
58+
).all
59+
.filter(({ result }) => !result)
5360
.map((condition) => {
54-
switch ((condition as { operator?: string }).operator) {
61+
switch (condition.operator) {
5562
case "equal":
56-
return `was not an ${(condition as { fact?: string }).fact}`;
63+
return `was not an ${condition.fact}`;
5764
case "greaterThanInclusive":
58-
return `${(condition as { fact: string }).fact} of ${(condition as { factResult?: unknown }).factResult} was too low`;
65+
return `${condition.fact} of ${condition.factResult} was too low`;
5966
default:
6067
return "";
6168
}

test/types.test-d.mts

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import rulesEngine, {
1414
Rule,
1515
RuleProperties,
1616
RuleResult,
17-
RuleSerializable,
18-
} from "../types/index.js";
17+
} from "../src/index.mjs";
1918

2019
// setup basic fixture data
2120
const ruleProps: RuleProperties = {
@@ -81,27 +80,6 @@ describe("type tests", () => {
8180
it("returns void when updating a rule", () => {
8281
expectTypeOf<void>(engine.updateRule(ruleFromString));
8382
});
84-
85-
it("returns rule when setting conditions", () => {
86-
expectTypeOf<Rule>(rule.setConditions({ any: [] }));
87-
});
88-
89-
it("returns rule when setting event", () => {
90-
expectTypeOf<Rule>(rule.setEvent({ type: "test" }));
91-
});
92-
93-
it("returns rule when setting priority", () => {
94-
expectTypeOf<Rule>(rule.setPriority(1));
95-
});
96-
97-
it("returns string when json stringifying", () => {
98-
expectTypeOf<string>(rule.toJSON());
99-
expectTypeOf<string>(rule.toJSON(true));
100-
});
101-
102-
it("returns serializable props when converting to json", () => {
103-
expectTypeOf<RuleSerializable>(rule.toJSON(false));
104-
});
10583
});
10684

10785
describe("operator tests", () => {
@@ -110,10 +88,10 @@ describe("type tests", () => {
11088
b: number,
11189
) => a === b;
11290

113-
const operator: Operator = new Operator(
91+
const operator: Operator<number, number> = new Operator(
11492
"test",
11593
operatorEvaluator,
116-
(num: number) => num > 0,
94+
(num: unknown): num is number => Number(num) > 0,
11795
);
11896

11997
it("returns void when adding an operatorEvaluator", () => {
@@ -137,10 +115,10 @@ describe("type tests", () => {
137115
number
138116
> = (a: number[], b: number, next: OperatorEvaluator<number, number>) =>
139117
next(a[0], b);
140-
const operatorDecorator: OperatorDecorator = new OperatorDecorator(
118+
const operatorDecorator: OperatorDecorator<number[], number, number, number> = new OperatorDecorator(
141119
"first",
142120
operatorDecoratorEvaluator,
143-
(a: number[]) => a.length > 0,
121+
(a: unknown): a is number[] => Array.isArray(a) && a.length > 0,
144122
);
145123

146124
it("returns void when adding a decorator evaluator", () => {
@@ -181,7 +159,7 @@ describe("type tests", () => {
181159
});
182160

183161
it("returns fact when getting a fact", () => {
184-
expectTypeOf<Fact<string>>(engine.getFact<string>("test"));
162+
expectTypeOf<Fact | undefined>(engine.getFact("test"));
185163
});
186164
});
187165

@@ -191,10 +169,6 @@ describe("type tests", () => {
191169
it("factValue returns promise of value", () => {
192170
expectTypeOf<Promise<string>>(almanac.factValue<string>("test-fact"));
193171
});
194-
195-
it("addRuntimeFact returns void", () => {
196-
expectTypeOf<void>(almanac.addRuntimeFact("test-fact", "some-value"));
197-
});
198172
});
199173

200174
describe("event tests", () => {

0 commit comments

Comments
 (0)