node.jshapi.jshapi.js-lab

Hapi.js lab test returns error no method 'experiment'


I am writing the test code for a function that will check for the existence of a username in twitter. I am using the Hapi framework and Lab for testing.

When I run npm test I get the following error:

> NameGen@0.0.0 test /Users/mario/projects/NameGen
> ./node_modules/lab/bin/lab -c


/Users/mario/projects/NameGen/test/test.js:5
Lab.experiment( "Test Username Existence", function() {
    ^
TypeError: Object #<Object> has no method 'experiment'
    at Object.<anonymous> (/Users/mario/projects/NameGen/test/test.js:5:5)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js [as origLoader] (module.js:474:10)
    at Object.require.extensions..js (/Users/mario/projects/NameGen/node_modules/lab/lib/coverage.js:32:26)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at /Users/mario/projects/NameGen/node_modules/lab/lib/cli.js:85:23
    at Array.forEach (native)
    at Object.internals.traverse (/Users/mario/projects/NameGen/node_modules/lab/lib/cli.js:81:19)
    at Object.exports.run (/Users/mario/projects/NameGen/node_modules/lab/lib/cli.js:30:29)
    at Object.<anonymous> (/Users/mario/projects/NameGen/node_modules/lab/bin/lab:3:23)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

My test code is:

var Lab = require("lab");
var server = require('../'); // require index.js
var request = require('request');

Lab.experiment( "Test Username Existence", function() {
  // tests

  Lab.test( "Test username exists", function(done){
    var options = {
      url: 'http://twitter.com/BarackObama'
    };

    request(options, function(error, response, body){
      Lab.expect(response.statusCode).to.equal(200);
    });
      done();
    });

    Lab.test("Test username does not exists", function(done){
      var options = {
        url: 'http://twitter.com/jhkhksdhkjahsdfkjhasdf'
      };

      request(options, function(error, response, body){
        Lab.expect(response.statusCode).to.equal(404);
      });
        done();
      });

  });

I have the latest version of node installed and all the necessary dependencies in my package.json file. What could be causing this error?


Solution

  • Per the Lab docs:

    Test files must require the lab module, and export a test script

    test.js

    var Lab = require('lab');
    var lab = module.exports = Lab.script();
    
    lab.experiment( "Test Username Existence", function() {
      lab.test( "Test username exists", function(done){
    
       });
    });