Question

So, armed with this tutorial, I decided to add some unit testing to my node.js app. My gruntfile seems to be OK, and when I type grunt nodeunit my only test runs just fine, but after that, it crashes with error Fatal error: Cannot find module 'tap':

$> grunt nodeunit
Running "nodeunit:all" (nodeunit) task
Testing db.test.js
.OK
Fatal error: Cannot find module 'tap'

I didn't know anything about this module, but after googliing it up, it seemed that it's something nodeunit would require. And indeed, there it is: $MY_NODE_APP/node_modules/nodeunit/node_modules/tap exists, and when I launch node in $MY_NODE_APP/node_modules/nodeunit and type require('tap') in interactive console, I get some object with a lot of stuff that gives me an impression that it works as it should.

So, the obvious question: why do I get this error, and how can I fix it?

Gruntfile:

module.exports = function(grunt) {

    grunt.initConfig({
        nodeunit: {
            all: ['./**/*.test.js']
        }
    });

    grunt.loadNpmTasks('grunt-contrib-nodeunit');
};

Update: Installed tap to my project, but it didn't help too.

Était-ce utile?

La solution 4

I solved the issue (well, this issue — it wasn't the last one) by installing the tap module manually.

Autres conseils

I landed on this question and have since resolved my own issue. I'll post my resolution as it might be helpful to someone else:

First the similarities:

I was just getting my Gulp scripts setup, and I got this error when running my tests:

Fatal error: Cannot find module 'tap'

Which didn't make sense because I wasn't using that library!

How I resolved:

Turns out my paths were all kind of screwed up in my gulpfile.js. Therefore I was trying to unit test all of the modules in my node_modules folder!

Here's the new path:

 paths: {
      mocha: [
        '**/*.test.js',
        '!node_modules/**/*.js'
      ]
 }

The issue for me was that I was inadvertently running the tests within the node_modules directory. My npm test was mocha **/*.test.js and when I changed it to mocha src/**/*.test.js all of a sudden the message went away. Makes sense really.

I have solved the problem by adding another '*' right after "mocha" it is related to the folder's path so it may also fix the problem in other cases.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top