Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Remove flow types from the npm distribution #37

Open
sansumbrella opened this issue Feb 5, 2019 · 12 comments
Open

Remove flow types from the npm distribution #37

sansumbrella opened this issue Feb 5, 2019 · 12 comments

Comments

@sansumbrella
Copy link

sansumbrella commented Feb 5, 2019

When I add mapbox-gl-js-mock to my project to a create-react-app project, I expect to be able to run unit tests that involve Mapbox types.

I add the mock as a dev dependency:

yarn add --dev mapbox-gl-js-mock

I then use it as a mock in my jest tests:

jest.mock("mapbox-gl", () => require("mapbox-gl-js-mock"));

Finally, I see failing tests because the Flow type annotations confuse Jest.

/node_modules/mapbox-gl-js-mock/node_modules/mapbox-gl/src/geo/lng_lat.js:24
        lng: number;
           ^
    SyntaxError: Unexpected token :

Unfortunately, Jest does not apply transformations to files in node_modules/, so I don't see a good way around this issue shy of updating the mock to not have type annotations. Any configuration suggestions that solve the problem would be great.

For what it's worth, the create-react-app typescript integration works just fine, but the workings of that are hidden in the react-scripts. After following the suggestions for enabling flow in create-react-app, I still have this issue.

@sansumbrella
Copy link
Author

I have also tried the remove-flow-types-loader in webpack in combination with react-app-rewired to customize webpack without success.

@yookoala
Copy link

yookoala commented Sep 3, 2019

Encountered the same problem. Does anybody have a solution yet?

@mwarren2
Copy link

mwarren2 commented Sep 3, 2019

Is Babel in your mix?
@babel/plugin-transform-flow-strip-types

@yookoala
Copy link

yookoala commented Sep 3, 2019

I'm using create-react-app and having problem with the jest test through react-scripts. So the water is pretty deep. I'm trying out a solution react-app-rewired with babel-jest.

@yookoala
Copy link

yookoala commented Sep 3, 2019

Solution

  1. Install react-app-rewired and rewrite package.json according to the documentation.
  2. Install @babel/plugin-transform-flow-strip-types.
  3. Create a new config-overrides.js with the jest attribute like this:
module.exports = {
    jest: (config) => {
        if (typeof config.plugins === 'undefined') {
            config.plugins = [];
        }
        config.plugins.push('transform-flow-strip-types');
        return config;
    },
}

Then the test works.

@gabimoncha
Copy link

gabimoncha commented Sep 13, 2019

@yookoala I tried your solution and still have this problem.
Are you defining the plugin in .babelrc or package.json?
Also, are you using @babel/preset-env and @babel/preset-flow?

@yookoala
Copy link

yookoala commented Sep 14, 2019

@gabrielmoncea: I have never handled my own babel config in this project. I don't even have a babelrc. I am using the CRA default setup with only the changes I wrote in my last comment.

Again, I only have problem when running test. There is no issue building or running test server.

@gabimoncha
Copy link

@yookoala weird. I tried w/ & w/o any babel config but still got this error

@yookoala
Copy link

@gabrielmoncea: What is the version of your CRA?

@gabimoncha
Copy link

gabimoncha commented Sep 17, 2019

My project initialised one year ago with 2.1.5, but I migrated to 3.1.1 and still nothing.

@gabimoncha
Copy link

@yookoala I just bootstrapped a new project with latest version of CRA and got the same test fail reason

@ted-moke
Copy link

ted-moke commented Nov 19, 2020

@gabrielmoncea I also couldn't get @yookoala fix to work. Ended up omitting this mock plugin and mocking just a bare object from this thread.

jest.mock('mapbox-gl/dist/mapbox-gl', () => ({
  GeolocateControl: jest.fn(),
  Map: jest.fn(() => ({
    addControl: jest.fn(),
    on: jest.fn(),
    remove: jest.fn(),
  })),
  NavigationControl: jest.fn(),
}));```

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants