Skip to content

Commit e99bba0

Browse files
committed
feat(tests): add example test for mocha, fixes #128
1 parent e661a4f commit e99bba0

File tree

7 files changed

+374
-49
lines changed

7 files changed

+374
-49
lines changed

__tests__/testWithSpectron.spec.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
jest.setTimeout(100000)
22

33
const createProject = require('./createProject.helper.js')
4+
const { readFile, writeFile } = require('fs-extra')
5+
const { join } = require('path')
46

5-
test('basic tests pass', async () => {
6-
const { project } = await createProject('spectron', false, {
7-
'@vue/cli-plugin-unit-jest': {}
8-
})
7+
test.each(['mocha', 'jest'])(
8+
'testWithSpectron works with %s',
9+
async testRunner => {
10+
const plugins = {}
11+
plugins[`@vue/cli-plugin-unit-${testRunner}`] = {}
12+
const { project } = await createProject(
13+
`spectron-${testRunner}`,
14+
false,
15+
plugins
16+
)
17+
// Remove example test
18+
await project.rm('tests/unit/example.spec.js')
919

10-
// Update jest config to find test
11-
const config = JSON.parse(await project.read('package.json'))
12-
config.jest.testMatch = ['<rootDir>/tests/unit/spectron.js']
13-
await project.write('package.json', JSON.stringify(config))
20+
// Copy electron test
21+
const testFile = (await readFile(
22+
`./generator/templates/tests-${testRunner}/tests/unit/electron.spec.js`,
23+
'utf8'
24+
))
25+
// Fix some unknown error
26+
.replace('testWithSpectron()', 'testWithSpectron({ mode: "production" })')
27+
await writeFile(join(project.dir, 'tests/unit/electron.spec.js'), testFile)
1428

15-
// Create spectron test
16-
await project.write(
17-
'tests/unit/spectron.js',
18-
`jest.setTimeout(60000)
19-
const { testWithSpectron } = require('vue-cli-plugin-electron-builder')
20-
test('app loads a window', async () => {
21-
const { app, stdout, stopServe } = await testWithSpectron({mode: 'production'})
22-
expect(await app.client.getWindowCount()).toBe(1)
23-
// App is served in production mode
24-
expect(stdout.indexOf('App is served in production mode.')).not.toBe(-1)
25-
await stopServe()
26-
})
27-
`
28-
)
29-
process.env.NODE_ENV = 'production'
30-
await project.run('vue-cli-service test:unit')
31-
})
29+
await project.run('vue-cli-service test:unit')
30+
}
31+
)

generator/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ module.exports = (api, options = {}) => {
77
fs.existsSync(api.resolve(`./src/background.ts`)) ||
88
fs.existsSync(api.resolve(`./src/background.js`))
99
if (!hasBackground) {
10-
// If user does not have a background file so it should be created
10+
// If user does not have a background file it should be created
1111
api.render('./templates/base')
1212
}
1313
// Add tests
14+
let testFramework
1415
if (options.electronBuilder.addTests) {
15-
let testFramework
16-
// TODO: support mocha
17-
// if (api.hasPlugin('unit-mocha')) testFramework = 'mocha'
16+
if (api.hasPlugin('unit-mocha')) testFramework = 'mocha'
1817
if (api.hasPlugin('unit-jest')) testFramework = 'jest'
1918
if (testFramework) api.render(`./templates/tests-${testFramework}`)
2019
}
@@ -89,8 +88,13 @@ module.exports = (api, options = {}) => {
8988
// Use provided electron version
9089
devDependencies.electron = options.electronBuilder.electronVersion
9190
}
91+
const dependencies = {}
92+
if (testFramework === 'mocha') {
93+
dependencies['chai-as-promised'] = '^7.1.1'
94+
}
9295
api.extendPackage({
9396
scripts,
97+
dependencies,
9498
devDependencies,
9599
main: 'background.js'
96100
})
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import testWithSpectron from 'vue-cli-plugin-electron-builder/lib/testWithSpectron'
2+
import chai from 'chai'
3+
import chaiAsPromised from 'chai-as-promised'
4+
5+
chai.should()
6+
chai.use(chaiAsPromised)
7+
8+
describe('Application launch', function () {
9+
this.timeout(30000)
10+
11+
beforeEach(function () {
12+
return testWithSpectron().then(instance => {
13+
this.app = instance.app
14+
this.stopServe = instance.stopServe
15+
})
16+
})
17+
18+
beforeEach(function () {
19+
chaiAsPromised.transferPromiseness = this.app.transferPromiseness
20+
})
21+
22+
afterEach(function () {
23+
if (this.app && this.app.isRunning()) {
24+
return this.stopServe()
25+
}
26+
})
27+
28+
it('opens a window', function () {
29+
return this.app.client
30+
.getWindowCount()
31+
.should.eventually.have.at.least(1)
32+
.browserWindow.isMinimized()
33+
.should.eventually.be.false.browserWindow.isVisible()
34+
.should.eventually.be.true.browserWindow.isFocused()
35+
.should.eventually.be.true.browserWindow.getBounds()
36+
.should.eventually.have.property('width')
37+
.and.be.above(0)
38+
.browserWindow.getBounds()
39+
.should.eventually.have.property('height')
40+
.and.be.above(0)
41+
})
42+
})

lib/webpackConfig.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ async function chainWebpack (api, pluginOptions, config) {
3636
// Apply user config
3737
rendererProcessChain(config)
3838
}
39+
40+
if (process.env.NODE_ENV === 'test') {
41+
// Configure for mocha-webpack
42+
config.module
43+
.rule('shebang')
44+
.test(/\.js$/)
45+
.use('shebang')
46+
.loader('shebang-loader')
47+
config.externals({
48+
'vue-cli-plugin-electron-builder/lib/testWithSpectron':
49+
'require("vue-cli-plugin-electron-builder/lib/testWithSpectron")',
50+
'vue-cli-plugin-electron-builder':
51+
'require("vue-cli-plugin-electron-builder")'
52+
})
53+
}
54+
3955
// Older generated files expect this
4056
process.env.VUE_APP_NODE_MODULES_PATH = false
4157
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"lodash.merge": "^4.6.1",
3636
"portfinder": "^1.0.16",
3737
"pumpify": "^1.5.1",
38+
"shebang-loader": "^0.0.1",
3839
"spectron": "^5.0.0",
3940
"split2": "^3.0.0",
4041
"terser-webpack-plugin": "^1.1.0",
@@ -51,9 +52,12 @@
5152
"@vue/cli-plugin-eslint": "^3.0.1",
5253
"@vue/cli-plugin-typescript": "^3.0.1",
5354
"@vue/cli-plugin-unit-jest": "^3.0.1",
55+
"@vue/cli-plugin-unit-mocha": "^3.4.0",
5456
"@vue/cli-service": "^3.0.1",
5557
"@vue/cli-test-utils": "^3.0.1",
5658
"@vue/eslint-config-typescript": "^3.0.1",
59+
"chai": "^4.2.0",
60+
"chai-as-promised": "^7.1.1",
5761
"electron": "^4.0.0",
5862
"eslint-config-standard": "^12.0.0",
5963
"eslint-plugin-import": "^2.14.0",

prompts.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ module.exports = [
4444
// Attempt to read package.json
4545
const pkg = require(path.join(process.cwd(), 'package.json'))
4646
// Don't show if electron version is already set
47-
return pkg.devDependencies['@vue/cli-plugin-unit-jest']
48-
// TODO: add support for mocha
49-
// ||
50-
// pkg.devDependencies['@vue/cli-plugin-unit-mocha']
47+
return (
48+
pkg.devDependencies['@vue/cli-plugin-unit-jest'] ||
49+
pkg.devDependencies['@vue/cli-plugin-unit-mocha']
50+
)
5151
} catch (e) {
5252
console.log('Unable to read package.json')
5353
return false

0 commit comments

Comments
 (0)