Skip to content

Commit 57895c7

Browse files
committed
feat(testWithSpectron): vue add option for testing with spectron
1 parent 86a89a6 commit 57895c7

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

generator/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
const fs = require('fs')
22

33
module.exports = (api, options = {}) => {
4+
if (!options.electronBuilder) options.electronBuilder = {}
45
const usesTS = api.hasPlugin('typescript')
56
const hasBackground =
67
fs.existsSync(api.resolve(`./src/background.ts`)) ||
78
fs.existsSync(api.resolve(`./src/background.js`))
89
if (!hasBackground) {
910
// If user does not have a background file so it should be created
10-
api.render('./template')
11+
api.render('./templates/base')
12+
}
13+
// Add tests
14+
if (options.electronBuilder.addTests) {
15+
let testFramework
16+
// TODO: support mocha
17+
// if (api.hasPlugin('unit-mocha')) testFramework = 'mocha'
18+
if (api.hasPlugin('unit-jest')) testFramework = 'jest'
19+
if (testFramework) api.render(`./templates/tests-${testFramework}`)
1120
}
1221
api.onCreateComplete(() => {
1322
// Update .gitignore if it exists
@@ -63,7 +72,7 @@ module.exports = (api, options = {}) => {
6372
postinstallScript = 'electron-builder install-app-deps'
6473
}
6574
const devDependencies = {}
66-
if (options.electronBuilder && options.electronBuilder.electronVersion) {
75+
if (options.electronBuilder.electronVersion) {
6776
// Use provided electron version
6877
devDependencies.electron = options.electronBuilder.electronVersion
6978
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const { testWithSpectron } = require('vue-cli-plugin-electron-builder')
2+
jest.setTimeout(50000)
3+
4+
test('Window Loads Properly', async () => {
5+
// Wait for dev server to start
6+
const { app, stopServe } = await testWithSpectron()
7+
const win = app.browserWindow
8+
const client = app.client
9+
10+
// Window was created
11+
expect(await client.getWindowCount()).toBe(1)
12+
// It is not minimized
13+
expect(await win.isMinimized()).toBe(false)
14+
// Window is visible
15+
expect(await win.isVisible()).toBe(true)
16+
// Size is correct
17+
const { width, height } = await win.getBounds()
18+
expect(width).toBeGreaterThan(0)
19+
expect(height).toBeGreaterThan(0)
20+
// App is loaded properly
21+
expect(
22+
/Welcome to Your Vue\.js (\+ TypeScript )?App/.test(
23+
await client.getHTML('#app')
24+
)
25+
).toBe(true)
26+
27+
await stopServe()
28+
})

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
testPathIgnorePatterns: [
1616
'/node_modules/',
1717
'/__tests__/projects/',
18+
'/generator/templates',
1819
'.*.helper.js'
1920
]
2021
}

prompts.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,24 @@ module.exports = [
2828
return true
2929
}
3030
}
31+
},
32+
{
33+
name: 'electronBuilder.addTests',
34+
type: 'confirm',
35+
message: 'Add tests with Spectron to your project?',
36+
when: () => {
37+
try {
38+
// Attempt to read package.json
39+
const pkg = require(path.join(process.cwd(), 'package.json'))
40+
// Don't show if electron version is already set
41+
return pkg.devDependencies['@vue/cli-plugin-unit-jest']
42+
// TODO: add support for mocha
43+
// ||
44+
// pkg.devDependencies['@vue/cli-plugin-unit-mocha']
45+
} catch (e) {
46+
console.log('Unable to read package.json')
47+
return false
48+
}
49+
}
3150
}
3251
]

0 commit comments

Comments
 (0)