I'm trying to use mocha for some Test Driven Development. Still at the beginning of this foray into Software Engineering, I'm having a problem including Graph.js in test.js:
project/Graph/Graph.js:
function Graph(){
this.nodes = {};
this.id = 0;
}
Graph.prototype.newId = function(){
return this.id++;
};
Graph.prototype.addNode = function(data){
var node = new Node(graph, this.newId());
node.data = data || {};
this.nodes[node.id] = node;
return edge.id;
};
Graph.prototype.removeNode = function(node){
for(var key in this.nodes){
for(var key2 in this.nodes[key].to){
if(this.nodes[key].edges.to[key2] == this){
delete this.nodes[key].edges.to[key2];
}
}
for(var key2 in this.nodes[key].from){
if(this.nodes[key].edges.from[key2] == this){
delete this.nodes[key].edges.from[key2];
}
}
}
delete this.nodes[node.id];
return 0;
};
Graph.prototype.addEdge = function(source, target){
id = this.newId();
source.edges.to[id] = target;
target.edges.from[id] = source;
return id;
};
Graph.prototype.removeEdge = function(edgeId){
delete this.edges[edgeId];
};
function Node(graph, id){
this.id = id;
this.graph = graph;
this.edges = {};
this.edges.from = {};
this.edges.to = {};
};
exports.Graph = Graph;
exports.Node = Node;
project/testing/test.js:
require('../Graph/Graph.js');
suite('Graph.js', function(){
setup(function(){
var graph = new Graph();
var n0 = graph.addNode({text: "Hello"}),
n1 = graph.addNode({text: "Sweet"}),
n2 = graph.addNode({text: "Worlds!"});
var e0 = graph.addEdge(n0, n1),
e1 = graph.addEdge(n1, n2);
});
test('traverse graph', function(){
var currentNode = n0;
var str = ''
while(Object.keys(currentNode.edges.to).length > 0){
str += currentNode.data.text + ' ';
currentNode = currentNode.edges.to[Object.keys(currentNode.edges.to)[0]];
}
assert.equals(str, 'Hello Sweet Worlds! ');
});
});
With the command
localhost:testing lowerkey$ mocha -u tdd -R nyan
I get the following result:
0 -__,------,
1 -__| /\_/\
0 -_~|_( o .o)
-_ "" ""
✖ 1 of 1 test failed:
1) Graph.js "before each" hook:
ReferenceError: Graph is not defined
at Context.<anonymous> (/Users/lowerkey/Desktop/TryThree/testing/test.js:5:19)
at Hook.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:200:32)
at next (/usr/local/lib/node_modules/mocha/lib/runner.js:201:10)
at Runner.hook (/usr/local/lib/node_modules/mocha/lib/runner.js:212:5)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Update With var graphjs = require(...);
and var graph = new graphjs.Graph();
I get graph (lowercase) not defined.