Skip to content
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

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

Closed
danr-za opened this issue Aug 17, 2021 · 9 comments
Closed

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

danr-za opened this issue Aug 17, 2021 · 9 comments
Labels
outdated scope: react Issues related to React support for Nx scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug

Comments

@danr-za
Copy link
Contributor

danr-za commented Aug 17, 2021

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
@FrozenPandaz FrozenPandaz added scope: react Issues related to React support for Nx scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx labels Aug 27, 2021
@backnotprop
Copy link

Same, Angular, not just tests.

@npwork
Copy link

npwork commented Sep 16, 2021

Same here :(

@npwork
Copy link

npwork commented 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

@danr-za
Copy link
Contributor Author

danr-za commented 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

this works, but its still a bug - by default Webstorm grabs the nearest jest.config.js file which is the one in the library and it extends the one in the root, so it should also work as same.

@ruslan-byondxr
Copy link

I have the same problem.
The solution given by @npwork works, but it's still a bug because it's not working from the command line and CI workflow (github actions)

@jvskriubakken
Copy link

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

@barbados-clemens
Copy link
Contributor

I'm not 100% sure how this is an nx issue to fix since we cannot control where webstorm looks for a jest config.

as a side note, I've not run into this myself as a daily webstorm user which I find odd 🤔

@tcoz
Copy link

tcoz commented Jun 25, 2022

Crashed headfirst into this today. An imported enum from a file in the same directory as the test spec fails. I tried some basic things, setting another const equal to the enum in the test itself, nothing worked. I had to put a note in the spec file that this is in fact using so-and-so enum but a bug is preventing the value of that enum from appearing properly.

Note that I'm not using Nx. I am however using webstorm. So it would seem this isn't on Nx.

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: react Issues related to React support for Nx scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx type: bug
Projects
None yet
Development

No branches or pull requests

8 participants