Here is my code:
import { it, describe, expect, vi, beforeEach } from 'vitest';
const ClientAuthenticator = require('../src/client-authenticator');
const { ssmClient, getParameterCommand } = require('../src/helpers/aws');
const ssmClientMock = vi.fn();
const getParameterCommandMock = vi.fn();
vi.mock('../src/helpers/aws', () => {
return {
ssmClient: ssmClientMock,
getParameterCommand: getParameterCommandMock,
};
});
describe('ClientAuthenticator.authenticator Tests', () => {
it('Should set correct client name', async () => {
// Arrange
console.log(ssmClient); // This logs real implementation not a mock.
const clientId = 'clientId';
const clientSecret = 'clientSecret';
... rest of the test ...
See that line that says console.log(ssmClient)
? this one prints real implementation not the mock though it should return ssmClientMock
(this happens also in the production code - it sees the real implementation not the mock.) What am I missing here? 🤔
It turned out that Vitest works with ES6 modules. It has no effect if you're using require
. More details are given here: https://vitest.dev/api/vi.html#vi-mock