|
| 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