while running npm start the application working perfectly, but running the npm test gets the below error
TypeError: (0 , _ToastComponent.showToast) is not a function
20 | } = error;
21 | if (response && msg) {
> 22 | showToast(msg, "toast toast-danger", "alert", true, getToastTimeLimit());
| ^
23 | } else {
24 | showToast("Internal server error!", "toast toast-danger", "alert", true, getToastTimeLimit());
25 | }
my jest-setup.js file is
import '@testing-library/jest-dom';
and my package.json is
{
"name": "test-ui",
"private": true,
"version": "1.0.0",
"scripts": {
"start": "vite",
"test": "jest",
"build": "vite build",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-transform-runtime": "^7.25.9",
"@trimble-oss/modus-icons": "^1.15.0",
"@trimble-oss/modus-react-components": "^0.36.0-react18",
"@trimbleinc/modus-react-bootstrap": "^1.2.4",
"axios": "^1.7.7",
"date-fns": "^4.1.0",
"express": "^4.21.1",
"gulp": "^5.0.0",
"jwt-decode": "^3.1.2",
"react": "^18.3.1",
"react-bootstrap": "^2.10.4",
"react-dom": "^18.3.1",
"react-router-dom": "^6.26.1"
},
"devDependencies": {
"@babel/core": "^7.26.0",
"@babel/preset-env": "^7.26.0",
"@babel/preset-react": "^7.8.3",
"@testing-library/dom": "^9.3.4",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^14.3.1",
"@vitejs/plugin-react": "^3.1.0",
"babel-jest": "^27.5.1",
"babel-loader": "^8.0.6",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.0.1",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.19.0",
"eslint-plugin-react-hooks": "^1.7.0",
"jest": "^27.5.1",
"jest-css-modules-transform": "^4.4.2",
"vite": "^4.5.5"
}
}
and my code which shows the error is
import http, { getRefreshToken } from "./httpService";
import { showToast } from "../components/common/ToastComponent";
import { showSpinner } from "../components/common/SpinnerComponent";
import { getToastTimeLimit } from "../properties/properties";
import { loadConfig } from "../utils/util";
export async function getAccessToken(username, password) {
const { hostName } = await loadConfig();
try {
const response = await http.post(`${hostName}/test/test`, { username, password });
console.log("Login successful :-)");
return response;
} catch (error) {
const {
response: {
data: { msg },
},
response,
} = error;
if (response && msg) {
showToast(msg, "toast toast-danger", "alert", true, getToastTimeLimit());
} else {
showToast("Internal server error!", "toast toast-danger", "alert", true, getToastTimeLimit());
}
showSpinner(false);
throw error;
}
}
The test file code which throws a error is
test("calling the api end point for a invalid input", async () => {
const checkLoggedInSpy = jest
.spyOn(http, "checkLoggedIn")
.mockReturnValue(true);
renderComponent();
axios.post.mockRejectedValue({
response: {
data: {
msg: "Invalid username or password",
},
},
});
const usernameInput = screen.getByPlaceholderText("Username");
const passwordInput = screen.getByPlaceholderText("Password");
fireEvent.change(usernameInput, { target: { value: "test" } });
fireEvent.change(passwordInput, { target: { value: "test" } });
await waitFor(() => {
const loginButton = screen.getByTestId("login-btn");
fireEvent.click(loginButton);
expect(axios.post).toHaveBeenCalledWith(expect.any(String), {
username: "test",
password: "test",
});
});
checkLoggedInSpy.mockRestore();
});
I have tried with AI, but it is asking me to mock the function, but it suppose to run to increase the coverage