|
| 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 | +}) |
0 commit comments