title: NestJS- npm test is very slow with a beforeEach hook
body: <p>I'm new to NestJS and i'm currently learning testing in NestJS.</p>
  <p>Is it a common issue that having a <code>beforeEach</code> with a <code>createTestingModule().compile()</code> and some <code>module.get()</code> increase test execution so much ?</p>
  <p>I have a dummy <code>expect(true)</code> test that takes 6 seconds to complete when having a <code>beforeEach</code> where it takes only 1 seconds without (and 1 second for this dummy test still seems a lot)...
  This is very annoying when you are running test in watch mode since it takes a lot of time and still i have only on little test.</p>
  <p>I couldn't find anything related to this issue with NestJS, so i'm probably something doing wrong but i couldn't figured what.</p>
  <p>I did try to use some jest flags like <code>--runInBand</code> or <code>--maxWorkers=4</code> but without success.</p>
  <p>--</p>
  <p><em>The tested application is really app created through a tutorial, nothing particular to notice on this side.</em></p>
  <p>Here is my <code>tasks.service.spec.ts</code> file (it's the only test file i have). I can provide a Github repo if it helps.</p>
  <pre class="lang-js prettyprint-override"><code>import { Test } from '@nestjs/testing';
  
  import { TaskRepository } from './task.repository';
  import { TasksService } from './tasks.service';
  
  const mockTaskRepository = () =&gt; ({});
  
  describe('tasks.service', () =&gt; {
    let tasksService;
    let taskRepository;
  
    beforeEach(async () =&gt; {
      const module = await Test.createTestingModule({
        providers: [
          TasksService,
          { provide: TaskRepository, useFactory: mockTaskRepository },
        ],
      }).compile();
  
      tasksService = await module.get(TasksService);
      taskRepository = await module.get(TaskRepository);
    });
    describe('Test', () =&gt; {
      it('pass', () =&gt; {
        expect(true);
      });
    });
  });
  
  </code></pre>
  <p>Here is my jest settings in <code>package.json</code></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;,
      &quot;moduleNameMapper&quot;: {
        &quot;^src/(.*)$&quot;: &quot;&lt;rootDir&gt;/$1&quot;
      }
    }
  </code></pre>
  <p>Here is results with <code>beforeEach</code> commented and without <code>beforeEach</code> commented</p>
  <pre><code># With beforeEach
  
  PASS  src/tasks/tasks.service.spec.ts (7.214 s)
    tasks.service
      Test
        ✓ pass (10 ms)
  
  Test Suites: 1 passed, 1 total
  Tests:       1 passed, 1 total
  Snapshots:   0 total
  Time:        7.241 s
  Ran all test suites.
  
  Watch Usage: Press w to show more.
  </code></pre>
  <pre><code># Without beforeEach
  
  PASS  src/tasks/tasks.service.spec.ts
    tasks.service
      Test
        ✓ pass (2 ms)
  
  Test Suites: 1 passed, 1 total
  Tests:       1 passed, 1 total
  Snapshots:   0 total
  Time:        1.561 s, estimated 8 s
  Ran all test suites.
  
  Watch Usage: Press w to show more.
  </code></pre>
  <p>Any tips or help are welcomed :-)
  Thanks!</p>