javascript - Rookie error while writing test with Chai, Mocha, Express, johnny-five and node -
hi there i'm trying learn bit of test driven development using express, mocha, chai , johnny-five. wrote little application can turn led on , off. application works test fails. can tell me doing wrong in test?
thank you
the output of npm test
is
> blink@1.0.0 test /users/me/documents/johnny-five/blink > mocha --reporter spec 1435257445439 looking connected device j5 .on() 1) should turn led on .off() ✓ should turn led off 1 passing (13ms) 1 failing 1) j5 .on() should turn led on: assertionerror: expected undefined equal 1 @ context.<anonymous> (test/j5.js:9:14) npm err! test failed. see above more details.
this test/j5.js
require('mocha'); var assert = require('chai').assert; var j5 = require("../j5"); describe('j5', function () { describe('.on()', function () { it('should turn led on',function(){ var result = j5.on(); assert.equal(result, 1); }); }); describe('.off()', function () { it('should turn led off', function () { // var res = j5.on(); // expect(res).to.equal(0); }); }); });
this server.js
var express = require("express"); var app = express(); var j5 = require("./j5"); var port = 3000; app.get('/', function(req, res){ res.send('hello j5'); }); app.get("/on", function(req, res) { j5.on(); res.send("on"); }); app.get("/off", function(req, res) { j5.off(); res.send("off"); }); console.log("listening on port http://localhost:" + port); app.listen(3000);
this j5.js
var exports = module.exports = {}; var 5 = require("johnny-five"); var board = new five.board(); var board_ready = false; var led = null; board.on("ready", function() { board_ready = true; led = new five.led(13); }); exports.on = function() { if (led !== null && board_ready === true) { led.on(); return 1; } }; exports.off = function() { if (led !== null && board_ready === true) { led.off(); return 0; } };
edit: path j5.js in test/j5.js wrong. have new error. assertionerror: expected undefined equal 1 @ context. (test/j5.js:9:14).
after playing around found error.
johnny-five needs time connect board via serial. build in repl available can use functions on()
, off()
. made test wait 5 seconds before making call of j5.on()
. standard max timeout done()
function 2000ms. make longer used this.timeout(10000);
this new test/j5.js
require('mocha'); var assert = require('chai').assert; var j5 = require("../j5"); var result = null; describe('j5', function() { describe('.on()', function() { it('should turn led on', function(done) { this.timeout(10000); settimeout(function() { result = j5.on(); assert.equal(result, 1); done(); }, 5000); }); }); });
result of npm test
:
> blink@1.0.0 test /users/icke/documents/johnny-five/blink > mocha --reporter spec 1435305595110 device(s) /dev/cu.usbmodem1421 1435305595124 connected /dev/cu.usbmodem1421 j5 .on() 1435305598694 repl initialized ✓ should turn led on (5003ms) .off() ✓ should turn led off 2 passing (5s)
Comments
Post a Comment