Skip to content

Commit b88c017

Browse files
committed
add serve tests
1 parent 797db23 commit b88c017

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`serve:electron 1`] = `
4+
"<div id=\\"app\\"><img src=\\"/img/logo.82b9c7a5.png\\"><div data-v-656039f0=\\"\\" class=\\"hello\\"><h1 data-v-656039f0=\\"\\">Welcome to Your Vue.js App</h1><p data-v-656039f0=\\"\\">
5+
For guide and recipes on how to configure / customize this project,<br data-v-656039f0=\\"\\">
6+
check out the
7+
<a data-v-656039f0=\\"\\" href=\\"https://cli.vuejs.org\\" target=\\"_blank\\">vue-cli documentation</a>.
8+
</p><h3 data-v-656039f0=\\"\\">Installed CLI Plugins</h3><ul data-v-656039f0=\\"\\"><li data-v-656039f0=\\"\\"><a data-v-656039f0=\\"\\" href=\\"https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-babel\\" target=\\"_blank\\">babel</a></li><li data-v-656039f0=\\"\\"><a data-v-656039f0=\\"\\" href=\\"https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-eslint\\" target=\\"_blank\\">eslint</a></li><li data-v-656039f0=\\"\\"><a data-v-656039f0=\\"\\" href=\\"https://www.npmjs.com/package/vue-cli-plugin-electron-builder\\" target=\\"_blank\\">electron-builder</a></li></ul><h3 data-v-656039f0=\\"\\">Essential Links</h3><ul data-v-656039f0=\\"\\"><li data-v-656039f0=\\"\\"><a data-v-656039f0=\\"\\" href=\\"https://vuejs.org\\" target=\\"_blank\\">Core Docs</a></li><li data-v-656039f0=\\"\\"><a data-v-656039f0=\\"\\" href=\\"https://forum.vuejs.org\\" target=\\"_blank\\">Forum</a></li><li data-v-656039f0=\\"\\"><a data-v-656039f0=\\"\\" href=\\"https://chat.vuejs.org\\" target=\\"_blank\\">Community Chat</a></li><li data-v-656039f0=\\"\\"><a data-v-656039f0=\\"\\" href=\\"https://twitter.com/vuejs\\" target=\\"_blank\\">Twitter</a></li></ul><h3 data-v-656039f0=\\"\\">Ecosystem</h3><ul data-v-656039f0=\\"\\"><li data-v-656039f0=\\"\\"><a data-v-656039f0=\\"\\" href=\\"https://router.vuejs.org\\" target=\\"_blank\\">vue-router</a></li><li data-v-656039f0=\\"\\"><a data-v-656039f0=\\"\\" href=\\"https://vuex.vuejs.org\\" target=\\"_blank\\">vuex</a></li><li data-v-656039f0=\\"\\"><a data-v-656039f0=\\"\\" href=\\"https://github.com/vuejs/vue-devtools#vue-devtools\\" target=\\"_blank\\">vue-devtools</a></li><li data-v-656039f0=\\"\\"><a data-v-656039f0=\\"\\" href=\\"https://vue-loader.vuejs.org\\" target=\\"_blank\\">vue-loader</a></li><li data-v-656039f0=\\"\\"><a data-v-656039f0=\\"\\" href=\\"https://github.com/vuejs/awesome-vue\\" target=\\"_blank\\">awesome-vue</a></li></ul></div></div>"
9+
`;

__tests__/build.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ test('build:electron', async () => {
8484
// App is loaded properly
8585
expect(await client.getHTML('#app')).toMatchSnapshot()
8686

87-
app.stop()
87+
await app.stop()
8888
})

__tests__/serve.spec.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
jest.setTimeout(100000)
2+
3+
const create = require('@vue/cli-test-utils/createTestProject')
4+
const { defaultPreset } = require('@vue/cli/lib/options')
5+
const path = require('path')
6+
const Application = require('spectron').Application
7+
const electronPath = require('electron')
8+
9+
const projectPath = p => path.join(process.cwd(), '__tests__/projects/serve', p)
10+
const serve = (project, notifyUpdate) =>
11+
new Promise((resolve, reject) => {
12+
const child = project.run('vue-cli-service serve:electron')
13+
let isFirstMatch = true
14+
let log = ''
15+
child.stdout.on('data', async data => {
16+
data = data.toString()
17+
log += data
18+
try {
19+
const urlMatch = data.match(/http:\/\/[^/]+\//)
20+
if (urlMatch && isFirstMatch) {
21+
isFirstMatch = false
22+
let url = urlMatch[0]
23+
24+
resolve({
25+
stdout: log,
26+
url,
27+
stopServe: () => {
28+
child.stdin.write('close')
29+
}
30+
})
31+
} else if (data.match(/App updated/)) {
32+
if (notifyUpdate) {
33+
notifyUpdate(data)
34+
}
35+
} else if (data.match(/Failed to compile/)) {
36+
reject(data)
37+
}
38+
} catch (err) {
39+
reject(err)
40+
}
41+
})
42+
})
43+
44+
test('serve:electron', async () => {
45+
let project
46+
// Install vcp-electron-builder
47+
defaultPreset.plugins['vue-cli-plugin-electron-builder'] = {
48+
version: 'file:../../../'
49+
}
50+
project = await create(
51+
'serve',
52+
defaultPreset,
53+
path.join(process.cwd(), '/__tests__/projects')
54+
)
55+
// Prevent electron from being launched
56+
jest.mock('execa')
57+
// Wait for dev server to start
58+
const { stopServe, url } = await serve(project)
59+
expect(project.has('dist_electron/background.js')).toBe(true)
60+
const app = new Application({
61+
path: electronPath,
62+
args: [projectPath('dist_electron/background.js')],
63+
env: {
64+
WEBPACK_DEV_SERVER_URL: url,
65+
IS_TEST: true
66+
},
67+
cwd: projectPath('')
68+
})
69+
70+
await app.start()
71+
const win = app.browserWindow
72+
const client = app.client
73+
await client.waitUntilWindowLoaded()
74+
75+
await client.getRenderProcessLogs().then(logs => {
76+
logs.forEach(log => {
77+
// Make sure there are no fatal errors
78+
expect(log.level).not.toBe('SEVERE')
79+
})
80+
})
81+
await client.getMainProcessLogs().then(logs => {
82+
logs.forEach(log => {
83+
// Make sure there are no fatal errors
84+
expect(log.level).not.toBe('SEVERE')
85+
})
86+
})
87+
// Window was created
88+
expect(await client.getWindowCount()).toBe(1)
89+
// It is not minimized
90+
expect(await win.isMinimized()).toBe(false)
91+
// Window is visible
92+
expect(await win.isVisible()).toBe(true)
93+
// Size is correct
94+
const { width, height } = await win.getBounds()
95+
expect(width).toBeGreaterThan(0)
96+
expect(height).toBeGreaterThan(0)
97+
// App is loaded properly
98+
expect(await client.getHTML('#app')).toMatchSnapshot()
99+
100+
stopServe()
101+
await app.stop()
102+
})

generator/template/src/background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ function createMainWindow () {
1616
const window = new BrowserWindow()
1717

1818
if (isDevelopment) {
19-
window.webContents.openDevTools()
2019
// Load the url of the dev server if in development mode
2120
window.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
21+
if (!process.env.IS_TEST) window.webContents.openDevTools()
2222
} else {
2323
createProtocol('app')
2424
// Load the index.html when not in development

0 commit comments

Comments
 (0)