title: Jest with NestJS and async function
body: <p>I'm trying to a test a async function of a <code>service</code> in <strong>nestJS</strong>.</p>
  <p>this function is async... basically get a value (JSON) from database (using repository - TypeORM), and when successfully get the data, &quot;transform&quot; to a different class (DTO)...
  the implementation:</p>
  <pre><code>async getAppConfig(): Promise&lt;ConfigAppDto&gt; {
    return this.configRepository.findOne({
      key: Equal(&quot;APPLICATION&quot;),
    }).then(config =&gt; {
      if (config == null) {
        return new class implements ConfigAppDto {
          clientId = '';
          clientSecret = '';
        };
      }
      return JSON.parse(config.value) as ConfigAppDto;
    });
  }
  </code></pre>
  <p>using a controller, I checked that this worked ok.
  Now, I'm trying to use Jest to do the tests, but with no success...
  My problem is how to mock the <code>findOne</code> function from <code>repository</code>..</p>
  <p><strong>Edit</strong>: I'm trying to use <code>@golevelup/nestjs-testing</code> to mock <code>Repository</code>!</p>
  <p>I already mocked the <code>repository</code>, but for some reason, the <code>resolve</code> is never called..</p>
  <pre><code>describe('getAppConfig', () =&gt; {
    const repo = createMock&lt;Repository&lt;Config&gt;&gt;();
  
    beforeEach(async () =&gt; {
      await Test.createTestingModule({
        providers: [
          ConfigService,
          {
            provide: getRepositoryToken(Config),
            useValue: repo,
          }
        ],
      }).compile();
    });
  
    it('should return ConfigApp parameters', async () =&gt; {
      const mockedConfig = new Config('APPLICATION', '{&quot;clientId&quot;: &quot;foo&quot;,&quot;clientSecret&quot;: &quot;bar&quot;}');
      repo.findOne.mockResolvedValue(mockedConfig);
      expect(await repo.findOne()).toEqual(mockedConfig); // ok
  
      const expectedReturn = new class implements ConfigAppDto {
        clientId = 'foo';
        clientSecret = 'bar';
      };
      expect(await service.getAppConfig()).toEqual(expectedReturn);
  
      // jest documentation about async -&gt; https://jestjs.io/docs/en/asynchronous
      // return expect(service.getAppConfig()).resolves.toBe(expectedReturn);
    });
  })
  </code></pre>
  <ul>
  <li>the <code>expect(await repo.findOne()).toEqual(mockedConfig);</code> works great;</li>
  <li><code>expect(await service.getAppConfig()).toEqual(expectedReturn);</code> got a timeout =&gt; <code>Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout</code>;</li>
  </ul>
  <p>using debug, I see that the <code>service.getAppConfig()</code> is called, the <code>repository.findOne()</code> too, but the <code>.then</code> of repository of findOne is never called.</p>
  <p><strong>Update</strong>: I'm trying to mock the repository using <code>@golevelup/nestjs-testing</code>, and for some reason, the mocked result don't works on service.
  If I mock the repository using only <code>jest</code> (like code below), the test works... so, I think my real problem it's <code>@golevelup/nestjs-testing</code>.</p>
  <pre><code>...
  provide: getRepositoryToken(Config),
  useValue: {
    find: jest.fn().mockResolvedValue([new Config()])
  },
  ...
  </code></pre>