I had a functioning Jest/Flow setup for my application. We switched to TypeScript, and all of my tests broke. I converted everything over to .ts and .test.ts and fixed all the bugs. For some reason, none of my __mocks__ are used anymore. ( I had to mock some modules that were failing to automock)
For example, the below code used to mock electron any time it was required, and allow the code to invoke mocked dialogs so i could check that error cases reported errors. Since i converted to TypeScript, any time the require ("electron") is hit in a test, it fails saying remote is undefined.
ex) aFile.test.ts
import reportError from "../aFile.ts";
const { dialog } = require ("electron").remote;
describe ("reportError", () =>
{
test ("creates dialog", () =>
{
const title = "foo";
const message = "bar";
reportError (title, message);
expect (dialog.showErrorBox).toHaveBeenLastCalledWith (title, message);
});
});
ex) aFile.ts
const { dialog } = require ("electron").remote;
export default function reportError (title: string, message: string)
{
dialog.showErrorBox (title, message);
}
ex) __mocks__/electron.js (sibling of node_modules)
module.exports = { remote: { dialog: { showErrorBox: jest.fn () } } };
I know for certain the mock isn't being used, because when i add the following to any failing .test.ts file, it starts to pass:
jest.mock ("electron", () => { return { remote: { dialog: { showErrorBox: jest.fn () } } } });
Why isn't TypeScript finding my __mocks__?
require('electron')is getting obscured from the jest parser and isn't detected when jest hoists the imports. This would likely require a deep dive into jest’s source.module.exportsnotation, and I'm not sure how well that would play with Typescript. Might I suggest rewriting your mock in Typescript and seeing if that works without thesetupFilessetting you added?