Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example testing #399

Open
wants to merge 3 commits into
base: v8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/01-hello-world.mts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ async function start() {
events.map((event) => console.log(event.params!.data.green));
}

start();
export default start();

/*
* OUTPUT:
*
Expand Down
2 changes: 1 addition & 1 deletion examples/02-nested-boolean-logic.mts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async function start() {

events.map((event) => console.log(event.params!.message.red));
}
start();
export default start();
/*
* OUTPUT:
*
Expand Down
2 changes: 1 addition & 1 deletion examples/03-dynamic-facts.mts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async function start() {
facts.accountId + " is a " + events.map((event) => event.params!.message),
);
}
start();
export default start();

/*
* OUTPUT:
Expand Down
2 changes: 1 addition & 1 deletion examples/04-fact-dependency.mts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ async function start() {
* for washington and jefferson after the initial data load.
*/
}
start();
export default start();

/*
* OUTPUT:
Expand Down
2 changes: 1 addition & 1 deletion examples/05-optimizing-runtime-with-fact-priorities.mts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async function start() {
// define fact(s) known at runtime
await engine.run();
}
start();
export default start();

/*
* OUTPUT:
Expand Down
2 changes: 1 addition & 1 deletion examples/06-custom-operators.mts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ async function start() {
facts = { word: "antelope" };
await engine.run(facts);
}
start();
export default start();

/*
* OUTPUT:
Expand Down
2 changes: 1 addition & 1 deletion examples/07-rule-chaining.mts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ async function start() {
);
}

start();
export default start();

/*
* OUTPUT:
Expand Down
2 changes: 1 addition & 1 deletion examples/08-fact-comparison.mts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ async function start() {
facts = Object.assign({}, userFacts, productList);
await engine.run(facts);
}
start();
export default start();
/*
* OUTPUT:
*
Expand Down
2 changes: 1 addition & 1 deletion examples/09-rule-results.mts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async function start() {
engine.run({ athlete: true, GPA: 1.1, username: "sarah" }),
]);
}
start();
export default start();
/*
* OUTPUT:
*
Expand Down
2 changes: 1 addition & 1 deletion examples/10-condition-sharing.mts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async function start() {
await engine.run(facts);
}

start();
export default start();

/*
* OUTPUT:
Expand Down
2 changes: 1 addition & 1 deletion examples/12-using-custom-almanac.mts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async function start() {
);
}

start();
export default start();

/*
* OUTPUT:
Expand Down
2 changes: 1 addition & 1 deletion examples/13-using-operator-decorators.mts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async function start() {
facts = { tags: ["dev", "PROD"] };
await engine.run(facts);
}
start();
export default start();

/*
* OUTPUT:
Expand Down
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "json-rules-engine-examples",
"version": "1.0.0",
"description": "examples for json-rule-engine",
"main": "",
"type": "module",
"private": true,
"scripts": {
"all": "for i in *.mts; do tsx $i; done;"
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
"name": "json-rules-engine",
"version": "8.0.0-alpha.1",
"description": "Rules Engine expressed in simple json",
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "types/index.d.ts",
"type": "module",
"exports": {
".": {
"import": "./dist/index.js"
}
},
"engines": {
"node": ">=18.0.0"
},
Expand Down
230 changes: 230 additions & 0 deletions test/__snapshots__/examples.test.mts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`examples > example 01-hello-world.mts > expected consistent console errors 1`] = `[]`;

exports[`examples > example 01-hello-world.mts > expected consistent console logs 1`] = `
[
[
"hello-world!",
],
]
`;

exports[`examples > example 02-nested-boolean-logic.mts > expected consistent console errors 1`] = `[]`;

exports[`examples > example 02-nested-boolean-logic.mts > expected consistent console logs 1`] = `
[
[
"Player has fouled out!",
],
]
`;

exports[`examples > example 03-dynamic-facts.mts > expected consistent console errors 1`] = `[]`;

exports[`examples > example 03-dynamic-facts.mts > expected consistent console logs 1`] = `
[
[
"loading account information for "lincoln"",
],
[
"lincoln is a current microsoft employee taking christmas day off",
],
]
`;

exports[`examples > example 04-fact-dependency.mts > expected consistent console errors 1`] = `[]`;

exports[`examples > example 04-fact-dependency.mts > expected consistent console logs 1`] = `
[
[
"-- FIRST RUN --",
],
[
"loading account information for "washington"",
],
[
"washington DID meet conditions for the five-year-tenure rule.",
],
[
"washington DID meet conditions for the microsoft-terminated-employees rule.",
],
[
"-- SECOND RUN --",
],
[
"loading account information for "jefferson"",
],
[
"jefferson DID meet conditions for the five-year-tenure rule.",
],
[
"jefferson did NOT meet conditions for the microsoft-terminated-employees rule.",
],
]
`;

exports[`examples > example 05-optimizing-runtime-with-fact-priorities.mts > expected consistent console errors 1`] = `[]`;

exports[`examples > example 05-optimizing-runtime-with-fact-priorities.mts > expected consistent console logs 1`] = `
[
[
"Checking the "date" fact...",
],
[
"washington did NOT meet conditions for the microsoft-employees rule.",
],
]
`;

exports[`examples > example 06-custom-operators.mts > expected consistent console errors 1`] = `[]`;

exports[`examples > example 06-custom-operators.mts > expected consistent console logs 1`] = `
[
[
"bacon did NOT start with "a"",
],
[
"bacon DID start with "b"",
],
[
"antelope DID start with "a"",
],
[
"antelope did NOT start with "b"",
],
]
`;

exports[`examples > example 07-rule-chaining.mts > expected consistent console errors 1`] = `[]`;

exports[`examples > example 07-rule-chaining.mts > expected consistent console logs 1`] = `
[
[
"loading account information for "washington"",
],
[
"washington(microsoft) DID meet conditions for the drinks-screwdrivers rule.",
],
[
"washington(microsoft) DID meet conditions for the invite-to-screwdriver-social rule.",
],
[
"washington IS a screwdriver aficionado",
],
[
"jefferson did NOT meet conditions for the drinks-screwdrivers rule.",
],
[
"jefferson did NOT meet conditions for the invite-to-screwdriver-social rule.",
],
[
"jefferson IS NOT a screwdriver aficionado",
],
]
`;

exports[`examples > example 08-fact-comparison.mts > expected consistent console errors 1`] = `[]`;

exports[`examples > example 08-fact-comparison.mts > expected consistent console logs 1`] = `
[
[
"washington DID meet conditions for the customer-can-afford-gift-card rule.",
],
[
"jefferson did NOT meet conditions for the customer-can-afford-gift-card rule.",
],
]
`;

exports[`examples > example 09-rule-results.mts > expected consistent console errors 1`] = `[]`;

exports[`examples > example 09-rule-results.mts > expected consistent console logs 1`] = `
[
[
"joe failed Athlete GPA Rule - was not an athlete",
],
[
"larry succeeded Athlete GPA Rule! Student made the athletics honor-roll",
],
[
"jane failed Athlete GPA Rule - was not an athlete and GPA of 3.1 was too low",
],
[
"janet succeeded Athlete GPA Rule! Student made the athletics honor-roll",
],
[
"sarah failed Athlete GPA Rule - GPA of 1.1 was too low",
],
]
`;

exports[`examples > example 10-condition-sharing.mts > expected consistent console errors 1`] = `[]`;

exports[`examples > example 10-condition-sharing.mts > expected consistent console logs 1`] = `
[
[
"washingtonDID meet conditions for the invite-to-screwdriver-social rule.",
],
[
"washington did NOT meet conditions for the invite-to-other-social rule.",
],
[
"jefferson did NOT meet conditions for the invite-to-screwdriver-social rule.",
],
[
"jeffersonDID meet conditions for the invite-to-other-social rule.",
],
]
`;

exports[`examples > example 11-using-facts-in-events.mts > expected consistent console errors 1`] = `[]`;

exports[`examples > example 11-using-facts-in-events.mts > expected consistent console logs 1`] = `[]`;

exports[`examples > example 12-using-custom-almanac.mts > expected consistent console errors 1`] = `[]`;

exports[`examples > example 12-using-custom-almanac.mts > expected consistent console logs 1`] = `
[
[
"HIGH SCORE
DOG - 968",
],
[
"
",
],
[
"HIGH SCORE
AAA - 1000",
],
[
"Bob is 19 years old and is not Over 21(ish)",
],
[
"Alice is 21 years old and is Over 21(ish)",
],
[
"Chad is 20 years old and is Over 21(ish)",
],
]
`;

exports[`examples > example 13-using-operator-decorators.mts > expected consistent console errors 1`] = `[]`;

exports[`examples > example 13-using-operator-decorators.mts > expected consistent console logs 1`] = `
[
[
"dev, prod WERE all valid tags",
],
[
"dev, deleted WERE NOT all valid tags",
],
[
"dev, PROD WERE NOT all valid tags",
],
[
"dev, PROD WERE all valid tags (case insensitive)",
],
]
`;
23 changes: 23 additions & 0 deletions test/examples.test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { beforeAll, beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { readdirSync } from 'node:fs';
import { resolve } from 'node:path';
import rulesEngineDefault, * as ruleEngine from '../src/index.mjs';

const exampleDir = resolve(__dirname, '../examples');

describe('examples', () => {
beforeAll(() => {
vi.mock('json-rules-engine', () => ({...ruleEngine, default: rulesEngineDefault }));
});

beforeEach(() => {
vi.spyOn(console, 'log');
vi.spyOn(console, 'error');
})

it.each(readdirSync(exampleDir).filter(fileName => fileName.endsWith(".mts")))('example %s', async (filename) => {
await (await import(resolve(exampleDir, filename))).default;
expect.soft((console.log as Mock).mock.calls).toMatchSnapshot("expected consistent console logs");
expect.soft((console.error as Mock).mock.calls).toMatchSnapshot("expected consistent console errors");
})
})
5 changes: 2 additions & 3 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { defineConfig } from "tsup";
export default defineConfig({
entry: ["src/index.mjs"],
sourcemap: true,
format: ["esm", "cjs"],
target: ["es2015"],
cjsInterop: true,
format: ["esm"],
target: ["node18"]
});
Loading