diff --git a/src/context.js b/src/context.js index ce0145d..36d3c8d 100644 --- a/src/context.js +++ b/src/context.js @@ -1,28 +1,15 @@ const Metadata = require('./metadata'); module.exports = class Context { - constructor({ debugMode = false } = {}) { + constructor() { this._bag = {}; this._bag.assertPresent = this._makeAssertPresent(this._bag).bind(this); this._metadata = new Metadata(); - this._missings = {}; - this._debugMode = debugMode; - } - - _addMissing(path, keys) { - this._missings[path] = keys; - } - - _hasMissing() { - return Object.keys(this._missings).length > 0; } seal() { this.flushPrivates(''); this._metadata.seal(); - if (this._debugMode && this._hasMissing()) { - throw new Error(`Missing dependencies: ${JSON.stringify(this._missings)}`); - } } get() { return this._bag; } @@ -37,10 +24,12 @@ module.exports = class Context { const keys = Object.keys(requisites); const missingKeys = keys .filter((key) => !Object.prototype.hasOwnProperty.call(bag, key)); - if (missingKeys.length > 0) { - if (this._debugMode) this._addMissing(this._metadata.getCurrentPath(), missingKeys); - else throw new Error(`Asserting dependencies on path "${this._metadata.getCurrentPath()}": Missing dependencies: "${missingKeys}"`); - } + if (missingKeys.length > 0) throw new Error(`Asserting dependencies on path "${this._metadata.getCurrentPath()}": Missing dependencies: "${missingKeys}"`); + + const undefinedKeys = keys + .filter((key) => bag[key] === undefined); + + if (undefinedKeys.length > 0) throw new Error(`Asserting dependencies on path "${this._metadata.getCurrentPath()}": Undefined dependencies: "${undefinedKeys}"`); this._metadata.setRequisites(keys); return true; diff --git a/src/index.d.ts b/src/index.d.ts index 7c3aeb8..544c844 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -37,9 +37,7 @@ export interface PlanEntry { options?: EntryOptions; } -declare class Context { - constructor({ debugMode }: { debugMode: boolean }); -} +declare class Context {} declare class Plan { constructor(entries: PlanEntry[], verbose?: boolean); diff --git a/src/plan.js b/src/plan.js index d424010..4b26d9b 100644 --- a/src/plan.js +++ b/src/plan.js @@ -5,7 +5,7 @@ const Context = require('./context'); const METADATA_HOOK = 'metadata-hook'; -class Plan { +module.exports = class Plan { constructor(_entries = [], verbose = false) { this._entries = _entries; this._stepsWalk = []; @@ -400,7 +400,4 @@ class Plan { this._addEntry(Symbol(METADATA_HOOK), METADATA_HOOK, hook); return this; } -} - -module.exports = Plan; -module.exports.Context = Context; +}; diff --git a/test/integration/plan.test.js b/test/integration/plan.test.js index 72d8a8a..189a932 100644 --- a/test/integration/plan.test.js +++ b/test/integration/plan.test.js @@ -1143,18 +1143,4 @@ describe('Plan', () => { expect(entries).toStrictEqual(expectedEntries); }); }); - - it('override assertPresent to list dependencies', () => { - const factory = ({ assertPresent, phantom }) => { - assertPresent({ phantom }); - return 'toto'; - }; - const plan = (p) => p - .addUsingFunction('miss1', factory) - .addUsingFunction('miss2', factory) - .addValue('phantom', 'phantom') - .addUsingFunction('ok', factory); - - expect(() => execute(plan, new Context({ debugMode: true }))).toThrow('Missing dependencies: {"/miss1":["phantom"],"/miss2":["phantom"]}'); - }); });