How can I use Mocha without removing Ava? - javascript

One of my co-workers added this Ava package to our setup, and it's done something I've never seen a Node package do before: interfere with other packages! Now when I try to run Mocha I get:
$ node_modules/mocha/bin/mocha test/
Test files must be run with the AVA CLI:
$ ava node_modules/mocha/bin/_mocha
I get that Ava would like to run my Mocha tests, but if I wanted that I'd run ava mocha not mocha. And because a co-worker is using it I can't simply uninstall the package.
Is there any way I can run plain Mocha tests on a machine with Ava installed?

One of the files in test/ imports ava and the imported code will recognise that it's not being run with the correct tooling and throw an error.
Might be worth subdividing your test/ directory to keep tests associated with their respective runners.
test/
ava/
SomeAvaTests.js
mocha/
SomeMochaTests.js
This way you can safely run mocha test/mocha/ and vice versa without worrying about treading on each other's toes.

Related

Different jest results when running via npm/package.json

Can anyone help me to understand that behaviour:
when I ran in terminal jest command: all my tests passed
I have also defined package.json script where I do exactly the same command jest and when i run npm run tests jest command is executed and all my tests passed.
BUT:
I run it on dockerimage. I have copy all project files to Docker, install all depend. and run tests.
npm run tests - failed
jest - passed
I am on the build docker image, in terminal run both commands. I see that they are giving different results. I know that it different OS. maybe different libraries, but npm, node and jest versions are identical.
Looks like running npm run tests behave differently. I'm stuck, don't know what to check, and how to debug it. Where the problem might be?

Run and add unit tests to nodejs project

I want to run the tests related to this project on github, see folder test on https://github.com/node-opcua/node-opcua . Unfortunately I have no idea which testing framework was used and how to run all the tests?
First, run npm install
This will install the required dependencies.
You can then use
npm test
See here:
https://github.com/node-opcua/node-opcua/blob/master/package.json#L15
That will run the tests using Mocha.

How to combine mocha tests with mocha-phantomjs tests

Test cases:
server/
appTest.js
client/
clientTest.js
clientBundle.js
clientTest.html
All are mocha tests. For client tests, I use sinon, and webpack them to generate a bundle - clientBundle.js and run it using mocha-phantomjs for headless testing.
Now I have defined npm test as mocha test/server && mocha-phantomjs test/clientTest.html. It gives me results for both of them separately. How can I combine them into one - say mocha test/all.js.
Tried require()s inside test/all.js. mocha-phantomjs cannot be used as a library inside node. It can only be used as a CLI.

How to create a custom reporter with Mocha

I am sure I am missing something obvious here!
I have read the instructions here (https://github.com/visionmedia/mocha/wiki/Third-party-reporters), and have taken their code and added as a new node module (i.e. it is within node_modules/my-reporter/reporter.js). However, I can't seem to get mocha to load this reporter.
I have tried numerous variations …
mocha allTests.js -R ./node_modules/my-reporter/reporter.js
mocha allTests.js -R my-reporter
But nothing works :-(
I can successfully load my reporter within a JS file:
my_reporter = require('./node_modules/my-reporter/reporter.js')
console.log(my_reporter);
Has anyone got any hints?
u should provide the reporter like this:
mocha allTests.js -R './node_modules/my-reporter/reporter'
You should not provide the .js file extension as that is the normal convention for including modules.
It appears that if mocha is installed globally (which I believe it almost always is), you have to install your reporter the same way.
If you don't want to publish the reporter as a public module, you can just:
npm pack
npm install /path/to/your/module.gz -g
I've tried putting the reporter everywhere else that would make sense, but was getting "invalid reporter" unless it was installed globally.

mocha run tests twice

I have a several tests for node js express application written in coffeescript run under Mocha control.
Unfortunately mocha runs all my tests twice, becouse in the same directory I jave .coffee and .js files. The .js files are generated by my editor automatically together with .map files. That's quite handy if I need to debug something.
How can I filter that only .coffee or .js are executed from directory not both of them?
In your package.json, create entries like the following:
"scripts": {
"test": "npm run test-coffee",
"test-js": "mocha -R spec test/*.js",
"test-coffee": "mocha -R spec test/*.coffee"
}
Now you can run npm test, npm run test-js, or npm run test-coffee. If you have tests in subdirectories, you will want to use the find command to run just the subtests you're interested in.
Mocha by default runs files with pattern: test/*.{js,coffee} which includes both coffee and js files. In your case, it will find both coffee and js files of each test, thus your tests are run twice.
You may want to set the file pattern explicitly, something like:
mocha -R spec "test/*.js"
If you have tests in subdirectories, you can use globstar (**) in your pattern like this:
mocha -R spec "test/**/*.js"
Note the use of double quotes. If you omit double quotes they will be interpreted by shell and it may not be able to match files in subdirectories. Using double quotes passes the pattern to mocha, and mocha handles it correctly.

Categories

Resources