Skip to content

Commit f604d13

Browse files
committed
remove useles env code and fix test env
1 parent c85c612 commit f604d13

File tree

6 files changed

+36
-76
lines changed

6 files changed

+36
-76
lines changed

__tests__/commands.spec.js

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -170,26 +170,6 @@ describe('build:electron', () => {
170170
'projectPath/dist_electron/bundled/css/fonts'
171171
)
172172
})
173-
174-
test('--mode argument takes precedence over other options', async () => {
175-
await runCommand(
176-
'build:electron',
177-
{ pluginOptions: { electronBuilder: { buildMode: 'production' } } },
178-
{ headless: true, mode: 'development' }
179-
)
180-
181-
// Development mode is used
182-
expect(buildRenderer.mock.calls[0][0].mode).toBe('development')
183-
})
184-
185-
test('buildMode sets Vue mode if provided', async () => {
186-
await runCommand('build:electron', {
187-
pluginOptions: { electronBuilder: { buildMode: 'development' } }
188-
})
189-
190-
// Development mode is used
191-
expect(buildRenderer.mock.calls[0][0].mode).toBe('development')
192-
})
193173
})
194174

195175
describe('serve:electron', () => {
@@ -289,41 +269,6 @@ describe('serve:electron', () => {
289269
// Electron is not launched
290270
expect(execa).not.toBeCalled()
291271
})
292-
test('If --headless argument is passed, serve:electron is launched in production mode', async () => {
293-
await runCommand(
294-
'serve:electron',
295-
{
296-
// It should take precedence over plugin options
297-
pluginOptions: { electronBuilder: { serveMode: 'production' } }
298-
},
299-
{ headless: true }
300-
)
301-
302-
// Production mode is used
303-
expect(serve.mock.calls[0][0].mode).toBe('production')
304-
// Electron is not launched
305-
expect(execa).not.toBeCalled()
306-
})
307-
308-
test('--mode argument takes precedence over other options', async () => {
309-
await runCommand(
310-
'serve:electron',
311-
{ pluginOptions: { electronBuilder: { serveMode: 'production' } } },
312-
{ headless: true, mode: 'development' }
313-
)
314-
315-
// Development mode is used
316-
expect(serve.mock.calls[0][0].mode).toBe('development')
317-
})
318-
319-
test('serveMode sets Vue mode if provided', async () => {
320-
await runCommand('serve:electron', {
321-
pluginOptions: { electronBuilder: { serveMode: 'production' } }
322-
})
323-
324-
// Development mode is used
325-
expect(serve.mock.calls[0][0].mode).toBe('production')
326-
})
327272
})
328273

329274
describe('testWithSpectron', async () => {
@@ -386,12 +331,30 @@ describe('testWithSpectron', async () => {
386331
await runSpectron({ spectronOptions: { testKey: 'expected' } })
387332
expect(Application.mock.calls[0][0].testKey).toBe('expected')
388333
})
334+
test('launches dev server in production mode if forceDev argument is not provided', async () => {
335+
await runSpectron()
336+
337+
// Node env was set to production
338+
expect(execa.mock.calls[0][2].env.NODE_ENV).toBe('production')
339+
})
389340
test('launches dev server in dev mode if forceDev argument is provided', async () => {
390341
await runSpectron({ forceDev: true })
391342

392-
// Mode argument was set to development
343+
// Node env was set to development
344+
expect(execa.mock.calls[0][2].env.NODE_ENV).toBe('development')
345+
})
346+
test('default vue mode is test', async () => {
347+
await runSpectron()
348+
349+
// Mode argument was set to test
350+
expect(execa.mock.calls[0][1].join(' ').indexOf('--mode test')).not.toBe(-1)
351+
})
352+
test('custom vue mode is used if provided', async () => {
353+
await runSpectron({ mode: 'expected' })
354+
355+
// Mode argument was set to expected
393356
expect(
394-
execa.mock.calls[0][1].join(' ').indexOf('--mode development')
357+
execa.mock.calls[0][1].join(' ').indexOf('--mode expected')
395358
).not.toBe(-1)
396359
})
397360
test('returns stdout of command', async () => {

__tests__/testWithSpectron.spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ test('basic tests pass', async () => {
1818
`jest.setTimeout(60000)
1919
const { testWithSpectron } = require('vue-cli-plugin-electron-builder')
2020
test('app loads a window', async () => {
21-
const { app, stopServe } = await testWithSpectron()
21+
const { app, stdout, stopServe } = await testWithSpectron({mode: 'production'})
2222
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)
2325
await stopServe()
2426
})
2527
`
2628
)
29+
process.env.NODE_ENV = 'production'
2730
await project.run('vue-cli-service test:unit')
2831
})

docs/guide/testingAndDebugging.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ testWithSpectron({
5555
noSpectron: false // Disables launching of Spectron. Enable this if you want to launch spectron yourself.
5656
noStart: false // Do not start Spectron app or wait for it to load. You will have to call app.start() and app.client.waitUntilWindowLoaded() before running any tests.
5757
forceDev: false // Run dev server in development mode. By default it is run in production (serve --mode production).
58+
mode: 'test', // Set custom Vue env mode.
5859
spectronOptions: {} // Custom options to be passed to Spectron. Defaults are already set, only use this if you need something customized.
5960
})
6061
```

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ interface Options {
1313
You will have to run app.start() and app.client.waitUntilWindowLoaded() yourself.
1414
*/
1515
noStart: boolean
16+
/** Set custom Vue env mode. Defaults to 'test' */
17+
mode: string
1618
}
1719
interface Server {
1820
/** Spectron instance. */

index.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ module.exports = (api, options) => {
3636
const buildRenderer = require('@vue/cli-service/lib/commands/build').build
3737
const fs = require('fs-extra')
3838
const builder = require('electron-builder')
39-
const mode = args.mode || pluginOptions.buildMode
4039
const yargs = require('yargs')
4140
// Import the yargs options from electron-builder
4241
const configureBuildCommand = require('electron-builder/out/builder')
@@ -77,9 +76,7 @@ module.exports = (api, options) => {
7776
// Make sure files are outputted to proper directory
7877
dest: outputDir + '/bundled',
7978
// Enable modern mode
80-
modern: true,
81-
// Apply mode if provided
82-
...(mode ? { mode } : {})
79+
modern: true
8380
}
8481
const mainConfig = new Config()
8582
// Configure main process webpack config
@@ -194,12 +191,6 @@ module.exports = (api, options) => {
194191
args => {
195192
const execa = require('execa')
196193
const serve = require('@vue/cli-service/lib/commands/serve').serve
197-
// --mode > headless > pluginOptions > defaults
198-
const mode =
199-
args.mode ||
200-
(args.headless ? 'production' : null) ||
201-
pluginOptions.serveMode
202-
console.log(mode)
203194
const rendererConfig = api.resolveChainableWebpackConfig()
204195
// Configure renderer process to work properly with electron
205196
rendererConfig
@@ -256,9 +247,7 @@ module.exports = (api, options) => {
256247
{
257248
_: [],
258249
// Use dashboard if called from ui
259-
dashboard: args.dashboard,
260-
// Apply mode if provided
261-
...(mode ? { mode } : {})
250+
dashboard: args.dashboard
262251
},
263252
api,
264253
options,

lib/testWithSpectron.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ module.exports = (options = {}) =>
1313
// Launch serve:electron in headless mode
1414
const child = execa(
1515
require.resolve('@vue/cli-service/bin/vue-cli-service'),
16-
[
17-
'serve:electron',
18-
'--headless',
19-
...(options.forceDev ? ['--mode', 'development'] : [])
20-
]
16+
['serve:electron', '--headless', '--mode', options.mode || 'test'],
17+
{
18+
env: {
19+
...process.env,
20+
NODE_ENV: !options.forceDev ? 'production' : 'development'
21+
}
22+
}
2123
)
2224
// Exit if serve:electron throws an error
2325
child.on('error', err => {

0 commit comments

Comments
 (0)