|
| 1 | +const create = require('@vue/cli-test-utils/createTestProject') |
| 2 | +const { defaultPreset } = require('@vue/cli/lib/options') |
| 3 | +const path = require('path') |
| 4 | +const fs = require('fs-extra') |
| 5 | +const Application = require('spectron').Application |
| 6 | +const portfinder = require('portfinder') |
| 7 | + |
| 8 | +portfinder.basePort = 9515 |
| 9 | +const runTests = useTS => |
| 10 | + new Promise(async resolve => { |
| 11 | + // Prevent modification of import |
| 12 | + let preset = { ...defaultPreset } |
| 13 | + let projectName = 'build' |
| 14 | + if (useTS) { |
| 15 | + // Install typescript plugin |
| 16 | + defaultPreset.plugins['@vue/cli-plugin-typescript'] = {} |
| 17 | + // Use different project name |
| 18 | + projectName += '-ts' |
| 19 | + } |
| 20 | + const projectPath = p => |
| 21 | + path.join(process.cwd(), '__tests__/projects/' + projectName, p) |
| 22 | + // Install vcp-electron-builder |
| 23 | + defaultPreset.plugins['vue-cli-plugin-electron-builder'] = {} |
| 24 | + const project = await create( |
| 25 | + projectName, |
| 26 | + preset, |
| 27 | + path.join(process.cwd(), '/__tests__/projects') |
| 28 | + ) |
| 29 | + const { stdout } = await project.run( |
| 30 | + 'vue-cli-service build:electron --x64 --win zip --linux zip' |
| 31 | + ) |
| 32 | + // Ensure built completes |
| 33 | + expect(stdout.indexOf('Build complete!')).not.toBe(-1) |
| 34 | + // Ensure /dist is not modified |
| 35 | + expect(project.has('dist')).toBe(false) |
| 36 | + // Ensure build successfully outputted files |
| 37 | + expect(project.has('dist_electron/bundled/index.html')).toBe(true) |
| 38 | + expect(project.has('dist_electron/bundled/favicon.ico')).toBe(true) |
| 39 | + expect(project.has('dist_electron/bundled/js')).toBe(true) |
| 40 | + expect(project.has('dist_electron/bundled/css')).toBe(true) |
| 41 | + expect(project.has('dist_electron/bundled/background.js')).toBe(true) |
| 42 | + expect(project.has(`dist_electron/win-unpacked/${projectName}.exe`)).toBe( |
| 43 | + true |
| 44 | + ) |
| 45 | + expect(project.has(`dist_electron/linux-unpacked/${projectName}`)).toBe( |
| 46 | + true |
| 47 | + ) |
| 48 | + // Ensure that setup file was not created |
| 49 | + expect(project.has(`dist_electron/${projectName} Setup 0.1.0.exe`)).toBe( |
| 50 | + false |
| 51 | + ) |
| 52 | + // Ensure that zip files were created |
| 53 | + expect(project.has(`dist_electron/${projectName}-0.1.0-win.zip`)).toBe(true) |
| 54 | + expect(project.has(`dist_electron/${projectName}-0.1.0.zip`)).toBe(true) |
| 55 | + // Ensure base is set properly (for app protocol) |
| 56 | + const index = fs.readFileSync( |
| 57 | + projectPath('dist_electron/bundled/index.html'), |
| 58 | + 'utf8' |
| 59 | + ) |
| 60 | + expect(index.indexOf('<base href=app://./ >')).not.toBe(-1) |
| 61 | + // Launch app with spectron |
| 62 | + const app = new Application({ |
| 63 | + path: `./__tests__/projects/${projectName}/dist_electron/win-unpacked/${projectName}.exe`, |
| 64 | + // Make sure tests do not interfere with each other |
| 65 | + port: await portfinder.getPortPromise() |
| 66 | + }) |
| 67 | + await app.start() |
| 68 | + const win = app.browserWindow |
| 69 | + const client = app.client |
| 70 | + await client.waitUntilWindowLoaded() |
| 71 | + |
| 72 | + await client.getRenderProcessLogs().then(logs => { |
| 73 | + logs.forEach(log => { |
| 74 | + // Make sure there are no fatal errors |
| 75 | + expect(log.level).not.toBe('SEVERE') |
| 76 | + }) |
| 77 | + }) |
| 78 | + await client.getMainProcessLogs().then(logs => { |
| 79 | + logs.forEach(log => { |
| 80 | + // Make sure there are no fatal errors |
| 81 | + expect(log.level).not.toBe('SEVERE') |
| 82 | + }) |
| 83 | + }) |
| 84 | + // Window was created |
| 85 | + expect(await client.getWindowCount()).toBe(1) |
| 86 | + // It is not minimized |
| 87 | + expect(await win.isMinimized()).toBe(false) |
| 88 | + // Dev tools is not open |
| 89 | + expect(await win.isDevToolsOpened()).toBe(false) |
| 90 | + // Window is visible |
| 91 | + expect(await win.isVisible()).toBe(true) |
| 92 | + // Size is correct |
| 93 | + const { width, height } = await win.getBounds() |
| 94 | + expect(width).toBeGreaterThan(0) |
| 95 | + expect(height).toBeGreaterThan(0) |
| 96 | + // Load was successful |
| 97 | + expect(await app.webContents.isLoading()).toBe(false) |
| 98 | + // App is loaded properly |
| 99 | + expect(await client.getHTML('#app')).toMatchSnapshot() |
| 100 | + |
| 101 | + await app.stop() |
| 102 | + resolve() |
| 103 | + }) |
| 104 | + |
| 105 | +module.exports.runTests = runTests |
0 commit comments