I recently created a Jest unit test for a TypeScript function called checkEmail
, which internally uses showAlert
.
The showAlert
function in the utils.ts
file looks like this:
export const showAlert = (message: string) => {
toast(message);
};
In my test case, I mocked the showAlert
function as follows:
import {showAlert} from './utils'
showAlert = jest.fn()
https://i.stack.imgur.com/w1fgt.png
Although the test runs without issues, both VSCode and WebStorm display an error in the test file stating:
Cannot assign to 'showAlert' because it is not a variable.
showAlert = jest.fn()
^^^^^^^^^
If anyone has any advice on how to resolve this error, I would greatly appreciate it.
This is how the showAlert
is utilized:
function checkEmail(email: string) {
if (!email.trim()) {
showAlert('Email is required.');
}
}
You can find the repository where you can replicate the issue here: https://github.com/shishiranshuman13/tsjest-demo-error