Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Mocha documentation #6

Closed
grofit opened this issue Jul 26, 2013 · 15 comments
Closed

Updated Mocha documentation #6

grofit opened this issue Jul 26, 2013 · 15 comments

Comments

@grofit
Copy link

grofit commented Jul 26, 2013

You mention that in the latest version there are breaking changes, but there is no documentation I can find around this. I know there are examples to look at in the repo, however they dont seem to work for me, I keep getting:

Error: TypeError: Cannot call method 'push' of undefined
    at Suite.<anonymous> (C:\Code\Archive\Guff\node\express\test\node_modules\yadda\lib\plugins\MochaPlugin.js:28:23)
(See full trace by running task with --trace)

When following the examples, so are the examples up to date and if not could the documentation reflect this? If however they are up to date examples any ideas whats causing the issue above?

The code being used is:

var yadda = require("yadda").Yadda;
require("yadda").plugins.mocha();

var functions = require("./functions/example-functions");
var yaddaContainer = new yadda(functions);
yaddaContainer.mocha("Bottle Tests", "./tests/acceptance/features/example-test.feature");

I assume its the require("yadda").plugins.mocha() but dont know specifically what will be causing it.

@cressie176
Copy link
Member

It looks like I forgot to update the release note with the mocha changes - sorry, however the example you pasted (and those in the examples folder) are correct. They work for me if I clone master and run

npm run examples

So my guess is there's a problem with your scenario or the library. Can you paste both please?

PS responses might be a little slow as I'm on holiday in a cottage with very unreliable wifi!

@grofit
Copy link
Author

grofit commented Jul 29, 2013

No worries, I have not been near the computer for a few days either. I have renamed some of the stuff, as I am just prototyping at the moment, but here is all the code required. It basically uses your example but a different directory structure, kicked off by a jake build script.

// <root-dir>/tests/acceptance/example-tests.js
var yadda = require("yadda").Yadda;
require("yadda").plugins.mocha();

var functions = require("./functions/example-functions");
var yaddaContainer = new yadda(functions);
yaddaContainer.mocha("Bottle Tests", "./tests/acceptance/features/example-feature.feature");
// <root-dir>/tests/acceptance/functions/example-functions.js
var Library = require('yadda').localisation.English;
var Dictionary = require('yadda').Dictionary;
var assert = require('assert');

module.exports = (function() {

    var wall;

    var dictionary = new Dictionary()
        .define('NUM', /(\d+)/);

    var library = new Library(dictionary)

    .given("$NUM green bottles are standing on the wall", function(number_of_bottles, next) {
      wall = new Wall(number_of_bottles);
        next();
    })

    .when("$NUM green bottle accidentally falls", function(number_of_falling_bottles, next) { 
        wall.fall(number_of_falling_bottles);
        next()
    })

    .then("there (?:are|are still) $NUM green bottles standing on the wall", function(number_of_bottles, next) {
        assert.equal(number_of_bottles, wall.bottles);
        next();
    })

    var Wall = function(bottles) {
        this.bottles = bottles;
        this.fall = function(n) {
            this.bottles -= n;
        }
        this.returned = function() {
            this.bottles++;
        }
    }

    return library;
})();
// <root-dir>/tests/acceptance/features/example-feature.feature
Scenario: should fall from the wall

    Given 100 green bottles are standing on the wall
    When 1 green bottle accidentally falls
    Then there are 99 green bottles standing on the wall 

Finally the jake task which kicks all this off:

desc('Run all acceptance tests.');
task('run_acceptance_tests', function () {
    console.log("  |- Running acceptance tests");

    var mochaRunner = new Mocha;

    var unitTestFiles = new jake.FileList();
    unitTestFiles.include(directories.tests + "acceptance/*-tests.js");
    unitTestFiles.toArray().forEach(function(file){
        mochaRunner.addFile(file)
    });

    mochaRunner.reporter("spec").run(); 
});

@cressie176
Copy link
Member

I created a new node 0.10.8 (and also 0.10.15) project based on the above example and pulled in the follow dependencies...

"dependencies": {
    "yadda": "~0.4.4",
    "mocha": "~1.12.0"
}

I've tried running mocha from the command line (I had to change the directory from tests to test) and also from a script

var Mocha = require('Mocha');

var mochaRunner = new Mocha;
mochaRunner.addFile('test/acceptance/example-tests.js')
mochaRunner.reporter("spec").run();

but not from jake. The test passed both times

  Bottle Tests
    ✓ should fall from the wall 

Maybe the problem is os related? I'm using Mountain Lion, but will try with Ubuntu too.

@cressie176
Copy link
Member

It's OK on ubuntu too

@grofit
Copy link
Author

grofit commented Aug 3, 2013

:( Im one of those windows users, I presume it should still all work ok. I will try cloning and just testing on yours directly on the system just to make sure it is nothing with my setup.

@cressie176
Copy link
Member

In that case maybe it's to do with line endings. The scenario parser (lib/parsers/TextParser) splits the lines in the scenario using /\n/, and maybe this should be /\r?\n/. I'll fire up a windows VM tomorrow and test it.

@cressie176
Copy link
Member

Just tried in a Windows 8 VM

  1. Installed Node
  2. From the command prompt
npm install yadda
cd yadda
npm install
node node_modules\.bin\mocha
// Tests pass
cd examples\mocha-async
node ..\..\node_modules\.bin\mocha test.js
// Tests pass

Would be interested to see what happens if you try the same

@grofit
Copy link
Author

grofit commented Aug 6, 2013

Right! after realising that your "node" above must be the node command line rather than the normal command line I ran the steps above, and if I run mocha directly:

C:\Code\Archive\Guff\node\yadda> node_modules\.bin\mocha

  .......................................

  39 passing (46 ms)

So mocha runs, then I went to try your example above, and got the following output:

C:\Code\Archive\Guff\node\yadda\examples\mocha-async> ..\..\node_modules\.bin\mocha test.js

C:\Code\Archive\Guff\node\yadda\lib\plugins\MochaPlugin.js:28
                throw new Error(e);
                      ^
Error: TypeError: Cannot call method 'push' of undefined
    at Suite.<anonymous> (C:\Code\Archive\Guff\node\yadda\lib\plugins\MochaPlugin.js:28:23)
    at context.describe.context.context (C:\Code\Archive\Guff\node\yadda\node_modules\mocha\lib\interfaces\bdd.js:72:10)
    at feature (C:\Code\Archive\Guff\node\yadda\lib\plugins\MochaPlugin.js:22:9)
    at Object.<anonymous> (C:\Code\Archive\Guff\node\yadda\examples\mocha-async\test.js:8:7)
    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 Module.require (module.js:364:17)
    at require (module.js:380:17)
    at C:\Code\Archive\Guff\node\yadda\node_modules\mocha\lib\mocha.js:152:27
    at Array.forEach (native)
    at Mocha.loadFiles (C:\Code\Archive\Guff\node\yadda\node_modules\mocha\lib\mocha.js:149:14)
    at Mocha.run (C:\Code\Archive\Guff\node\yadda\node_modules\mocha\lib\mocha.js:306:31)
    at Object.<anonymous> (C:\Code\Archive\Guff\node\yadda\node_modules\mocha\bin\_mocha:339:7)
    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:901:3

C:\Code\Archive\Guff\node\yadda\examples\mocha-async>

So get the same issue running against your test examples

@grofit
Copy link
Author

grofit commented Aug 6, 2013

even more info, right as I am working on the source code directly now, I have realised that you are expecting the files as UTF8. However the files come as ANSI, so whenever you try to load them it just blows up as it is not in the right format. If I use Notepad++ and convert the feature text file to UTF8 then it still blows up, so if I take out the UTF-8 argument and leave it encoding-less then it gets as far as this:

C:\Code\Archive\Guff\node\yadda\examples\mocha-async> ..\..\node_modules\.bin\mocha test.js

C:\Code\Archive\Guff\node\yadda\lib\plugins\MochaPlugin.js:28
                throw new Error(e);
                      ^
Error: TypeError: Object Scenario: A bottle falls from the wall

    Given 100 green bottles are standing on the wall
    when 1 green bottle accidentally falls
    then there are 99 green bottles standing on the wall

Scenario: No bottles are left

    Given 1 green bottles are standing on the wall
    when 1 green bottle accidentally falls
    then there are 0 green bottles standing on the wall  has no method 'split'
    at Suite.<anonymous> (C:\Code\Archive\Guff\node\yadda\lib\plugins\MochaPlugin.js:28:23)
    at context.describe.context.context (C:\Code\Archive\Guff\node\yadda\node_modules\mocha\lib\interfaces\bdd.js:72:10)
    at feature (C:\Code\Archive\Guff\node\yadda\lib\plugins\MochaPlugin.js:22:9)
    at Object.<anonymous> (C:\Code\Archive\Guff\node\yadda\examples\mocha-async\test.js:8:7)
    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 Module.require (module.js:364:17)
    at require (module.js:380:17)
    at C:\Code\Archive\Guff\node\yadda\node_modules\mocha\lib\mocha.js:152:27
    at Array.forEach (native)
    at Mocha.loadFiles (C:\Code\Archive\Guff\node\yadda\node_modules\mocha\lib\mocha.js:149:14)
    at Mocha.run (C:\Code\Archive\Guff\node\yadda\node_modules\mocha\lib\mocha.js:306:31)
    at Object.<anonymous> (C:\Code\Archive\Guff\node\yadda\node_modules\mocha\bin\_mocha:339:7)
    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:901:3

C:\Code\Archive\Guff\node\yadda\examples\mocha-async>

Weird thing is if I console.log(text); I see the file contents, but something in the parsing phase must blow up.

Using Node 0.10.13.

@grofit
Copy link
Author

grofit commented Aug 6, 2013

It seems like the regex isnt hitting the lines properly for some reason, I went and used rubular and that seemed fine with them, however if I sprinkle console.log statements throughout I get the following outputs:

Scenario: A bottle falls from the wall
{}
null
    given 100 green bottles are standing on the wall
{}
null
    when 1 green bottle accidentally falls
{}
null
    then there are 99 green bottles standing on the wall
{}
null
Scenario: No bottles are left
{}
null
    Given 1 green bottles are standing on the wall
{}
null
    when 1 green bottle accidentally falls
{}
null
    then there are 0 green bottles standing on the wall
{}
MATCHED: /^\s*([^\s].*)$/

I basically put the following in the parse_line method:

var parse_line = function(line) {
        var match;

        console.log(line);          
        console.log(current_scenario);  

        if (match = SCENARIO_REGEX.exec(line)) {
            console.log("MATCHED: " + SCENARIO_REGEX);
            current_scenario = { title: match[1], steps: [] };        
            scenarios.push(current_scenario);
        } else if (match = STEP_REGEX.exec(line)) {
            console.log("MATCHED: " + STEP_REGEX);
            current_scenario.steps.push(match[1]);
        }
        console.log(match);
    }

So the issue is because the scenarios and steps do not appear to get picked up, so when it goes to assign the steps, it is empty so the push will throw an exception. Like shown in the original errors.

So I would assume if the scenario matches (like it should) then the steps will already exist to be populated...

@grofit
Copy link
Author

grofit commented Aug 6, 2013

If I remove the $ symbol (end of line) from the end of the 2 regex expressions then it all works, and I get 2 passes, so I can only assume that there is some inconsistency between the line endings, although I assume the regex would just know what to do based on the OS.

I am not sure if this has any negative effects, as you are always receiving a single line to this method anyway, so it shouldn't matter...

@cressie176
Copy link
Member

I agree. I'll make the change and push a new release out. Thanks for your efforts!

@cressie176
Copy link
Member

0.4.5 pushed to npm. If you can confirm this fixes the problem I'll close the issue. Once again thanks.

@grofit
Copy link
Author

grofit commented Aug 8, 2013

Will go check it now, thanks for your help, im glad it was something simple.

@grofit
Copy link
Author

grofit commented Aug 8, 2013

Seems to have fixed it, thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants