Sequential test scenarios for Jest - javascript

I've been starting to use create-react-app for my React projects, and as a testing library it comes with Jest.
As part of my React apps, I like to create integration tests (as well as unit tests) as I find it useful to be able to check the happy paths in the app are working as expected. For example: Render (mount) a page using Enzyme, but only mock out the http calls (using Sinon) so that the full React/Redux flow is exercised at once.
Before using create-react-app, I've used Mocha for testing, and I found mocha-steps to be a great extension for integration tests, as it allows tests in a group to be executed in sequence, and handles stopping if a step fails without stopping the entire test run.
Question: Is there any way to get Jest to behave in a similar way? Specifically, I'd like to be able to specify a series of tests in a group (such as in a describe) and have them execute sequentially in order.
I've been looking through the Jest docs, or for any other libraries that extend it, but I've come up empty. For now, it feels like the only option is to have one large test which makes me sad, and I'd prefer not to swap out Jest for Mocha if I can avoid it.
Thanks!

jest does not currently support flagging specific tests to be run serially (like ava's test.serial).
This has been requested before but I don't think they are working on it.
The workaround is for you to identify which test files are to be run concurrently and which ones are to be run serially.
For example, I usually name my unit tests with *.test.js and my integration tests with *.spec.js.
I can then run my unit tests concurrently.
$ jest '(/__tests__/.*\\.test)\\.js$'
And I can run my integration tests serially.
$ jest '(/__tests__/.*\\.spec)\\.ts$' --runInBand
I can combine the two in my package.json.
"scripts": {
"unit": "jest '(/__tests__/.*\\.test)\\.js$'",
"integration": "jest '(/__tests__/.*\\.spec)\\.ts$' --runInBand",
"test": "npm run unit && npm run integration"
}
Running npm test can then run concurrent tests first and then the serial ones next.

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?

What is the best way to get coverage stats in cucumber js?

I'm designing my tests using the Behavior Driven Development (BDD) approach using Gherkin syntax and running my tests with Cucumber JS.
I'm using Cucumber Studio to share reports and keep synced with my business stakeholders, and management.
Recently I needed to get test coverage reports for the project, and made some research but couldn't decide which library to use to get coverage reports and how.
So far I've found JSCover, Cucumber Reports, and Istanbul for test coverage reports, but I'm not sure how to use them exactly and which would be best for my case to use with Cucumber JS.
After several trials, I've figured out that it is pretty simple to use Istanbul JS to see code coverage.
I've followed the instructions on the website and install Istanbul's JavaScript library nyc using:
yarn add -D nyc
Then, I've updated my scripts in the package.json as the following:
...
"scripts": {
"test": "cucumber-js ...",
...
"coverage": "nyc yarn test"
},
...
And when I run yarn coverage it runs the tests with wrapping by nyc and creating a coverage report as the following:

What is a good way to run a large number of mocha tests at once from the command line?

I have some mocha tests that I run from a docker container which tests some services in other running docker containers.
Right now, I have a shell script that finds all the mocha js files, de-newlines them and passes them as an argument to mocha itself. That script then gets run in a docker container as the dockerfile CMD.
This works ok, but it is kind of hacky and is starting to get ugly with several dozen js files.
In javaland, I'd let maven run these, but I figure there must be something better suited for node/javascript.
You can either use mocha --recursive path/to/tests if you want recursively go through all folders and run all files as tests, or you can use globs to pass to mocha like mocha tests/**/test-*.js to filter out specific files matching a pattern.

How to run specific tests with frisby?

We are using frisby to run our integration tests and while developing them, it would be handy to execute one specific one or a group of tests, without having run all of them and see extra noise. Right now I am commenting out all the ones I don't want to run, which is getting tedious.
Is there a way, from the command line, to run specific frisby tests?
So basically, instead of
npm test
I want to be able to say
npm test --name: posts
Or something like that. I found this post about jasmine-only, but I'm not sure it will satisfy my needs.
Thanks so much!
I'm not sure if you're still looking for answer, but this is pretty simple.
Firstly install latest version of jasmine-node from command line: npm install jasmine-node -g
Then to run particular test use: jasmine-node --coffee putTestNameHere
Install jasmine-node module. Execute one file at a time - you can group your test cases in specific file:
jasmin-node moduleTestCases_spec.js
Also, if you want to specify exact test case name to be executed, you can make use of sequenty module. It is a nodejs module, which you can specify the order(and thus the exact test cases to execute).
To run specific test in Frishby just run:
npm test ./folder/filename.js
So lets assume you have an folder say test under that you have a file called api.spec.js
then you will execute like this:
npm test ./test/api.spec.js
Parallely don't forget to specify these below things into your package.json file
"scripts": {
"test": "mocha" }

Setup karma to test a node.js module?

I'm developing a Node.js module, and I want to use Karma to auto-test it while working.
In my config file, I setup this:
// list of files / patterns to load in the browser
files: [
'./index.js',
'./test/indexSpecs.js'
],
Obviously, since Node.js isn't included in the browser files, I get this error:
Uncaught ReferenceError: require is not defined
If i add:
files: [
'./node_modules/**/*.js',
'./index.js',
'./test/indexSpecs.js'
],
I get a bunch of errors. I think js files get loaded in alphabetical order, which is wrong.
I also think that Node.js cannot be run in a browser, so what I'm trying to do may be totally wrong. Is there an alternative?
I'm developing a Node.js module, and I want to use Karma to auto-test
it while working.
You should not. Karma is designed for client-side code.
To auto-test your code, the simplest way is to create a npm script similar to this one (with mocha):
"scripts": {
"test": "mocha ./**",
"test:watch": "npm run test -- -w"
}
Then, use npm test to run the tests on demand, or npm run test:watch to continuously run the tests.
You can also use a grunt or gulp script with a watch task if you prefer.
You are right about karma not being a good fit for testing server side code. It is going to run everything in the context of a browser, which is causing the issues you are seeing. If you wanted to develop a module for the server and the client you could use karma in conjunction with browserfiy, but you would still need to run the tests in a node environment.
Instead, I would suggest using mocha: a simple and powerful test runner that works great for testing node modules.

Categories

Resources