[ ] 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.
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)
No error. I don't see this error on NestJS 5.1.0
@Optional()
annotation for a constructor argumentIt breaks tests. Workaround is to import 'reflect-metadata';
at the top of each affected test file.
Nest version: 5.4.0
For Tooling issues:
- Node version: 8.9.4
- Platform: Mac 10.13.6
Others:
Jest version: 2.22.2
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.
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.