Mocha: Cannot find module 'tap', but tap isn't listed as a dependency

Created on 3 Nov 2017  路  15Comments  路  Source: mochajs/mocha

Description

On mocha 4.0.1 writing tests for backend node code. Upon running npm run test error is thrown saying 'tap' module can't be found. Upon checking dependencies, TAP isn't listed

question

Most helpful comment

It's something I run into as well. I think that 's because you run a test from your node_modules.
You can prevent running test from node_modules by doing mocha \"./{,!(node_modules)/**/}*.test.js\" (uf you want to run *.test.js files

All 15 comments

try this:

npm la tap

you can figure out which dep depends on tap with that command

Unless you didn't npm i though, it's more likely that something is requireing tap without it being in the dependency tree. But it is probably somewhere in the project code, so it should be a simple matter of text searching. And if it is your code calling tap and it is supposed to be there, then it's just going to be a matter of npm i --save-dev tap to actually install it (and record it in the devDependencies).

I also experienced this, and even though I couldn't find tap as a requirement anywhere, npm i --save-dev tap fixed the issue. Maybe mocha 5.0.1 needs to add it as a dependency?

It's something I run into as well. I think that 's because you run a test from your node_modules.
You can prevent running test from node_modules by doing mocha \"./{,!(node_modules)/**/}*.test.js\" (uf you want to run *.test.js files

@nchevobbe You're right, that was it - thanks!

thank you @nchevobbe! It made no sense until you came along :)

@nchevobbe thanks brother you saved a day :smile:

thanks Nicolas .. perfect solution

It worked for me thanks

thank you @nchevobbe

For anyone who finds this like I did...

TLDR: Overly aggressive find (And same issue as @nchevobbe mentioned)

My package.lock scripts.test looked like this

  • "NODE_ENV=test mocha --compilers js:babel-core/register $(find . \\( -name '*.spec.js' -or -name '*.test.js' \\) -print) $(find ./test/ -name '*.js' -print)"

which will find al *.spec.js and *.test.js files, including those inside node_modules, and those tests typically depend on dev dependencies that are not installed in packages installed in node_modules.

My solution was to filter out node_modules from my test command.

  • "NODE_ENV=test mocha --compilers js:babel-core/register $(find . \\( -name '*.spec.js' -or -name '*.test.js' \\) -print | grep -v node_modules) $(find ./test/ -name '*.js' -print | grep -v node_modules)"

@nchevobbe just like magic it works!

_All_ your tests (e.g., "*.spec.js" and "*.test.js") should be in your "test" directory (or some subdirectory within). The find commands should be wholly unnecessary if you organize them as such.

Assuming this, the cmdline for recent versions of Mocha would be:

# Using Babel 6...
$ NODE_ENV=test mocha --require babel-core/register 'test/**/*.js'

# Using Babel 7...
$ NODE_ENV=test mocha --require @babel/register 'test/**/*.js'

One downside of that strategy is that it鈥檚 harder to visually verify that all files are tested at a glance. Putting test files right next to the files under test makes that easier.

Meh, I don't find it difficult to have an editor window with two tabs showing both code and corresponding test in different directories. I usually organize mine like this:

| -- myproj
     |-- /lib
         {source code}
     |-- /test
         |-- /unit
             |-- /fixtures
         |-- /e2e
             |-- /fixtures

But it's a rather trivial cmdline change to incorporate your style...

$ NODE_ENV=test mocha --require @babel/register 'lib/**/*.spec.js' 'test/**/*.js'
Was this page helpful?
0 / 5 - 0 ratings

Related issues

adamhooper picture adamhooper  路  3Comments

seelikes picture seelikes  路  3Comments

Swivelgames picture Swivelgames  路  3Comments

EdvinOlofsson picture EdvinOlofsson  路  3Comments

eschwartz picture eschwartz  路  3Comments