Skip to content

Commit bce5b7d

Browse files
committed
Add jest config and test environment to launch app
1 parent 736a706 commit bce5b7d

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

jest.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
globals: {
3+
__APP__: undefined, // A different app will be launched in each test environment
4+
},
5+
testEnvironment: "./tests/environment",
6+
testTimeout: 30000, // 30s
7+
};

tests/environment.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const path = require("path");
2+
3+
const getPort = require("get-port");
4+
const NodeEnvironment = require("jest-environment-node");
5+
const { Application } = require("spectron");
6+
7+
class TestEnvironment extends NodeEnvironment {
8+
constructor(config) {
9+
super(config);
10+
}
11+
12+
async setup() {
13+
await super.setup();
14+
15+
const electronPath = path.resolve(
16+
__dirname,
17+
"..",
18+
"node_modules",
19+
".bin",
20+
"electron"
21+
);
22+
const appPath = path.resolve(__dirname, "..");
23+
const port = await getPort();
24+
25+
this.global.__APP__ = new Application({
26+
path: electronPath,
27+
args: [appPath],
28+
port,
29+
});
30+
await this.global.__APP__.start();
31+
const { client } = this.global.__APP__;
32+
await client.waitUntilWindowLoaded();
33+
}
34+
35+
async teardown() {
36+
await this.global.__APP__.stop();
37+
await super.teardown();
38+
}
39+
40+
runScript(script) {
41+
return super.runScript(script);
42+
}
43+
}
44+
45+
module.exports = TestEnvironment;

0 commit comments

Comments
 (0)