Table of contents

  1. 1. Environment
  2. 2. Code files
  3. 3. Takeaways
    1. 3.1. Checklist
    2. 3.2. Good to know

Write tests for Electron using TypeScript and mocha. The following tutorial will show you how to create a test setup.

Environment

Tested with:

Code files

package.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"devDependencies": {
"@babel/core": "7.3.3",
"@babel/plugin-proposal-class-properties": "7.3.3",
"@babel/preset-env": "7.3.1",
"@babel/preset-typescript": "7.3.3",
"@babel/register": "7.0.0",
"@types/mocha": "5.2.6",
"electron": "4.0.5",
"electron-mocha": "6.0.4",
"typescript": "3.3.3"
},
"main": "dist/main.js",
"scripts": {
"test": "electron-mocha --require ./babel-register.js src/**/*.test.ts"
},
"version": "0.0.0"
}
babel-register.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
require('@babel/register')({
cache: false,
extensions: ['.ts'],
plugins: ['@babel/proposal-class-properties'],
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
'@babel/preset-typescript',
],
});
tsconfig.json
1
2
3
4
5
6
7
8
9
10
11
12
13
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"outDir": "dist",
"rootDir": "src",
"target": "es5"
},
"exclude": [
"dist",
"node_modules"
]
}

Takeaways

Checklist

Good to know