title: NestJS mock JWT authentication in e2e tests
body: <p>I want to mock JWT Authentication in my NestJS e2e (integration) tests. I use injected token data in my business logic, and I don't want to insert credentials on my test code.</p>
  
  <p>I implemented JWT authentication as per the <a href="https://docs.nestjs.com/techniques/authentication" rel="noreferrer">Nest authentication docs</a>. I use a decorator (similar) to the <code>@User</code> decorator in the examples, and use information on the User object in my business logic.</p>
  
  <pre class="lang-js prettyprint-override"><code>@Post()
  async myPostEndpoint(@Body() body: PostBody, @User() user: MyUser){
      // do stuff using user properties
  }
  </code></pre>
  
  <p>When testing my application using supertest as indicated in the <a href="https://docs.nestjs.com/fundamentals/testing#end-to-end-testing" rel="noreferrer">Nest testing docs</a>, I don't want to make a real authentication request, as I would need to persist credentials in my version control system.</p>
  
  <p>I expected to be able to override providers to return a test user. But couldn't figure it out. </p>
  
  <p>I tried overriding the <code>AuthService</code>'s <code>validateClient</code> and <code>login</code> to return a default user using <code>overrideProvider + useClass/useFactory/useValue</code> as indicated in the <a href="https://docs.nestjs.com/fundamentals/testing#end-to-end-testing" rel="noreferrer">Nest testing docs</a>. I also tried overriding methods in <code>JwtStrategy</code> and <code>LocalStrategy</code>, but the requests still return 401 - Unauthorized.</p>