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.

Jest in Webstorm fails when importing an enum from a lib in the repo

See original GitHub issue

Current Behavior

Not sure its Nx related ** When running a unit test using Webstorm, it fails when the test includes an import of an enum from a lib in the repo. However, if I only import an interface / class / type, it does not fail. When running yarn test everything passes successfully even with the enum.

I searched for related tickets in ts-jest / babel-jest but couldn’t find anything specific.

Expected Behavior

Tests will pass successfully

Steps to Reproduce

https://github.com/FrozenPandaz/nx-examples/tree/enum-jest

  • Create a new repo with React app
  • Create a lib inside it
  • Add an interface and an enum to the exports of the lib
  • Go to app.tsx and import the interface only
  • Run the tests in app.spec.tsx from Webstorm - it passes successfully
  • Now add the import of the enum, and run tests again - it will fail

Failure Logs

Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    ../../libs/api/src/index.ts:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from './lib/api';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      4 | import star from './star.svg';
      5 |
    > 6 | import { InterfaceProps, InterfaceEnum } from '@enum-import/api';
        | ^
      7 |
      8 | export function App() {
      9 |   const props: InterfaceProps = { name: 'D' };

      at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1479:14)
      at Object.<anonymous> (src/app/app.tsx:6:1)

Environment

  Node : 14.16.1
  OS   : darwin x64
  yarn : 1.22.10
  
  nx : Not Found
  @nrwl/angular : Not Found
  @nrwl/cli : 12.7.1
  @nrwl/cypress : 12.7.1
  @nrwl/devkit : 12.7.1
  @nrwl/eslint-plugin-nx : 12.7.1
  @nrwl/express : Not Found
  @nrwl/jest : 12.7.1
  @nrwl/linter : 12.7.1
  @nrwl/nest : Not Found
  @nrwl/next : Not Found
  @nrwl/node : Not Found
  @nrwl/nx-cloud : Not Found
  @nrwl/react : 12.7.1
  @nrwl/schematics : Not Found
  @nrwl/tao : 12.7.1
  @nrwl/web : 12.7.1
  @nrwl/workspace : 12.7.1
  @nrwl/storybook : 12.7.1
  @nrwl/gatsby : Not Found
  typescript : 4.3.5

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
jvskriubakkencommented, Nov 11, 2021

It is time consuming to figure out the workaround, and a bit awkward to always ensure that working-dir is set to root when running tests from a JetBrains product, so it would be very useful to have this issue fixed

3reactions
npworkcommented, Sep 16, 2021

Managed to overcome this.

  1. Open Edit Configuration
Screenshot 2021-09-16 at 13 17 14
  1. Click on Edit configuration templates (left bottom)
  2. Select Jest
  3. Make sure Working directory is a root directory of your project
Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing With Jest in WebStorm - The JetBrains Blog
Let's see how WebStorm can help you test your app with Jest! ... For failed tests, it will open the line that was...
Read more >
TypeScript ES6 import module "File is not a module error"
This can be a class, interface, module, function, or enum. When imported, the exported symbol is consumed directly and is not qualified by...
Read more >
jest unexpected token export node_modules - You.com | The Search ...
Jest gives an error: "SyntaxError: Unexpected token export" ... However, it happens that library authors assume that you'll compile their sources.
Read more >
ts-node - npm
This is accomplished by hooking node's module loading APIs, enabling it to be used seamlessly alongside other Node.js tools and libraries.
Read more >
Understanding TypeScript Configuration Options
lib - allowJs - checkJs - jsx - declaration - declarationMap - sourceMap ... This is useful for the editors like VS Code...
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