diff --git a/index.js b/index.js index b7a85d9..67f131f 100644 --- a/index.js +++ b/index.js @@ -29,6 +29,10 @@ function toposort(nodes, edges) { throw new Error('Cyclic dependency: '+JSON.stringify(node)) } + if (!~nodes.indexOf(node)) { + throw new Error('Found unknown node. Make sure to provided all involved nodes. Unknown node: '+JSON.stringify(node)) + } + if (visited[i]) return; visited[i] = true diff --git a/test.js b/test.js index 56a27e3..10a58af 100644 --- a/test.js +++ b/test.js @@ -80,6 +80,20 @@ suite.addBatch( assert.instanceOf(val, Error) } } +, 'unknown nodes in edges': + { topic: function() { + return toposort.array(['bla'] + [ ["foo", 'bar'] + , ["bar", "ron"] + , ["john", "bar"] + , ["tom", "john"] + , ["ron", "tom"] + ]) + } + , 'should throw an exception': function(_, val) { + assert.instanceOf(val, Error) + } + } , 'triangular dependency': { topic: function() { /* @@ -121,4 +135,4 @@ suite.addBatch( }) .run(null, function() { (suite.results.broken+suite.results.errored) > 0 && process.exit(1) -}) \ No newline at end of file +})