Skip to content

Commit eb8be8c

Browse files
committed
add tests for custom plugin options
1 parent c8a6ae1 commit eb8be8c

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const serve = jest.fn(
2+
() =>
3+
new Promise(resolve => {
4+
resolve({ url: 'serveUrl' })
5+
})
6+
)
7+
module.exports.serve = serve

__mocks__/electron-builder.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const builder = jest.genMockFromModule('electron-builder')
2+
builder.build = jest.fn(
3+
() =>
4+
new Promise(resolve => {
5+
resolve()
6+
})
7+
)
8+
module.exports = builder

__mocks__/webpack.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const DefinePlugin = require.requireActual('webpack').DefinePlugin
2+
const EnvironmentPlugin = require.requireActual('webpack').EnvironmentPlugin
3+
// Mock webpack function
4+
const webpack = jest.genMockFromModule('webpack')
5+
// Use unmocked plugins plugins are real
6+
webpack.DefinePlugin = DefinePlugin
7+
webpack.EnvironmentPlugin = EnvironmentPlugin
8+
// Make webpack() return mock run function
9+
const run = jest.fn(callback =>
10+
callback(null, {
11+
toString: jest.fn(),
12+
toJson: jest.fn(),
13+
hasErrors: jest.fn(() => false),
14+
hasWarnings: jest.fn(() => false)
15+
})
16+
)
17+
webpack.mockReturnValue({ run })
18+
// Allow run mock to be accessed in test files
19+
webpack.__run = run
20+
module.exports = webpack

__tests__/commands.spec.js

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
// jest.enableAutomock()
2+
// jest.unmock('bluebird')
3+
// jest.unmock('once')
4+
5+
const pluginIndex = require('../index.js')
6+
const Config = require('webpack-chain')
7+
const webpack = require('webpack')
8+
const builder = require('electron-builder')
9+
const buildRenderer = require('@vue/cli-service/lib/commands/build').build
10+
const fs = require('fs-extra')
11+
jest.mock('@vue/cli-service/lib/commands/build')
12+
jest.mock('fs-extra')
13+
jest.mock('electron-builder')
14+
console.log = jest.fn()
15+
16+
beforeEach(() => {
17+
jest.clearAllMocks()
18+
})
19+
20+
const runCommand = async (command, options = {}, args = { _: [] }) => {
21+
const renderConfig = new Config()
22+
// Command expects define plugin to exist
23+
renderConfig
24+
.plugin('define')
25+
.use(webpack.DefinePlugin, [{ 'process.env': {} }])
26+
let commands = {}
27+
const api = {
28+
// Make app think typescript plugin is installed
29+
hasPlugin: jest.fn().mockReturnValue(true),
30+
resolveChainableWebpackConfig: jest.fn(() => renderConfig),
31+
registerCommand: jest.fn().mockImplementation((name, options, command) => {
32+
commands[name] = command
33+
}),
34+
resolve: jest.fn(path => 'projectPath/' + path)
35+
}
36+
pluginIndex(api, options)
37+
await commands[command](args, [])
38+
}
39+
40+
describe('build:electron', () => {
41+
test('typescript is disabled when set in options', async () => {
42+
await runCommand('build:electron', {
43+
pluginOptions: {
44+
electronBuilder: {
45+
disableMainProcessTypescript: true
46+
}
47+
}
48+
})
49+
50+
const mainConfig = webpack.mock.calls[0][0]
51+
// Typescript rule is not added
52+
expect(Object.keys(mainConfig)).not.toContain('module')
53+
// Ts files are not resolved
54+
expect(
55+
mainConfig.resolve ? mainConfig.resolve.extensions : []
56+
).not.toContain('ts')
57+
// Proper entry file is used
58+
expect(mainConfig.entry.background[0]).toBe('projectPath/src/background.js')
59+
})
60+
61+
test('custom background file is used if provided', async () => {
62+
await runCommand('build:electron', {
63+
pluginOptions: {
64+
electronBuilder: {
65+
mainProcessFile: 'customBackground.js'
66+
}
67+
}
68+
})
69+
70+
const mainConfig = webpack.mock.calls[0][0]
71+
// Proper entry file is used
72+
expect(mainConfig.entry.background[0]).toBe(
73+
'projectPath/customBackground.js'
74+
)
75+
})
76+
77+
test('custom output dir is used if provided', async () => {
78+
await runCommand('build:electron', {
79+
pluginOptions: {
80+
electronBuilder: {
81+
outputDir: 'output'
82+
}
83+
}
84+
})
85+
86+
const mainConfig = webpack.mock.calls[0][0]
87+
// Main config output is correct
88+
expect(mainConfig.output.path).toBe('projectPath/output/bundled')
89+
// Render build output is correct
90+
expect(buildRenderer.mock.calls[0][0].dest).toBe('output/bundled')
91+
// Electron-builder output is correct
92+
expect(builder.build.mock.calls[0][0].config.directories.output).toBe(
93+
'output'
94+
)
95+
})
96+
97+
test('Custom main process webpack config is used if provided', async () => {
98+
await runCommand('build:electron', {
99+
pluginOptions: {
100+
electronBuilder: {
101+
chainWebpackMainProcess: config => {
102+
config.node.set('test', 'expected')
103+
return config
104+
}
105+
}
106+
}
107+
})
108+
const mainConfig = webpack.mock.calls[0][0]
109+
// Custom node key is passed through
110+
expect(mainConfig.node.test).toBe('expected')
111+
})
112+
113+
test('Custom main process webpack config is used if provided', async () => {
114+
await runCommand('build:electron', {
115+
pluginOptions: {
116+
electronBuilder: {
117+
builderOptions: {
118+
testOption: 'expected'
119+
}
120+
}
121+
}
122+
})
123+
// Custom electron-builder config is used
124+
expect(builder.build.mock.calls[0][0].config.testOption).toBe('expected')
125+
})
126+
127+
test('Fonts folder is copied to css if it exists', async () => {
128+
// Mock existence of fonts folder
129+
fs.existsSync.mockReturnValueOnce(true)
130+
await runCommand('build:electron')
131+
// css/fonts folder was created
132+
expect(fs.mkdirSync.mock.calls[0][0]).toBe(
133+
'projectPath/dist_electron/bundled/css/fonts'
134+
)
135+
// fonts was copied to css/fonts
136+
expect(fs.copySync.mock.calls[0][0]).toBe(
137+
'projectPath/dist_electron/bundled/fonts'
138+
)
139+
expect(fs.copySync.mock.calls[0][1]).toBe(
140+
'projectPath/dist_electron/bundled/css/fonts'
141+
)
142+
})
143+
})
144+
145+
describe('serve:electron', () => {
146+
test('typescript is disabled when set in options', async () => {
147+
await runCommand('serve:electron', {
148+
pluginOptions: {
149+
electronBuilder: {
150+
disableMainProcessTypescript: true
151+
}
152+
}
153+
})
154+
155+
const mainConfig = webpack.mock.calls[0][0]
156+
// Typescript rule is not added
157+
expect(Object.keys(mainConfig)).not.toContain('module')
158+
// Ts files are not resolved
159+
expect(
160+
mainConfig.resolve ? mainConfig.resolve.extensions : []
161+
).not.toContain('ts')
162+
// Proper entry file is used
163+
expect(mainConfig.entry.background[0]).toBe('projectPath/src/background.js')
164+
})
165+
166+
test('custom background file is used if provided', async () => {
167+
await runCommand('serve:electron', {
168+
pluginOptions: {
169+
electronBuilder: {
170+
mainProcessFile: 'customBackground.js'
171+
}
172+
}
173+
})
174+
175+
const mainConfig = webpack.mock.calls[0][0]
176+
// Proper entry file is used
177+
expect(mainConfig.entry.background[0]).toBe(
178+
'projectPath/customBackground.js'
179+
)
180+
})
181+
182+
test('custom output dir is used if provided', async () => {
183+
await runCommand('serve:electron', {
184+
pluginOptions: {
185+
electronBuilder: {
186+
outputDir: 'output'
187+
}
188+
}
189+
})
190+
191+
const mainConfig = webpack.mock.calls[0][0]
192+
// Main config output is correct
193+
expect(mainConfig.output.path).toBe('projectPath/output')
194+
})
195+
test('Custom main process webpack config is used if provided', async () => {
196+
await runCommand('serve:electron', {
197+
pluginOptions: {
198+
electronBuilder: {
199+
chainWebpackMainProcess: config => {
200+
config.node.set('test', 'expected')
201+
return config
202+
}
203+
}
204+
}
205+
})
206+
const mainConfig = webpack.mock.calls[0][0]
207+
// Custom node key is passed through
208+
expect(mainConfig.node.test).toBe('expected')
209+
})
210+
})

0 commit comments

Comments
 (0)