Skip to content
This repository has been archived by the owner on Jan 31, 2021. It is now read-only.

Unexpected token 'export' #3

Closed
websocket98765 opened this issue Jun 4, 2020 · 2 comments
Closed

Unexpected token 'export' #3

websocket98765 opened this issue Jun 4, 2020 · 2 comments
Labels
question Further information is requested

Comments

@websocket98765
Copy link

websocket98765 commented Jun 4, 2020

I encountered the same error that we saw on the sandbox again. This time when running a Jest test that shallow renders my main <App /> component using Enzyme:

It seems to be triggered by using import { useRouter } from 'react-router-ts';, since this line is pointed out in the error message. That's also about when we saw the error on the sandbox too, after it was updated to use that.

/node_modules/react-router-ts/dist/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest)
    {export * from "./basename";                 
     ^^^^^^
    SyntaxError: Unexpected token 'export'

    > 
    > 19 | import { useRouter } from 'react-router-ts';
    > 
    
    at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
    at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
    at Object.<anonymous> (src/components/App.jsx:19:1)

Possibly an issue with the build script not compiling the ./basename file?

Interestingly, my app runs fine. Only my tests causes the error.

@Lusito
Copy link
Owner

Lusito commented Jun 4, 2020

This library is shipped as es2015 modules. jest doesn't support this natively yet, see these topics for various workarounds:
nrwl/nx#812 (comment)
jestjs/jest#4842
jestjs/jest#2081

I've used the first one successfully. I.e. change your jest.config.js to this:

const esModules = ['react-router-ts'].join('|');
// ...
module.exports = {
  //...
    transformIgnorePatterns: [`/node_modules/(?!${esModules})`],
  // ...
};

I thought about providing an additional commonjs build to avoid these kind of issues in outdated tools, but I didn't get to do the research yet.

@Lusito Lusito added the question Further information is requested label Jun 4, 2020
@websocket98765
Copy link
Author

Thanks for the info. Hopefully Jest gets this updated eventually. I experimented with the suggestion & others, but it gets more complicated once other layers like create-react-app and react-app-rewired are involved, given both alter Babel and Jest behavior. Decided it's not a big deal and just skipping tests for my the App component to keep making progress.

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

No branches or pull requests

2 participants