Skip to content

Commit b1f85e4

Browse files
committed
feat(testWithSpectron): allow using custom version of spectron
1 parent e3ec07a commit b1f85e4

File tree

6 files changed

+24
-8
lines changed

6 files changed

+24
-8
lines changed

__tests__/commands.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const portfinder = require('portfinder')
1010
const Application = require('spectron').Application
1111
const { chainWebpack, getExternals } = require('../lib/webpackConfig')
1212
const chokidar = require('chokidar')
13+
const spectron = require('spectron')
1314
// #endregion
1415

1516
// #region Mocks
@@ -826,7 +827,7 @@ describe('testWithSpectron', async () => {
826827
}
827828
}
828829
})
829-
const testPromise = testWithSpectron(spectronOptions)
830+
const testPromise = testWithSpectron(spectron, spectronOptions)
830831
// Mock console.log from electron:serve
831832
if (launchOptions.customLog) await sendData(launchOptions.customLog)
832833
await sendData(`$outputDir=${launchOptions.outputDir || 'dist_electron'}`)

generator/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ module.exports = (api, options = {}) => {
110110
devDependencies.electron = electronVersion
111111
}
112112
const dependencies = {}
113+
if (testFramework) {
114+
// Spectron version should be electron version + 2
115+
devDependencies.spectron = '^' + (parseInt(electronVersion.match(/^\^(\d*)\./)[1]) + 2) + '.0.0'
116+
}
113117
if (testFramework === 'mocha') {
114118
dependencies['chai-as-promised'] = '^7.1.1'
115119
}

generator/templates/tests-jest/tests/unit/electron.spec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
/**
2+
* @jest-environment node
3+
*/
4+
const spectron = require('spectron')
15
const { testWithSpectron } = require('vue-cli-plugin-electron-builder')
26
jest.setTimeout(50000)
37

48
test('Window Loads Properly', async () => {
59
// Wait for dev server to start
6-
const { app, stopServe } = await testWithSpectron()
10+
const { app, stopServe } = await testWithSpectron(spectron)
711
const win = app.browserWindow
812
const client = app.client
913

generator/templates/tests-mocha/tests/unit/electron.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import spectron from 'spectron'
12
import testWithSpectron from 'vue-cli-plugin-electron-builder/lib/testWithSpectron'
23
import chai from 'chai'
34
import chaiAsPromised from 'chai-as-promised'
@@ -9,7 +10,7 @@ describe('Application launch', function () {
910
this.timeout(30000)
1011

1112
beforeEach(function () {
12-
return testWithSpectron().then(instance => {
13+
return testWithSpectron(spectron).then(instance => {
1314
this.app = instance.app
1415
this.stopServe = instance.stopServe
1516
})

lib/testWithSpectron.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ const portfinder = require('portfinder')
66

77
portfinder.basePort = 9515
88

9-
module.exports = (options = {}) =>
9+
module.exports = (spectron, options = {}) =>
1010
new Promise(async (resolve, reject) => {
1111
let log = ''
1212
let outputDir = ''
1313
// Launch electron:serve in headless mode
1414
const child = execa(
15-
require.resolve('@vue/cli-service/bin/vue-cli-service'),
16-
['electron:serve', '--headless', '--mode', options.mode || 'test'],
15+
'node',
16+
[
17+
require.resolve('@vue/cli-service/bin/vue-cli-service'),
18+
'electron:serve',
19+
'--headless',
20+
'--mode',
21+
options.mode || 'test'
22+
],
1723
{
1824
env: {
1925
...process.env,
@@ -53,7 +59,7 @@ module.exports = (options = {}) =>
5359
options.spectronOptions
5460
)
5561
// Launch app with spectron
56-
app = new Application(spectronOptions)
62+
app = new spectron.Application(spectronOptions)
5763
if (!options.noStart) {
5864
await app.start()
5965
await app.client.waitUntilWindowLoaded()

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"pumpify": "^1.5.1",
4040
"semver": "^6.0.0",
4141
"shebang-loader": "^0.0.1",
42-
"spectron": "^9.0.0",
4342
"split2": "^3.0.0",
4443
"terser-webpack-plugin": "^2.0.0",
4544
"through2-filter": "^3.0.0",
@@ -70,6 +69,7 @@
7069
"jest": "^24.8.0",
7170
"lnk": "^1.1.0",
7271
"rimraf": "^3.0.0",
72+
"spectron": "^9.0.0",
7373
"terminal-tasks": "^0.0.5",
7474
"typescript": "^3.0.1",
7575
"vue": "^2.5.20",

0 commit comments

Comments
 (0)