Skip to content

Jest test fails on dynamic import #4600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
somysz opened this issue Jun 12, 2018 · 1 comment
Closed

Jest test fails on dynamic import #4600

somysz opened this issue Jun 12, 2018 · 1 comment

Comments

@somysz
Copy link

somysz commented Jun 12, 2018

Is this a bug report?

No

Did you try recovering your dependencies?

Yes

Steps to Reproduce

(Write your steps here:)

  1. Create a new project with create-react-app
  2. Create a new component in a new file and export it
  3. In your App.js component import it using dynamic import (I am also using react-loadable for this):
const AsyncComponent = Loadable({
  loader: () => import('../views/Component' /* webpackChunkName: "My component" */),
});
  1. In App.test.js (I am using enzyme) try to write a simple test:
it('renders without crashing', () => {
    const wrapper = shallow(<App />);
    expect(wrapper).toMatchSnapshot();
});

Expected Behavior

The test should pass.

Actual Behavior

Result:

src/app/App.tsx: Unexpected token (36:33)

> 36 | {...} return import('../views
                    ^

Possible solution

Allow "transform" option in jest's package.json config?

@ianschmitz
Copy link
Contributor

It looks like you're using Typescript? I'm guessing via create-react-app-typescript? If so this issue belongs over there.

If this is the case I ran into the same issue, and disabling allowSyntheticDefaultImports in the tsconfig.json did the trick for me. Check out wmonk/create-react-app-typescript#214 (comment) for more info.

@Timer Timer closed this as completed Jun 14, 2018
@lock lock bot locked and limited conversation to collaborators Jan 19, 2019
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

3 participants