Skip to content

Commit 2b28658

Browse files
committed
feat: switch to playwright for testing
1 parent cdc716b commit 2b28658

File tree

8 files changed

+99
-178
lines changed

8 files changed

+99
-178
lines changed

generator/index.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ module.exports = (api, options = {}) => {
1414
if (!hasBackground) {
1515
// If user does not have a background file it should be created
1616
api.render('./templates/base', {
17-
spectronSupport: options.electronBuilder.addTests,
1817
vue3: /^(\^?)3/.test(((pkg || {}).dependencies || {}).vue)
1918
})
2019
}
@@ -99,22 +98,11 @@ module.exports = (api, options = {}) => {
9998
if (usesTS) {
10099
devDependencies['@types/electron-devtools-installer'] = '^2.2.0'
101100
}
102-
const dependencies = {}
103-
if (testFramework) {
104-
// Spectron version should be electron version + 2
105-
devDependencies.spectron =
106-
parseInt(
107-
(electronVersion || pkg.devDependencies.electron).match(/^\^(\d*)\./)[1]
108-
) +
109-
2 +
110-
'.0.0'
111-
}
112101
if (testFramework === 'mocha') {
113102
devDependencies['chai-as-promised'] = '^7.1.1'
114103
}
115104
api.extendPackage({
116105
scripts,
117-
dependencies,
118106
devDependencies
119107
})
120108
}

generator/templates/base/src/background.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ async function createWindow() {
1616
width: 800,
1717
height: 600,
1818
webPreferences: {
19-
<% if (spectronSupport) { %>
20-
// Required for Spectron testing
21-
enableRemoteModule: !!process.env.IS_TEST,
22-
<% } %>
2319
// Use pluginOptions.nodeIntegration, leave this alone
2420
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
2521
nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
11
/**
22
* @jest-environment node
33
*/
4-
import spectron from 'spectron'
5-
import { testWithSpectron } from 'vue-cli-plugin-electron-builder'
4+
import { testWithPlaywright } from 'vue-cli-plugin-electron-builder'
65
jest.setTimeout(50000)
76

87
test('Window Loads Properly', async () => {
98
// Wait for dev server to start
10-
const { app, stopServe } = await testWithSpectron(spectron)
11-
const win = app.browserWindow
12-
const client = app.client
9+
const { app, stop } = await testWithPlaywright()
10+
const win = await app.firstWindow()
11+
await win.waitForLoadState('domcontentloaded')
1312

1413
// Window was created
15-
expect(await client.getWindowCount()).toBe(1)
16-
// It is not minimized
17-
expect(await win.isMinimized()).toBe(false)
18-
// Window is visible
19-
expect(await win.isVisible()).toBe(true)
20-
// Size is correct
21-
const { width, height } = await win.getBounds()
22-
expect(width).toBeGreaterThan(0)
23-
expect(height).toBeGreaterThan(0)
14+
expect(app.windows().length).toBe(1)
2415
// App is loaded properly
2516
expect(
2617
/Welcome to Your Vue\.js (\+ TypeScript )?App/.test(
27-
await (await app.client.$('#app')).getHTML()
18+
await win.innerText('#app')
2819
)
2920
).toBe(true)
3021

31-
await stopServe()
22+
await stop()
3223
})

lib/testWithPlaywright.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const execa = require('execa')
2+
const merge = require('lodash.merge')
3+
const { _electron: electron } = require('playwright')
4+
5+
module.exports = (options = {}) =>
6+
new Promise((resolve, reject) => {
7+
let log = ''
8+
9+
let outputDir = ''
10+
// Launch electron:serve in headless mode
11+
const child = execa(
12+
require.resolve('@vue/cli-service/bin/vue-cli-service', {
13+
paths: [process.cwd()]
14+
}),
15+
['electron:serve', '--headless', '--mode', options.mode || 'production'],
16+
{
17+
extendEnv: false,
18+
env: {
19+
...process.env,
20+
// Extending NODE_ENV causes warnings with build
21+
NODE_ENV: undefined
22+
}
23+
}
24+
)
25+
// Exit if electron:serve throws an error
26+
child.on('error', (err) => {
27+
reject(err)
28+
})
29+
child.stdout.on('data', async (data) => {
30+
data = data.toString()
31+
log += data
32+
const urlMatch = data.match(
33+
/\$WEBPACK_DEV_SERVER_URL=https?:\/\/[^/]+\/?/
34+
)
35+
const outputDirMatch = data.match(/\$outputDir=\b.*\b/)
36+
if (outputDirMatch) {
37+
// Record output dir
38+
outputDir = outputDirMatch[0].split('=')[1]
39+
}
40+
if (urlMatch) {
41+
// Record url and launch app
42+
const url = urlMatch[0].split('=')[1]
43+
let app
44+
if (!options.noPlaywright) {
45+
const launchOptions = merge(
46+
{
47+
args: [`${outputDir}`],
48+
env: {
49+
...process.env,
50+
IS_TEST: true,
51+
WEBPACK_DEV_SERVER_URL: url
52+
}
53+
},
54+
// Apply user options
55+
options.launchOptions
56+
)
57+
// Launch app with playwright
58+
app = await electron.launch(launchOptions)
59+
}
60+
resolve({
61+
stdout: log,
62+
url,
63+
app,
64+
stop: () => {
65+
// Exit serve process
66+
child.stdin.write('close')
67+
child.kill('SIGKILL')
68+
// Close playwright
69+
return app.close()
70+
}
71+
})
72+
}
73+
})
74+
})

lib/testWithSpectron.js

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)