question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use jest flow-types in tests

See original GitHub issue

After running flow-typed install and getting package flow-typed/npm/jest_v21.x.x.js, how are we supposed to use these types? The names that flow check requires are not resolving

For example, this fails with the following messages: Missing type annotation for TArguments AND Cannot resolve name TArguments

export const fn = jest.fn(...args: TArguments): TReturn => {
    ...
};

Here’s an untyped boilerplate example of what I am having issues with:

// @flow
// fetch mock
export const fn = jest.fn(() => (url, params) => {
  console.log(params);
  console.log(url);
  return new Promise(resolve => resolve({ ok: true, status: 200 }));
});

export default jest.mock('../../src/shared/fetch', fn);

And here are the errors flow is giving me:

Error ------------------------------------------------------------------------------------------------- untitled.js:3:19

Missing type annotation for `TArguments`.

                        v-------------------------------
   3| export const fn = jest.fn(() => (url, params) => {
   4|   console.log(params);
   5|   console.log(url);
   6|   return new Promise(resolve => resolve({ ok: true, status: 200 }));
   7| });
      -^


Error ------------------------------------------------------------------------------------------------- untitled.js:3:19

Missing type annotation for `TReturn`.

                        v-------------------------------
   3| export const fn = jest.fn(() => (url, params) => {
   4|   console.log(params);
   5|   console.log(url);
   6|   return new Promise(resolve => resolve({ ok: true, status: 200 }));
   7| });
      -^

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
cortopycommented, Jul 17, 2018

@sobolevn solution doesn’t work for me

I’m getting babel parser errors: “Unexpected token”

My babel config:

"babel": {
    "presets": [
      "flow",
      [
        "env",
        {
          "targets": {
            "node": "8.10.0"
          }
        }
      ]
    ]
  },
2reactions
thejettdurhamcommented, Aug 3, 2018

@cortopy I had the same parsing error as you when using the provided example, but was able to move the type annotations to the left-hand-side of the assignment to get a version that worked 🙂

export const mockFn: ($ReadOnlyArray<string>) => string = jest.fn();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Running Jest tests with Flow typed Javascript files
This will be a simple tutorial about running Jest tests with Flow typed Javascript files in NodeJS project.
Read more >
What's the right way to write Jest tests verified with Flow?
1.Install flow to your project. If you use create-reat-app, here is a guide for this step. yarn add -D flow-bin yarn run flow...
Read more >
Jest 13.0: Flow & REPL
The Flow types serve two purposes: First, we believe that code is written to be read. Most of the time, code is written...
Read more >
Jest Tutorial - JavaScript Unit Testing Using Jest Framework
In this Jest tutorial, we will learn about various Jest features, Jest matchers and how to use Jest framework for JavaScript Unit Testing....
Read more >
Jest Tutorial for Beginners: Getting Started With JavaScript ...
In this Jest tutorial we'll cover only unit testing, but at the end of the article you'll find resources for the other types...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found