I have 2 test files in ./test
.
Let's say test1.js
is a Mocha test that spins up a server and runs various requests against URIs to determine that they all behave as expected.
test2.js
is a Mocha test that uses Karma to load specific scripts (including angular-mocks.js
) and run in a few browsers to ensure all functionality there behaves as expected.
In package.json
I have the test
property configured as such:
"scripts": {
"test": "./node_modules/.bin/mocha --reporter spec -t 5000"
},
Cool, I can npm test
from the command line to run Mocha. But oh noes – Mocha runs both of my scripts, and test2.js
of course crashes the whole thing since the logic therein assumes it's running within the context of Karma (excuse me if I'm not describing this properly).
I could have 2 test folders, test
and test-ng
or something, but I think ultimately I want to be able to npm test
and have different sets of tests run, i.e.:
"./node_modules/.bin/mocha --reporter spec -t 5000"
"./node_modules/.bin/karma start"
and configure them to each run the correct js files. I've been searching like mad to find an example where client-side and server-side tests exist in the same repo, but I'm only finding tutorials and blog posts and the like for demonstrating one or the other. Can someone help me along in the right direction?
Edit: Should I be thinking about / organizing my tests like this?
./tests/server/**.js
./tests/e2e/**.js
./tests/unit/**.js
I was perhaps making assumptions based on the fact that npm by default uses the ./test/
folder.
Edit 2: I'm now doing what I described above, more or less, and have my tests described in the repo's readme like:
Angular unit tests can be run via Karma:
./node_modules/.bin/karma start karma.conf.js
Angular end-to-end tests can be run via Protractor:
./node_modules/.bin/protractor protractor.conf.js
Express unit tests can be run via Mocha:
./node_modules/.bin/mocha ...
So now I'm not using npm test
at all, and I'm wondering what advantages there are to using that.
You can put something like this in your package.json
:
"scripts": {
"unittest": "mocha --reporter spec tests/test1.js",
"browsertest": "mocha --reporter spec -t 5000 tests/test2.js",
"test": "npm run unittest && npm run browsertest"
}
and have Mocha (or Karma, etc.) have a script each, which can be run individually by eg.
$ npm run browsertest
There are several benefits to putting all parts of the build system directly into the scripts in package.json:
./node_modules/.bin/mocha
, because
npm already knows about ./node_modules/.bin
.start
, stop
, test
, etc.), which in turn makes it easier to collaborate with others,
people as well as frameworks (like Phusion Passenger).And you can use npm run
to get a list of your scripts.
For a more thorough description of the merits, see Keith Cirkel's great blog post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With