'jest unit test mock google oauthClient in nestjs
I am currently doing unit test for the services in Nestjs using Jest, one of which is using the google Oauth service. My goal is to verify the token from google and get the email to continue the process. Are there any ways to mock or skip the auth process and return the mocked email? Here is my code:
// ...other modules
import { google, Auth } from "googleapis";
export class GoogleAuthService {
oauthClient: Auth.OAuth2Client;
constructor(
// ...some provider services
) {
const clientID = this.configService.get("GOOGLE_AUTH_CLIENT_ID");
const clientSecret = this.configService.get("GOOGLE_AUTH_CLIENT_SECRET");
// how can I mock this?
this.oauthClient = new google.auth.OAuth2(clientID, clientSecret);
}
async authenticate(token: string) {
try {
// or need to mock this?
const tokenInfo = await this.oauthClient.getTokenInfo(token);
const email = tokenInfo.email;
// ...other code
} catch (error) {
throw new UnauthorizedException();
}
}
}
describle("AuthService unit test", () => {
let authService: AuthService;
beforeEach(async () => {
const module = await Test.createTestingModule({
providers: [
GoogleAuthService,
// ... other services
],
}).compile();
GoogleAuthService = await module.get(GoogleAuthService);
});
describe("google login", () => {
it("should return a user", async () => {
// don't know how to test the 'authenticate' service
const mockedToken = "mockedToken";
const result = await GoogleAuthService.authenticate(mockedToken);
expect(typeof result).toEqual(User);
});
});
})
Thanks for the help!!!
Solution 1:[1]
Here's the tested and working code. You only had to mock the google.auth.OAuth2
from googleapis
. In the following mock, I've created the User
object with only email
property, you need to add more properties according to your project requirements. Also for the completeness here, the authenticate()
returns email
instead of User
object. Change that according to your project:
jest.mock('googleapis', () => {
const googleApisMock = {
google: {
auth: {
// this is how to mock a constructor
OAuth2: jest.fn().mockImplementation(() => {
return {
getTokenInfo: jest.fn().mockResolvedValue({
email: testEmail
})
}
})
}
}
}
return googleApisMock
})
const testEmail = '[email protected]'
describe('AuthService', () => {
let googleAuthService: GoogleAuthService
beforeEach(async () => {
const module = await Test.createTestingModule({
providers: [
GoogleAuthService
// ... other services
]
}).compile()
googleAuthService = await module.get(GoogleAuthService)
})
describe('authenticate', () => {
it('should return a user', async () => {
const email = await googleAuthService.authenticate('any token')
expect(email).toEqual(testEmail)
})
})
})
Test results:
PASS src/auth/google-auth.service.spec.ts (9.391 s)
AuthService
authenticate
? should return a user (6 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 10.077 s
Ran all test suites related to changed files.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Yogesh Umesh Vaity |