title: Nest.js Jest cannot mock testing app but can mock testing controller
body: <p>I have a Jest Nest.js controller test where I can stub the calls to the live database. It works fine.</p>
  <p>I would like to have 'stubbed e2e' tests using nests HTTP Server (I am using supertest). However, when I import the <code>AppModule</code> I cannot seem to get jest to override anything.</p>
  <p>Here is my working controller setup using stubbed classes.</p>
  <pre><code>describe('Working stubbed controller test', () =&gt; {
      let controller;
  
      beforeAll(async () =&gt; {
          const moduleFixture = await Test.createTestingModule({
              controllers: [MyController],
              providers: [
                  {
                      provide: ObjectInjectionDependency,
                      useClass: ObjectInjectionDependency
                  },
                  {
                      provide: '@string/injection/dependency',
                      useClass: DbOjectMockIAmUsing
                  },
                  {
                      provide: '@another/string/injection/dependency',
                      useValue: '@another/string/injection/dependency',
                  },
                  {
                      provide: DbConnection,
                      useValue: {
                          selectOne: () =&gt; null,
                          selectAll: () =&gt; null
                      }
                  }
              ]
          })
          .compile();
  
          controller = moduleFixture.get&lt;MyController&gt;(MyController)
      });
  
      it('/GET url', async () =&gt; {
          const request = {
              method: &quot;GET&quot;,
              params: {},
              query: {}
          }
  
          expect(
              await controller.all(request)
          ).toHaveProperty(
              'data'
          )
      });
  });
  </code></pre>
  <p>Here is my unsuccessful attempt at incorporating stubbed classes along with the HTTP server using <code>supertest</code>. The stubbed classes are ignored.</p>
  <pre><code>describe('Bypassing stubs application test', () =&gt; {
      let app;
      let server;
  
      beforeAll(async () =&gt; {
          const moduleFixture = await Test.createTestingModule({
              imports: [AppModule],
              providers: [
                  {
                      provide: ObjectInjectionDependency,
                      useClass: ObjectInjectionDependency
                  },
                  {
                      provide: '@string/injection/dependency',
                      useClass: DbOjectMockIAmUsing
                  },
                  {
                      provide: '@another/string/injection/dependency',
                      useValue: '@another/string/injection/dependency',
                  },
                  {
                      provide: DbConnection,
                      useValue: {
                          selectOne: () =&gt; null,
                          selectAll: () =&gt; null
                      }
                  }
              ]
          })
          .compile();
  
          app = moduleFixture.createNestApplication();
          server = app.getHttpServer()
          await app.init();
      });
  
      it('/GET roots', async () =&gt; {
          expect(
              await request(server).get('/myEndpoint')
          ).toMatchObject({
              'statusCode': 200
          })
      });
  });
  </code></pre>
  <p>I tried using the <code>overrideProvider()</code> methods but they didn't work either</p>
  <pre><code>const moduleFixture = await Test.createTestingModule({
      imports: [AppModule]
  })
  .overrideProvider(ObjectInjectionDependency)
      .useClass(ObjectInjectionDependency)
  .overrideProvider('@string/injection/dependency')
      .useClass(DbOjectMockIAmUsing)
  .overrideProvider('@another/string/injection/dependency')
      .useValue('@another/string/injection/dependency')
  .overrideProvider(DbConnection)
      .useValue({
          selectOne: () =&gt; null,
          selectAll: () =&gt; null
      })
  .compile() 
  </code></pre>
  <p>I also tried using Jest to override the classes</p>
  <pre><code>Jest.mock('@path/to/dbconnection', () =&gt; {
      selectOne: () =&gt; null,
      selectAll: () =&gt; null
  }))
  </code></pre>
  <p>All didn't seem to have any effect.</p>
  <p>I tried <code>spyOn()</code></p>
  <pre><code>jest.spyOn(DbConnection, 'selectOne').mockImplementation(() =&gt; null);
  jest.spyOn(DbConnection, 'selectAll').mockImplementation(() =&gt; null);
  </code></pre>
  <p>but I seem to get a strange error</p>
  <pre><code>No overload matches this call.
    Overload 1 of 4, '(object: typeof DbConnection, method: never): SpyInstance&lt;never, never&gt;', gave the following error.
      Argument of type 'string' is not assignable to parameter of type 'never'.
    Overload 2 of 4, '(object: typeof DbConnection, method: never): SpyInstance&lt;never, never&gt;', gave the following error.
      Argument of type 'string' is not assignable to parameter of type 'never'.ts(2769)
  </code></pre>
  <p>I understand that testing the controller is 'good enough' but I am still curious what I am doing wrong, as I am sure I will find a use case for this testing method in future.</p>
  <p><strong>EDIT:</strong></p>
  <p>It turns out I was encountering two problems. Firstly, for whatever reason, I struggled to have Jest mock a method of a class. As per @Estus Flask's suggestion, I can now at least mock/stub methods</p>
  <pre><code>import { DbConnection } from '@some/path';
  
  jest.spyOn(DbConnection.prototype, 'selectOne').mockReturnValue(null); 
  </code></pre>
  <p>Secondly, nearly all tutorials explaining to mock using the explicit import path</p>
  <pre><code>import { DbConnection } from '@some/path';
  
  jest.mock('@some/path');
  DbConnection.mockReturnValue(null);
  </code></pre>
  <p>left out the detail about typescripts type checking which <a href="https://stackoverflow.com/questions/48759035/mock-dependency-in-jest-with-typescript?rq=1">I discovered from this answer</a> causing issues.</p>
  <pre><code>let myMock = &lt;jest.Mock&lt;DbConnection&gt;&gt;DbConnection;
  </code></pre>
  <p>While the initial error described above was different, the type casting and examples from the linked answer solved a lot of confusion.</p>
  <p>Still, given there is a bounty, perhaps someone can explain why the <code>providers</code> array is pretty much ignored when the <code>import</code> array contains the <code>AppModule</code></p>