Skip to content

Commit fbbb429

Browse files
committed
add tests for webpack-injected variables (__static and BASE_URL)
1 parent eb8be8c commit fbbb429

File tree

3 files changed

+130
-36
lines changed

3 files changed

+130
-36
lines changed

__tests__/build.helper.js

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const create = require('@vue/cli-test-utils/createTestProject')
2-
const { defaultPreset } = require('@vue/cli/lib/options')
1+
const create = require('./createProject.helper.js')
32
const path = require('path')
43
const fs = require('fs-extra')
54
const Application = require('spectron').Application
@@ -8,24 +7,11 @@ const portfinder = require('portfinder')
87
portfinder.basePort = 9515
98
const runTests = useTS =>
109
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-
}
10+
const { project, projectName } = await create('build', useTS)
11+
2012
const projectPath = p =>
2113
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-
)
14+
2915
const { stdout } = await project.run(
3016
'vue-cli-service build:electron --x64 --win zip --linux zip'
3117
)
@@ -74,12 +60,52 @@ const runTests = useTS =>
7460
// Make sure there are no fatal errors
7561
expect(log.level).not.toBe('SEVERE')
7662
})
63+
let appBaseUrl = logs
64+
// Find BASE_URL log
65+
.find(v => v.message.indexOf('process.env.BASE_URL=') !== -1)
66+
// Get just the value
67+
.message.split('=')[1]
68+
// Remove any quotes
69+
appBaseUrl = appBaseUrl.replace('"', '')
70+
// Base url should be root of server
71+
expect(path.normalize(appBaseUrl)).toBe(
72+
projectPath(
73+
'dist_electron/win-unpacked/resources/app.asar/dist_electron/bundled'
74+
)
75+
)
76+
let appStatic = logs
77+
// Find __static log
78+
.find(v => v.message.indexOf('__static=') !== -1)
79+
// Get just the value
80+
.message.split('=')[1]
81+
// Remove any quotes
82+
appStatic = appStatic.replace('"', '')
83+
// __static should point to public folder
84+
expect(path.normalize(appStatic)).toBe(
85+
projectPath(
86+
'dist_electron/win-unpacked/resources/app.asar/dist_electron/bundled'
87+
)
88+
)
7789
})
7890
await client.getMainProcessLogs().then(logs => {
7991
logs.forEach(log => {
8092
// Make sure there are no fatal errors
8193
expect(log.level).not.toBe('SEVERE')
8294
})
95+
let appStatic = logs
96+
// Find __static log
97+
.find(m => m.indexOf('__static=') !== -1)
98+
// Get just the value
99+
.split('=')[1]
100+
// Remove any quotes
101+
appStatic = appStatic.replace('"', '')
102+
appStatic = appStatic.replace('', '')
103+
// __static should point to public folder
104+
expect(path.normalize(appStatic)).toBe(
105+
projectPath(
106+
'dist_electron/win-unpacked/resources/app.asar/dist_electron/bundled'
107+
)
108+
)
83109
})
84110
// Window was created
85111
expect(await client.getWindowCount()).toBe(1)

__tests__/createProject.helper.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const { defaultPreset } = require('@vue/cli/lib/options')
2+
const create = require('@vue/cli-test-utils/createTestProject')
3+
const path = require('path')
4+
const fs = require('fs-extra')
5+
6+
const createProject = (projectName, useTS) =>
7+
new Promise(async resolve => {
8+
// Prevent modification of import
9+
let preset = { ...defaultPreset }
10+
if (useTS) {
11+
// Install typescript plugin
12+
defaultPreset.plugins['@vue/cli-plugin-typescript'] = {}
13+
// Use different project name
14+
projectName += '-ts'
15+
}
16+
// Install vcp-electron-builder
17+
defaultPreset.plugins['vue-cli-plugin-electron-builder'] = {}
18+
const projectPath = p =>
19+
path.join(process.cwd(), '__tests__/projects/' + projectName, p)
20+
const project = await create(
21+
projectName,
22+
preset,
23+
path.join(process.cwd(), '/__tests__/projects')
24+
)
25+
let backgroundFile = fs.readFileSync(
26+
projectPath(`src/background.${useTS ? 'ts' : 'js'}`),
27+
'utf8'
28+
)
29+
let mainFile = fs.readFileSync(
30+
projectPath(`src/main.${useTS ? 'ts' : 'js'}`),
31+
'utf8'
32+
)
33+
// Have main process log __static to console to make sure it is correct
34+
backgroundFile = backgroundFile.replace(
35+
"const isDevelopment = process.env.NODE_ENV !== 'production'",
36+
`const isDevelopment = process.env.NODE_ENV !== 'production'
37+
${useTS ? 'declare var __static: string' : ''}
38+
console.log('__static=' + __static)`
39+
)
40+
// Have render process log __static and BASE_URL to console to make sure they are correct
41+
mainFile = mainFile.replace(
42+
"import App from './App.vue'",
43+
`import App from './App.vue'
44+
${useTS ? 'declare var __static: string' : ''}
45+
console.log('process.env.BASE_URL=' + process.env.BASE_URL)
46+
console.log('__static=' + __static )`
47+
)
48+
fs.writeFileSync(
49+
projectPath(`src/background.${useTS ? 'ts' : 'js'}`),
50+
backgroundFile
51+
)
52+
fs.writeFileSync(projectPath(`src/main.${useTS ? 'ts' : 'js'}`), mainFile)
53+
resolve({ project, projectName })
54+
})
55+
56+
module.exports = createProject

__tests__/serve.helper.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const create = require('@vue/cli-test-utils/createTestProject')
1+
const create = require('./createProject.helper.js')
22
const path = require('path')
33
const Application = require('spectron').Application
44
const electronPath = require('electron')
5-
const { defaultPreset } = require('@vue/cli/lib/options')
65
const portfinder = require('portfinder')
76

87
portfinder.basePort = 9515
@@ -41,24 +40,9 @@ const serve = (project, notifyUpdate) =>
4140
})
4241
const runTests = useTS =>
4342
new Promise(async resolve => {
44-
// Prevent modification of import
45-
let preset = { ...defaultPreset }
46-
let projectName = 'serve'
47-
if (useTS) {
48-
// Install typescript plugin
49-
defaultPreset.plugins['@vue/cli-plugin-typescript'] = {}
50-
// Use different project name
51-
projectName += '-ts'
52-
}
43+
const { project, projectName } = await create('serve', useTS)
5344
const projectPath = p =>
5445
path.join(process.cwd(), '__tests__/projects/' + projectName, p)
55-
// Install vcp-electron-builder
56-
defaultPreset.plugins['vue-cli-plugin-electron-builder'] = {}
57-
const project = await create(
58-
projectName,
59-
preset,
60-
path.join(process.cwd(), '/__tests__/projects')
61-
)
6246
// Prevent electron from being launched
6347
jest.mock('execa')
6448
// Wait for dev server to start
@@ -87,12 +71,40 @@ const runTests = useTS =>
8771
// Make sure there are no fatal errors
8872
expect(log.level).not.toBe('SEVERE')
8973
})
74+
let appBaseUrl = logs
75+
// Find BASE_URL log
76+
.find(v => v.message.indexOf('process.env.BASE_URL=') !== -1)
77+
// Get just the value
78+
.message.split('=')[1]
79+
// Remove any quotes
80+
appBaseUrl = appBaseUrl.replace('"', '')
81+
// Base url should be root of server
82+
expect(appBaseUrl).toBe('/')
83+
let appStatic = logs
84+
// Find __static log
85+
.find(v => v.message.indexOf('__static=') !== -1)
86+
// Get just the value
87+
.message.split('=')[1]
88+
// Remove any quotes
89+
appStatic = appStatic.replace('"', '')
90+
// __static should point to public folder
91+
expect(path.normalize(appStatic)).toBe(projectPath('public'))
9092
})
9193
await client.getMainProcessLogs().then(logs => {
9294
logs.forEach(log => {
9395
// Make sure there are no fatal errors
9496
expect(log.level).not.toBe('SEVERE')
9597
})
98+
let appStatic = logs
99+
// Find __static log
100+
.find(m => m.indexOf('__static=') !== -1)
101+
// Get just the value
102+
.split('=')[1]
103+
// Remove any quotes
104+
appStatic = appStatic.replace('"', '')
105+
appStatic = appStatic.replace('', '')
106+
// __static should point to public folder
107+
expect(path.normalize(appStatic)).toBe(projectPath('public'))
96108
})
97109
// Window was created
98110
expect(await client.getWindowCount()).toBe(1)

0 commit comments

Comments
 (0)