How to run npm tests from a single folder? - node.js

Currently I am testing a React node application, and have all my tests in the root/tests folder, with components in separate folders pertaining to their function.
I have 10+ folders and 100+ tests, and would like to 'watch' a single folder while I write a test for a new component.
Currently, I am using
npm run test:watch
Which is working brilliantly, however, not only is there a lot of overhead re-running the 100 other tests not related to my new component, it's also hard to weed through all the feedback to see the results of my current test.
Is there a nice command to only watch the directory of my new test, or even the test file?

You can pass mocha test directory to the npm command:
npm run test:watch -- root/tests/subfolder
However it might depend on how your test script test:watch is defined.

Related

How to prevent testing-libarary running coverage on untested files every time I save my test file?

I am using the test-library for React , and everytime I edit my test file and save it, it will automatically running coverage on other untested files and gave me a report. there are hundreds of files in my project,and it could really waste time. I don't want this, I just want it run test on my current selected file.
the command I have used is
npm run test /myprojrct/myFile.test.js
is there a way to resolve it ?
Adding the --watch flag to your test script should make jest run tests only on changed files after each save.
More details and other available options can be found here: https://jestjs.io/docs/en/cli#--watch
You should add --coverage=false flag to prevent collecting coverage.
npm run test --coverage=false
Other useful flags:
npm run test --watch // watcher will re-run only the tests that depend on the changed files
npm run test --watchAll // watcher will re-run *ALL* tests when a file has changed
npm run test -t="rendering Button" // will run only tests with a name that matches the regex
npm run test fileName.js // this argument will be treated as a regular expression to match against files in your project

Run lab test for all test subfolders

I'm refactoring one backend at my company and I'm modularizing it as much as possible. My structure can be defined as a monorepo composed of several local submodules and one or two main modules that requires them(the submodules). Let's illustrate this:
root
main
package.json
stuff that uses submoduleA
stuff that uses submoduleB
test
modules
submoduleA
package.json
code
test
submoduleB
package.json
test
As you can see, each module has it's own tests, package.json, and dependencies. Everything works flawlessly within them (running npm test inside the folder of a module, for example). I'm using lab and code for testing.
What I want is to be able to do is going to the root of the project, run npm test and run all the tests for all the submodules and the main module. Basically run all the test under any test folder in one go.
The problems that I found so far are:
- Currently only the files under the target test directory are being run. I don't even know how to run all the test at once. My command looks like lab src/main/test --reporter console --threshold 100 --assert code
What I'm doing now is putting all the test under the same test directory and make relative requirements for everything. This is very inconvenient because makes test fragile with all those ../../../some/away/path requires and that stuff.
Any advice for keeping the code modular, and being able to run all the test at once will be very welcome. Thank you very much.
I asked this a week ago on hapijs repository, so if you want to answer there too it would be fine: https://github.com/hapijs/discuss/issues/397
I would suggest making submoduleA and submoduleB stand alone node modules, then you can install and use with the main module when required. This way you are testing each part in isolation and then together which should yield the same outcome and will enable you to run tests more easier.

Temporary files on Travis

I have a Javascript project which uses Grunt for build process, QUnit for tests, Blanket for code coverage and a custom Grunt task to convert coverage results into LCOV files, sended to Coveralls. Everything running on TravisCI.
the project : https://github.com/mistic100/jQuery-QueryBuilder
my Grunt task : https://github.com/mistic100/grunt-qunit-blanket-lcov
So what should happen is that npm test runs QUnit+Blanket tests in a PhantomJS process and in the meanwhile, coverage results are saved in .coverage-results/all.lcov.
After a successfull build, grunt coveralls sends this file to Coveralls.
And my problem is here, the task does not find the file, although when I test on my computer it does.
see the last Travis log: https://travis-ci.org/mistic100/jQuery-QueryBuilder#L389
The only thing I can think about is that the file, for some reason, is deleted once npm test is finished. Is it possible ?
edit
so this has nothing to do with Travis but with my Grunt task where I use absolute paths thinking it's relative paths (I still don't know why it doesn't append on Windows though)
The only thing I can think about is that the file, for some reason, is deleted once npm test is finished. Is it possible ?
No, lifecycle-wise the build artefacts are still present, when running after_success commands.
The gruntfile.js configures force true and defines path - no issue here.
This should work.
I would suggest to throw in some commands to check the folders and files on Travis.
- sudo ls -alh /home/travis/build/mistic100/jQuery-QueryBuilder/*
- sudo ls -alh /home/travis/build/mistic100/jQuery-QueryBuilder/.coverage-results/*
Maybe you spot a permission issue during folder and file creation.
But that's my only guess.

nodejs unit testing framework that does not require an external test runner

All of the unit testing frameworks that I know of / can find require a test runner.
They all require you to globally install and run some program that runs your tests. Is there a well supported testing framework that can run as an npm require() ?
I need one like this because I want to be able to debug my tests, and it is much easier for me to do this though webstorm. Also, the project I'm working on is very small and I don't want to get fancy
The node.js project itself simply uses the built-in assert module and JavaScript exceptions. They have a fairly straightforward script that runs every .js file in a directory tree and if the file doesn't throw any exceptions, the test is considered passing. You could use something like that.
However, although most frameworks do have a command line runner, you absolutely never need to install them (or anything) with -g. If you understand the basic concept of the unix PATH environment variable, you can npm install --save-dev mocha (for example) and then run your tests with ./node_modules/.bin/mocha. No -g required.
See also http://peterlyons.com/problog/2012/09/managing-per-project-interpreters-and-the-path

Use jasmine-node to test meteor application with auto-test

I'm using jasmine-node to test my Meteor application and I want to use the auto-test feature so I don't have to rerun the tests all the time by myself.
My meteor application folder structure is like this:
server
foo.coffee
tests
foo.spec.coffee
And with the spec file I want to test code which is located in foo.coffee. I start jasmine-node with this args:
jasmine-node ./ --autotest --coffee --test-dir tests
And now I would assume that the autotest feature will react on all changes in the root folder but it just reacts on changes in the test folder. And I can't start it in the root folder because I get an error in the .meteor files (and I don't want to have jasmine testing/including the meteor code anyway).
So I want to have jasmine rerun the tests even if I change code in the server folder. How can I achieve that?
Use the --watch parameter along with --autotest and specify the directories that contain whatever files you want to have watched.

Resources