Best strategy for Typescript with Electron?


I want to migrate everything in Electron to using Typescript.

All renderer code, all main code, and also mocha tests and spectron e2e tests.

So far I’ve written a webpack that works well so I have a proof of concept working.

The main problem I have now is unit testing and spectron and making it work well in my IDE.

My thinking was to have webpack auto-generate a -bundle for each test unit, then run these units via mocha.

That works but it’s a real pain having so many -bundle files all over my disk.

mocha-webpack kind of works there so it’s not the end of the world but it’s not perfect either.

Then spectron magnifies the problem because there are three javascript files I have to webpack there.

What about babel-register? It’s not webpack but it might be easier to work with.



I’m not sure why you would want to write your Mocha tests in TypeScript as well, but have you considered using ts-node to run your test script?


The same reason I want to write Typescript elsewhere… type safety and all the benefits of typescript.


I know one project that uses Mocha tests in TypeScript, maybe you can learn a thing or two from its setup


I’m using TypeScript in Electron successfully with ts-node, except for when I load a program inside of a <webview> element (GitHub issue). Otherwise, using new BrowserWindow seems to work just fine: the entry point will set up ts-node, then the entry point can import the first TypeScript file.


You can do it like this:

yarn add --dev ts-node
yarn mocha --require ts-node/register e2e/spec.ts

That’s what we do here to run Spectron tests written in TS. Or, you can put the ts-node/register requirement to mocha.opts like the aforementioned decaffeinate does.