|
1 |
| -// jest.enableAutomock() |
2 |
| -// jest.unmock('bluebird') |
3 |
| -// jest.unmock('once') |
4 |
| - |
5 | 1 | const pluginIndex = require('../index.js')
|
| 2 | +const testWithSpectron = pluginIndex.testWithSpectron |
6 | 3 | const Config = require('webpack-chain')
|
7 | 4 | const webpack = require('webpack')
|
8 | 5 | const builder = require('electron-builder')
|
9 | 6 | const buildRenderer = require('@vue/cli-service/lib/commands/build').build
|
| 7 | +const serve = require('@vue/cli-service/lib/commands/serve').serve |
10 | 8 | const fs = require('fs-extra')
|
| 9 | +const execa = require('execa') |
| 10 | +const portfinder = require('portfinder') |
| 11 | +const Application = require('spectron').Application |
11 | 12 | jest.mock('@vue/cli-service/lib/commands/build')
|
12 | 13 | jest.mock('fs-extra')
|
13 | 14 | jest.mock('electron-builder')
|
| 15 | +jest.mock('execa', () => jest.fn(() => ({ on: jest.fn() }))) |
| 16 | +jest.mock('@vue/cli-service/lib/commands/serve', () => ({ |
| 17 | + serve: jest.fn().mockResolvedValue({ url: 'serveUrl' }) |
| 18 | +})) |
| 19 | +jest.mock('electron-builder', () => ({ build: jest.fn().mockResolvedValue() })) |
| 20 | +const mockWait = jest.fn(() => new Promise(resolve => resolve())) |
| 21 | +const mockStart = jest.fn() |
| 22 | +jest.mock('spectron', () => ({ |
| 23 | + Application: jest.fn().mockImplementation(() => ({ |
| 24 | + start: mockStart, |
| 25 | + client: { waitUntilWindowLoaded: mockWait } |
| 26 | + })) |
| 27 | +})) |
14 | 28 | console.log = jest.fn()
|
15 | 29 |
|
16 | 30 | beforeEach(() => {
|
17 | 31 | jest.clearAllMocks()
|
18 | 32 | })
|
19 |
| - |
20 |
| -const runCommand = async (command, options = {}, args = { _: [] }) => { |
| 33 | +const runCommand = async (command, options = {}, args = {}) => { |
| 34 | + if (!args._) args._ = [] |
21 | 35 | const renderConfig = new Config()
|
22 | 36 | // Command expects define plugin to exist
|
23 | 37 | renderConfig
|
@@ -207,4 +221,105 @@ describe('serve:electron', () => {
|
207 | 221 | // Custom node key is passed through
|
208 | 222 | expect(mainConfig.node.test).toBe('expected')
|
209 | 223 | })
|
| 224 | + test('If --debug argument is passed, electron is not launched and main process is not minified', async () => { |
| 225 | + await runCommand('serve:electron', {}, { debug: true }) |
| 226 | + const mainConfig = webpack.mock.calls[0][0] |
| 227 | + |
| 228 | + // UglifyJS plugin does not exist |
| 229 | + expect( |
| 230 | + mainConfig.plugins.find( |
| 231 | + p => p.__pluginConstructorName === 'UglifyJsPlugin' |
| 232 | + ) |
| 233 | + ).toBeUndefined() |
| 234 | + // Electron is not launched |
| 235 | + expect(execa).not.toBeCalled() |
| 236 | + }) |
| 237 | + test('If --healdess argument is passed, serve:electron is launched in production mode', async () => { |
| 238 | + await runCommand('serve:electron', {}, { headless: true }) |
| 239 | + |
| 240 | + // Production mode is used |
| 241 | + expect(serve.mock.calls[0][0].mode).toBe('production') |
| 242 | + // Electron is not launched |
| 243 | + expect(execa).not.toBeCalled() |
| 244 | + }) |
| 245 | + test('If --forceDev argument is passed, serve:electron is launched in dev mode', async () => { |
| 246 | + await runCommand('serve:electron', {}, { headless: true, forceDev: true }) |
| 247 | + |
| 248 | + // Production mode is used |
| 249 | + expect(serve.mock.calls[0][0].mode).toBe('development') |
| 250 | + // Electron is not launched |
| 251 | + expect(execa).not.toBeCalled() |
| 252 | + }) |
| 253 | +}) |
| 254 | + |
| 255 | +describe('testWithSpectron', async () => { |
| 256 | + // Mock portfinder's returned port |
| 257 | + portfinder.getPortPromise = jest.fn().mockResolvedValue('expectedPort') |
| 258 | + |
| 259 | + const runSpectron = async (spectronOptions, launchOptions = {}) => { |
| 260 | + let sendData |
| 261 | + execa.mockReturnValueOnce({ |
| 262 | + on: jest.fn(), |
| 263 | + stdout: { |
| 264 | + on: (event, callback) => { |
| 265 | + if (event === 'data') { |
| 266 | + sendData = callback |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + }) |
| 271 | + const testPromise = testWithSpectron(spectronOptions) |
| 272 | + // Mock console.log from serve:elctron |
| 273 | + if (launchOptions.customLog) await sendData(launchOptions.customLog) |
| 274 | + await sendData(`$outputDir=${launchOptions.outputDir || 'dist_electron'}`) |
| 275 | + await sendData( |
| 276 | + `$WEBPACK_DEV_SERVER_URL=${launchOptions.url || 'http://localhost:8080/'}` |
| 277 | + ) |
| 278 | + return testPromise |
| 279 | + } |
| 280 | + |
| 281 | + test('uses custom output dir and url', async () => { |
| 282 | + const { url } = await runSpectron( |
| 283 | + {}, |
| 284 | + { |
| 285 | + url: 'http://localhost:1234/', |
| 286 | + outputDir: 'customOutput' |
| 287 | + } |
| 288 | + ) |
| 289 | + // Proper url is returned |
| 290 | + expect(url).toBe('http://localhost:1234/') |
| 291 | + const appArgs = Application.mock.calls[0][0] |
| 292 | + // Spectron is launched with proper url |
| 293 | + expect(appArgs.env.WEBPACK_DEV_SERVER_URL).toBe('http://localhost:1234/') |
| 294 | + // Spectron is launched with proper path to background |
| 295 | + expect(appArgs.args).toEqual(['customOutput/background.js']) |
| 296 | + }) |
| 297 | + test('secures an open port with portfinder', async () => { |
| 298 | + await runSpectron() |
| 299 | + // Port should match portfinder's mock return value |
| 300 | + expect(Application.mock.calls[0][0].port).toBe('expectedPort') |
| 301 | + }) |
| 302 | + test("doesn't start app if noStart option is provided", async () => { |
| 303 | + await runSpectron({ noStart: true }) |
| 304 | + // App should not be started nor waited for to load |
| 305 | + expect(mockStart).not.toBeCalled() |
| 306 | + expect(mockWait).not.toBeCalled() |
| 307 | + }) |
| 308 | + test("doesn't launch spectron if noSpectron option is provided", async () => { |
| 309 | + await runSpectron({ noSpectron: true }) |
| 310 | + // Spectron instance should not be created |
| 311 | + expect(Application).not.toBeCalled() |
| 312 | + }) |
| 313 | + test('uses custom spectron options if provided', async () => { |
| 314 | + await runSpectron({ spectronOptions: { testKey: 'expected' } }) |
| 315 | + expect(Application.mock.calls[0][0].testKey).toBe('expected') |
| 316 | + }) |
| 317 | + test('launches dev server in dev mode if forceDev argument is provided', async () => { |
| 318 | + await runSpectron({ forceDev: true }) |
| 319 | + expect(execa.mock.calls[0][1]).toContain('--forceDev') |
| 320 | + }) |
| 321 | + test('returns stdout of command', async () => { |
| 322 | + const { stdout } = await runSpectron({}, { customLog: 'shouldBeInLog' }) |
| 323 | + expect(stdout.indexOf('shouldBeInLog')).not.toBe(-1) |
| 324 | + }) |
210 | 325 | })
|
0 commit comments