Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the right way to write Jest tests verified with Flow?

I imagine people commonly use Flow and Jest (and React) together, but Flow doesn't seem to know about Jest (or Jasmine) globals. When I add // @flow to my tests, I get Flow errors like this:

src/__tests__/Thing-test.js:3   3: jest.unmock('../Thing')      ^^^^ identifier `jest`. Could not resolve name  src/__tests__/Thing-test.js:7   7: describe('Thing', () => {      ^^^^^^^^ identifier `describe`. Could not resolve name  src/__tests__/Thing-test.js:8   8:   it('does stuff', () => {        ^^ identifier `it`. Could not resolve name 

I could write a Flow interface for Jest/Jasmine, but that seems lengthy and like I must be missing something. Letting Flow process node_modules/jest-cli doesn't seem to help.

like image 253
Trevor Robinson Avatar asked Mar 09 '16 17:03

Trevor Robinson


People also ask

How do you write Jest tests?

To create a test case in Jest we use the test() function. It takes a test name string and handler function as the first two arguments. The test() function can also be called under the alias - it() .

What order do you run Jest tests?

If you want to run files in sequence as well, run Jest with the --runInBand command line flag. ( -i does the same thing.) Doing this, the scheduler might still run them in a different order from run to run. To prevent this as well, you will need to define your own test sequencer.

Is Jest a runner test?

Jest is a JavaScript test runner, that is, a JavaScript library for creating, running, and structuring tests. Jest ships as an NPM package, you can install it in any JavaScript project. Jest is one of the most popular test runner these days, and the default choice for React projects.

What is beforeAll and afterAll in Jest?

Jest provides beforeAll and afterAll . As with test / it it will wait for a promise to resolve, if the function returns a promise. beforeAll(() => { return new Promise(resolve => { // Asynchronous task // ...


1 Answers

Although Jest is written with flow annotations they strip types for the npm version so we don't need babel to run it. Fortunately the types are already in flow-type so the solution is quite easy (just as mentioned in the comment):

npm install -g flow-typed  flow-typed install [email protected] # <-- replace the version with the latest 

Although I had to add this line as well to my .eslintrc.json:

{   "env": {     "jest": true   } } 
like image 88
Kutyel Avatar answered Oct 11 '22 19:10

Kutyel