Nest: Jest error with reflect-metadata

Created on 21 Nov 2018  路  4Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When @Optional() is used, I get Jest tests which fail with the following error:


 Test suite failed to run

    TypeError: Reflect.getMetadata is not a function

      at Optional (node_modules/@nestjs/common/decorators/core/optional.decorator.js:11:34)
      at node_modules/@nestjs/common/services/logger.service.js:12:37
      at Object..__decorate (node_modules/@nestjs/common/services/logger.service.js:5:95)
      at Object. (node_modules/@nestjs/common/services/logger.service.js:95:21)
      at Object. (node_modules/@nestjs/common/utils/load-package.util.js:3:26)

Expected behavior

No error. I don't see this error on NestJS 5.1.0

Minimal reproduction of the problem with instructions

  • Create a service with the @Optional() annotation for a constructor argument
  • Import that service into a Jest unit test

What is the motivation / use case for changing the behavior?

It breaks tests. Workaround is to import 'reflect-metadata'; at the top of each affected test file.

Environment


Nest version: 5.4.0


For Tooling issues:
- Node version: 8.9.4
- Platform: Mac 10.13.6

Others:

Jest version: 2.22.2

Most helpful comment

Nest shouldn't even import reflect-metadata at all.

Everytime you import it and its not cached, it resets all metadata.
So for instance if two libraries decides to import it from their own dependencies, you'll end up with missing metadata.
Instead the documentation should say that you MUST import it yourself in your entry file.

Anyhow, to workaround your problem, you can use https://jestjs.io/docs/en/configuration#setupfiles-array and them just import reflect-metadata before anything else, so you don't have to import it in every single test file.

All 4 comments

Nest shouldn't even import reflect-metadata at all.

Everytime you import it and its not cached, it resets all metadata.
So for instance if two libraries decides to import it from their own dependencies, you'll end up with missing metadata.
Instead the documentation should say that you MUST import it yourself in your entry file.

Anyhow, to workaround your problem, you can use https://jestjs.io/docs/en/configuration#setupfiles-array and them just import reflect-metadata before anything else, so you don't have to import it in every single test file.

Anyhow, to workaround your problem, you can use https://jestjs.io/docs/en/configuration#setupfiles-array and them just import reflect-metadata before anything else, so you don't have to import it in every single test file.

Sample of how doing it.

In package.json:

  "jest": {
    ...
    "setupFiles": ["./jest-setup-file.ts"]
  }

In jest-setup-file.ts:

import 'reflect-metadata';

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cdiaz picture cdiaz  路  3Comments

VRspace4 picture VRspace4  路  3Comments

marshall007 picture marshall007  路  3Comments

rlesniak picture rlesniak  路  3Comments

anyx picture anyx  路  3Comments