title: NestJS - Test suite failed to run Cannot find module 'src/article/article.entity' from 'comment/comment.entity.ts'
body: <p>i need help with nestjs and jest testing. I am new to NestJS and i got stuck on Cannot find module error when i run tests.</p>
  <p>I am trying to test my service and when i run tests i have received error message:</p>
  <p>src/article/article.service.spec.ts
  ● Test suite failed to run</p>
  <pre><code>Cannot find module 'src/article/article.entity' from 'comment/comment.entity.ts'
  
  Require stack:
    comment/comment.entity.ts
    article/article.entity.ts
    article/article.service.spec.ts
  
     6 |   ManyToOne,
     7 | } from 'typeorm';
  &gt;  8 | import { Article } from 'src/article/article.entity';
       | ^
     9 | 
    10 | @Entity()
    11 | export class Comment {
  
    at Resolver.resolveModule (../node_modules/jest-resolve/build/index.js:307:11)
    at Object.&lt;anonymous&gt; (comment/comment.entity.ts:8:1)
  </code></pre>
  <p>This similiar error is showing up through all other tests in different controllers, services etc.</p>
  <p>There is my code what i am trying to test.</p>
  <p>article.service.ts</p>
  <pre><code>import { Injectable } from &quot;@nestjs/common&quot;;
  import { InjectRepository } from &quot;@nestjs/typeorm&quot;;
  import { Article } from &quot;./article.entity&quot;;
  import { ArticleRepository } from &quot;./article.repository&quot;;
  import { ArticleDTO } from &quot;./dto/article.dto&quot;;
  import { DeleteResult } from &quot;typeorm&quot;;
  import { ArticleRO } from &quot;./dto/article.response&quot;;
  import { UserRepository } from &quot;src/user/user.repository&quot;;
  
  @Injectable()
  export class ArticleService {
    constructor(
      private readonly articleRepository: ArticleRepository,
      private readonly userRepository: UserRepository
    ) {}
  
    async getAllPosts(): Promise&lt;ArticleRO[]&gt; {
      return await this.articleRepository.find();
    }
  }
  </code></pre>
  <p>article.repository.ts</p>
  <pre><code>import { Repository, EntityRepository } from 'typeorm';
  import { Article } from './article.entity';
  @EntityRepository(Article)
  export class ArticleRepository extends Repository&lt;Article&gt; {
  }
  </code></pre>
  <p>article.service.specs.ts</p>
  <pre><code>import { Test, TestingModule } from &quot;@nestjs/testing&quot;;
  import { getRepositoryToken } from &quot;@nestjs/typeorm&quot;;
  import { Article } from &quot;./article.entity&quot;;
  import { ArticleRepository } from &quot;./article.repository&quot;;
  import { ArticleService } from &quot;./article.service&quot;;
  import { ArticleRO } from &quot;./dto/article.response&quot;;
  
  describe(&quot;PostService&quot;, () =&gt; {
    let service: ArticleService;
    let articleRepository: ArticleRepository;
    beforeEach(async () =&gt; {
      const module: TestingModule = await Test.createTestingModule({
        providers: [
          ArticleService,
          {
            provide: getRepositoryToken(Article),
            useClass: ArticleRepository,
          },
        ],
      }).compile();
  
      service = module.get&lt;ArticleService&gt;(ArticleService);
      articleRepository = module.get&lt;ArticleRepository&gt;(
        getRepositoryToken(Article)
      );
    });
  
    it(&quot;should be defined&quot;, () =&gt; {
      expect(service).toBeDefined();
    });
    describe(&quot;findAll&quot;, () =&gt; {
      it(&quot;should return an array of cats&quot;, async () =&gt; {
        const result: ArticleRO[] = [];
        jest.spyOn(service, &quot;getAllPosts&quot;).mockResolvedValueOnce(result);
  
        expect(await service.getAllPosts()).toBe(result);
      });
    });
  });
  
  
  </code></pre>
  <p>comment.entity.ts</p>
  <pre><code>import {
    Entity,
    Column,
    PrimaryGeneratedColumn,
    CreateDateColumn,
    ManyToOne,
  } from 'typeorm';
  import { Article } from 'src/article/article.entity';
  
  @Entity()
  export class Comment {
    @PrimaryGeneratedColumn()
    id: number;
  
    @Column()
    author: string;
  
    @Column()
    content: string;
  
    @CreateDateColumn()
    createdAt: Date;
  
    @ManyToOne(
      () =&gt; Article,
      article =&gt; article.comments,
    )
    article: Article;
  }
  </code></pre>
  <p>there are my jest settings from package.json.</p>
  <pre><code>&quot;jest&quot;: {
      &quot;moduleFileExtensions&quot;: [
        &quot;js&quot;,
        &quot;json&quot;,
        &quot;ts&quot;
      ],
      &quot;rootDir&quot;: &quot;src&quot;,
      &quot;testRegex&quot;: &quot;.spec.ts$&quot;,
      &quot;transform&quot;: {
        &quot;^.+\\.(t|j)s$&quot;: &quot;ts-jest&quot;
      },
      &quot;coverageDirectory&quot;: &quot;../coverage&quot;,
      &quot;testEnvironment&quot;: &quot;node&quot;
    }
  </code></pre>
  <p>I tried to change my rootDir to &quot;./src&quot; but this does not work.</p>
  <p>I generated project with nest new blabla. So my jest settings are default. Maybe i am doing something wrong with my custom repository mocking in tests.</p>