nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.62k stars 2.36k forks source link

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

Closed ghost closed 2 years ago

ghost commented 3 years ago

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

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
backnotprop commented 3 years ago

Same, Angular, not just tests.

npwork commented 3 years ago

Same here :(

npwork commented 3 years ago

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
ghost commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 2 years ago

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 commented 2 years ago

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[bot] commented 1 year ago

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.