When running Jest tests on a Nest project, my file fails to find a module that has no issues when running outside of tests

huangapple go评论106阅读模式
英文:

When running Jest tests on a Nest project, my file fails to find a module that has no issues when running outside of tests

问题

I have a NestJs file and I am trying to write some tests with Jest because I am trying to run some e2e tests on the endpoints. My test file sits on the same directory level of the controller. I am trying to test the PlaceController and my place.controller.spec.ts file is sitting on the same directory level as PlaceService and PlaceController.

The spec file looks like this:

  1. import { Test, TestingModule } from '@nestjs/testing';
  2. import { PlaceController } from './place.controller';
  3. import { PlaceService } from './place.service';
  4. import { expect } from 'chai';
  5. describe('PlaceController', () => {
  6. let appController: PlaceController;
  7. beforeEach(async () => {
  8. const app: TestingModule = await Test.createTestingModule({
  9. controllers: [PlaceController],
  10. providers: [PlaceService],
  11. }).compile();
  12. appController = app.get<PlaceController>(PlaceController);
  13. });
  14. it('should be defined', () => {
  15. expect(appController).toBeDefined();
  16. });
  17. });

My jest.config.ts looks like this:

  1. /** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
  2. module.exports = {
  3. preset: 'ts-jest',
  4. testEnvironment: 'node',
  5. testMatch: ['**/**/*.spec.ts'],
  6. verbose: true,
  7. forceExit: true,
  8. clearMocks: true,
  9. resetMocks: true,
  10. restoreMocks: true,
  11. bail: false,
  12. };

My tsconfig.json at the root of the project is this:

  1. {
  2. "compileOnSave": false,
  3. "compilerOptions": {
  4. "baseUrl": "./src",
  5. "outDir": "./dist",
  6. "incremental": true,
  7. "sourceMap": true,
  8. "declaration": false,
  9. "removeComments": true,
  10. "downlevelIteration": true,
  11. "emitDecoratorMetadata": true,
  12. "experimentalDecorators": true,
  13. "module": "commonjs",
  14. "moduleResolution": "node",
  15. "importHelpers": true,
  16. "skipLibCheck": true,
  17. "target": "es2017",
  18. "typeRoots": ["node_modules/@types"],
  19. "lib": ["es2018", "dom"],
  20. "paths": {
  21. "@app/*": ["./client/app/*"],
  22. "@assets/*": ["./client/assets/*"],
  23. "@env/*": ["./client/environments/*"],
  24. "@common/*": ["./common/*"],
  25. "@server/*": ["./server/*"]
  26. }
  27. }
  28. }

I don't know why, but there is another tsconfig file two levels up in the directory where all the controller/controller.spec files are. I didn't write this so I'd rather not touch it. But it looks like this:

  1. {
  2. "extends": "../../tsconfig.json",
  3. "exclude": ["node_modules", "test", "dist"]
  4. }

To run the tests, I have "test": "NODE_ENV=test jest" in my package.json scripts. However, when I run npm run test, I got the following error:

  1. Cannot find module '@server/app/api/place/place.dto' from 'src/server/app/api/place/place.controller.ts'
  2. Require stack:
  3. src/server/app/api/place/place.controller.ts
  4. src/server/app/api/place/place.controller.spec.ts
  5. > 1 | import { CreatePlaceDto } from '@server/app/api/place/place.dto';
  6. | ^
  7. 2 | import { PlaceService } from '@server/app/api/place/place.service';
  8. 3 | import { CrudRequest } from '@server/app/core/crud/crud';
  9. 4 | import {

But when I run the app normally, not tests, everything works perfectly and my api returns the correct response when I call it in Postman. What can be causing the issue? Thank you!

英文:

I have a NestJs file and I am trying to write some tests with Jest because I am trying to run some e2e tests on the endpoints. My test file sits on the same directory level of the controller. I am trying to test the PlaceController and my place.controller.spec.ts file is sitting on the same directory level as PlaceService and PlaceController.

The spec file looks like this:

  1. import { Test, TestingModule } from &#39;@nestjs/testing&#39;;
  2. import { PlaceController } from &#39;./place.controller&#39;;
  3. import { PlaceService } from &#39;./place.service&#39;;
  4. import { expect } from &#39;chai&#39;;
  5. describe(&#39;PlaceController&#39;, () =&gt; {
  6. let appController: PlaceController;
  7. beforeEach(async () =&gt; {
  8. const app: TestingModule = await Test.createTestingModule({
  9. controllers: [PlaceController],
  10. providers: [PlaceService],
  11. }).compile();
  12. appController = app.get&lt;PlaceController&gt;(PlaceController);
  13. });
  14. it(&#39;should be defined&#39;, () =&gt; {
  15. expect(appController).toBeDefined();
  16. });
  17. });

My jest.config.ts looks like this:

  1. /** @type {import(&#39;ts-jest/dist/types&#39;).InitialOptionsTsJest} */
  2. module.exports = {
  3. preset: &#39;ts-jest&#39;,
  4. testEnvironment: &#39;node&#39;,
  5. testMatch: [&#39;**/**/*.spec.ts&#39;],
  6. verbose: true,
  7. forceExit: true,
  8. clearMocks: true,
  9. resetMocks: true,
  10. restoreMocks: true,
  11. bail: false,
  12. };

My tsconfig.json at the root of the project is this:

  1. {
  2. &quot;compileOnSave&quot;: false,
  3. &quot;compilerOptions&quot;: {
  4. &quot;baseUrl&quot;: &quot;./src&quot;,
  5. &quot;outDir&quot;: &quot;./dist&quot;,
  6. &quot;incremental&quot;: true,
  7. &quot;sourceMap&quot;: true,
  8. &quot;declaration&quot;: false,
  9. &quot;removeComments&quot;: true,
  10. &quot;downlevelIteration&quot;: true,
  11. &quot;emitDecoratorMetadata&quot;: true,
  12. &quot;experimentalDecorators&quot;: true,
  13. &quot;module&quot;: &quot;commonjs&quot;,
  14. &quot;moduleResolution&quot;: &quot;node&quot;,
  15. &quot;importHelpers&quot;: true,
  16. &quot;skipLibCheck&quot;: true,
  17. &quot;target&quot;: &quot;es2017&quot;,
  18. &quot;typeRoots&quot;: [
  19. &quot;node_modules/@types&quot;
  20. ],
  21. &quot;lib&quot;: [
  22. &quot;es2018&quot;,
  23. &quot;dom&quot;
  24. ],
  25. &quot;paths&quot;: {
  26. &quot;@app/*&quot;: [
  27. &quot;./client/app/*&quot;
  28. ],
  29. &quot;@assets/*&quot;: [
  30. &quot;./client/assets/*&quot;
  31. ],
  32. &quot;@env/*&quot;: [
  33. &quot;./client/environments/*&quot;
  34. ],
  35. &quot;@common/*&quot;: [
  36. &quot;./common/*&quot;
  37. ],
  38. &quot;@server/*&quot;: [
  39. &quot;./server/*&quot;
  40. ]
  41. }
  42. },
  43. }

I don't know why, but there is another tsconfig file two levels up in the directory where all the controller/controller.spec files are. I didn't write this so I'd rather not touch it. But it looks like this

  1. {
  2. &quot;extends&quot;: &quot;../../tsconfig.json&quot;,
  3. &quot;exclude&quot;: [&quot;node_modules&quot;, &quot;test&quot;, &quot;dist&quot;]
  4. }

To run the tests, I have &quot;test&quot;: &quot;NODE_ENV=test jest&quot; in my package.json scripts. However, when I run npm run test, I got the following error:

  1. Cannot find module &#39;@server/app/api/place/place.dto&#39; from &#39;src/server/app/api/place/place.controller.ts&#39;
  2. Require stack:
  3. src/server/app/api/place/place.controller.ts
  4. src/server/app/api/place/place.controller.spec.ts
  5. &gt; 1 | import { CreatePlaceDto } from &#39;@server/app/api/place/place.dto&#39;;
  6. | ^
  7. 2 | import { PlaceService } from &#39;@server/app/api/place/place.service&#39;;
  8. 3 | import { CrudRequest } from &#39;@server/app/core/crud/crud&#39;;
  9. 4 | import {

But when I run the app normally, not tests, everything works perfectly and my api returns the correct response when I call it in Postman. What can be causing the issue? Thank you!

答案1

得分: 1

如果您直接使用'./'而不是src来显示路径,问题将得以解决。

英文:

If you show the path directly with './' instead of src, the problem will be solved.

huangapple
  • 本文由 发表于 2023年6月22日 11:28:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定