diff --git a/examples/01-hello-world.mts b/examples/01-hello-world.mts index f0cc1f5..9e4d729 100644 --- a/examples/01-hello-world.mts +++ b/examples/01-hello-world.mts @@ -53,7 +53,8 @@ async function start() { events.map((event) => console.log(event.params!.data.green)); } -start(); +export default start(); + /* * OUTPUT: * diff --git a/examples/02-nested-boolean-logic.mts b/examples/02-nested-boolean-logic.mts index c596e9c..ee2bf97 100644 --- a/examples/02-nested-boolean-logic.mts +++ b/examples/02-nested-boolean-logic.mts @@ -78,7 +78,7 @@ async function start() { events.map((event) => console.log(event.params!.message.red)); } -start(); +export default start(); /* * OUTPUT: * diff --git a/examples/03-dynamic-facts.mts b/examples/03-dynamic-facts.mts index e241be0..cecda02 100644 --- a/examples/03-dynamic-facts.mts +++ b/examples/03-dynamic-facts.mts @@ -77,7 +77,7 @@ async function start() { facts.accountId + " is a " + events.map((event) => event.params!.message), ); } -start(); +export default start(); /* * OUTPUT: diff --git a/examples/04-fact-dependency.mts b/examples/04-fact-dependency.mts index 3315528..3749d0b 100644 --- a/examples/04-fact-dependency.mts +++ b/examples/04-fact-dependency.mts @@ -145,7 +145,7 @@ async function start() { * for washington and jefferson after the initial data load. */ } -start(); +export default start(); /* * OUTPUT: diff --git a/examples/05-optimizing-runtime-with-fact-priorities.mts b/examples/05-optimizing-runtime-with-fact-priorities.mts index 2075362..65ed073 100644 --- a/examples/05-optimizing-runtime-with-fact-priorities.mts +++ b/examples/05-optimizing-runtime-with-fact-priorities.mts @@ -105,7 +105,7 @@ async function start() { // define fact(s) known at runtime await engine.run(); } -start(); +export default start(); /* * OUTPUT: diff --git a/examples/06-custom-operators.mts b/examples/06-custom-operators.mts index e318201..cbceb11 100644 --- a/examples/06-custom-operators.mts +++ b/examples/06-custom-operators.mts @@ -101,7 +101,7 @@ async function start() { facts = { word: "antelope" }; await engine.run(facts); } -start(); +export default start(); /* * OUTPUT: diff --git a/examples/07-rule-chaining.mts b/examples/07-rule-chaining.mts index cece64d..7396753 100644 --- a/examples/07-rule-chaining.mts +++ b/examples/07-rule-chaining.mts @@ -147,7 +147,7 @@ async function start() { ); } -start(); +export default start(); /* * OUTPUT: diff --git a/examples/08-fact-comparison.mts b/examples/08-fact-comparison.mts index 7c38d42..ff12aaa 100644 --- a/examples/08-fact-comparison.mts +++ b/examples/08-fact-comparison.mts @@ -150,7 +150,7 @@ async function start() { facts = Object.assign({}, userFacts, productList); await engine.run(facts); } -start(); +export default start(); /* * OUTPUT: * diff --git a/examples/09-rule-results.mts b/examples/09-rule-results.mts index 48a5693..2d9ff5f 100644 --- a/examples/09-rule-results.mts +++ b/examples/09-rule-results.mts @@ -94,7 +94,7 @@ async function start() { engine.run({ athlete: true, GPA: 1.1, username: "sarah" }), ]); } -start(); +export default start(); /* * OUTPUT: * diff --git a/examples/10-condition-sharing.mts b/examples/10-condition-sharing.mts index 836bafe..c8c051f 100644 --- a/examples/10-condition-sharing.mts +++ b/examples/10-condition-sharing.mts @@ -126,7 +126,7 @@ async function start() { await engine.run(facts); } -start(); +export default start(); /* * OUTPUT: diff --git a/examples/12-using-custom-almanac.mts b/examples/12-using-custom-almanac.mts index b1743c0..ef9995e 100644 --- a/examples/12-using-custom-almanac.mts +++ b/examples/12-using-custom-almanac.mts @@ -93,7 +93,7 @@ async function start() { ); } -start(); +export default start(); /* * OUTPUT: diff --git a/examples/13-using-operator-decorators.mts b/examples/13-using-operator-decorators.mts index 60b34f6..fe80336 100644 --- a/examples/13-using-operator-decorators.mts +++ b/examples/13-using-operator-decorators.mts @@ -93,7 +93,7 @@ async function start() { facts = { tags: ["dev", "PROD"] }; await engine.run(facts); } -start(); +export default start(); /* * OUTPUT: diff --git a/examples/package.json b/examples/package.json index 231cd7d..cc84200 100644 --- a/examples/package.json +++ b/examples/package.json @@ -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;" diff --git a/package.json b/package.json index ed2a94b..19da40e 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test/__snapshots__/examples.test.mts.snap b/test/__snapshots__/examples.test.mts.snap new file mode 100644 index 0000000..9c2cca1 --- /dev/null +++ b/test/__snapshots__/examples.test.mts.snap @@ -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)", + ], +] +`; diff --git a/test/examples.test.mts b/test/examples.test.mts new file mode 100644 index 0000000..854fde4 --- /dev/null +++ b/test/examples.test.mts @@ -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"); + }) +}) \ No newline at end of file diff --git a/tsup.config.ts b/tsup.config.ts index 4f77f3c..adf0100 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -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"] });