nx icon indicating copy to clipboard operation
nx copied to clipboard

@nx/angular fetch is undefined in tests

Open alessandro-verdura-maersk opened this issue 1 year ago • 2 comments

Current Behavior

When trying to upgrade from @nx/[email protected] to @nx/[email protected], any tests related to code using fetch starts to fail, with this error:

// TypeError: (0, angular_1.fetch) is not a function

class OneOfMyEffects {
  public someEffect$ = createEffect(() =>
    this.actions$.pipe(
      ofType(MyActions.someAction),
      fetch({ // <-- error is thrown here
        run: () => {}
      })
    )
  );
}

It actually seems that anything imported from @nx/angular is undefined when the code is running in the test environment

I tracked the issue coming from something added to 16.3.0-beta.8, but couldn't figure out the exact issue : /

Expected Behavior

That fetch is available as before in the test environment

GitHub Repo

No response

Steps to Reproduce

  1. Bump @nx/angular to a version equal to or greater than 16.3.0-beta.8
  2. Tests some code that imports the module's fetch function
  3. Should get an error saying that fetch is undefined

Nx Report

Node   : 18.16.0
OS     : darwin x64
npm    : 9.5.1
Hasher : Native

nx                 : 16.3.2
@nx/js             : 16.3.2
@nx/jest           : 16.3.2
@nx/linter         : 16.3.2
@nx/workspace      : 16.3.2
@nx/angular        : 16.3.2
@nx/cypress        : 16.3.2
@nx/devkit         : 16.3.2
@nx/eslint-plugin  : 16.3.2
@nx/express        : 16.3.2
@nx/node           : 16.3.2
@nrwl/tao          : 16.3.2
@nx/web            : 16.3.2
@nx/webpack        : 16.3.2
nx-cloud           : 16.0.5
typescript         : 4.8.4
---------------------------------------
Community plugins:
@compodoc/compodoc    : 1.1.21
@ngrx/component-store : 14.3.3
@ngrx/effects         : 14.3.3
@ngrx/entity          : 14.3.3
@ngrx/router-store    : 14.3.3
@ngrx/schematics      : 14.3.3
@ngrx/store           : 14.3.3
@ngrx/store-devtools  : 14.3.3
ngrx-immer            : 2.0.1

Failure Logs

No response

Operating System

  • [X] macOS
  • [ ] Linux
  • [ ] Windows
  • [ ] Other (Please specify)

Additional Information

No response

I also ran into this issue when updating to 16.3. Looks like this was caused by the new index.cts - index.ts exports fetch, index.cts doesn't.

I worked around this by setting the following in my jest.preset.js

moduleNameMapper: {
  '^@nx/angular$': __dirname + '/node_modules/@nx/angular/index.js'
}

swcm-mnestler avatar Jun 12 '23 08:06 swcm-mnestler

@swcm-mnestler thanks a lot, it works!

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[bot] avatar Jul 23 '23 00:07 github-actions[bot]