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__?
I used the following workaround to fix this:
create mocks.js:
jest.mock ("electron", () => 
  {
     return { remote: { dialog: { showErrorBox: jest.fn () } } };
  });
in package.json add this to the jest section (path to where your mocks.js is):
"jest":
{
   "setupFiles": [ "<rootDir>/../mocks.js" ]
},
This will globally mock electron, and any other module you declare here, similar to having __mocks__ folder. You could probably put each module in its own file and add to setupFiles array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With