I want to use jest for my server unit testing (instead of mocha+chai). Is there a way I can run async function before all tests start (init purposes) only once and not for every test file? And also if there's a way of running something after all tests are done?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
jest provides option for both global setup and teardown in new versions. You can create files for both setup and teardown exporting an async function and provide that path in jest configurarion like this.
If you execute jest tests with npm you can run any node command or any executable before executing other command
so now you can run this with command
This feature was added in Jest's 22 version, with
globalSetup
andglobalTeardown
configurations. Look at this for examples.Jest provides
beforeAll
andafterAll
. As withtest
/it
it will wait for a promise to resolve, if the function returns a promise.It also supports callback style, if you have some existing test code that uses callbacks, although it's recommended to use promises.