I recently changed from
Jest. Pretty lovely experiences, cost a while to figure out the right way to test an express route, but when it works, it works like a charm. I share my experiences here, hope it helps. I love its assertion function more than
Chai, seems more concise to me.
You need to install 1 npm install --save-dev babel-cli babel-preset-env jest supertest superagent
npm install --save-dev babel-cli babel-preset-env jest supertest superagent
The reason behind this is that so it won’t listen to the port after testing.
This is your
Then you can start your server via
node server.js, and you can import that
app.js for testing.
Jest test will end when it hits the last line of the test function, so you need to use a
done() to make it right.
done is not a must, the following one is more neat.
Two Things to be noticed here:
returnis crucial, otherwise your tests will get stuck.
- No need to pass
doneto your test.
It’s good to see that my beloved
Does the imperative programming style and synchronous looking make you feel happy? :D Two things also be noticed here.
asyncto the function before you want to use
- You need the
babel-preset-envpackage to use this.
Supertest way is still available. You just need to
return the statement and remember not use
.end() and the end.
Thanks Adam Beres-Deak for the hint!
A working example is as the following:
Notice that without that
return, the test will always pass.
That’s all, hope it help. :)