-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.test.js
116 lines (102 loc) · 3.19 KB
/
plugin.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const { expect, test } = require('@jest/globals')
const path = require('path')
const { promisify } = require('util')
const rimraf = promisify(require('rimraf'))
const webpack5 = promisify(require('webpack'))
const webpack4 = promisify(require('webpack-4'))
const { readFile } = require('fs/promises')
beforeAll(() => rimraf(`${__dirname}/dist`))
describe.each([
['webpack-4', webpack4],
['webpack-5', webpack5],
])('%s', (webpackVersion, webpack) => {
async function build(plugins, filename) {
const webpackConfig = {
context: __dirname,
entry: path.resolve(__dirname, '..', 'main.js'),
output: {
path: path.resolve(__dirname, 'dist', webpackVersion),
filename,
},
optimization: {
minimize: false,
},
mode: 'production',
devtool: false,
plugins,
}
return webpack(webpackConfig)
}
async function checkBundleEquality(filename) {
const [actual, expected] = await Promise.all([
readFile(`${__dirname}/dist/${webpackVersion}/${filename}`, 'utf8'),
readFile(`${__dirname}/fixtures/${webpackVersion}/${filename}`, 'utf8'),
])
expect(actual).toEqual(expected)
}
jest.resetModules()
jest.doMock('webpack', () => {
if (webpackVersion === 'webpack-4') return webpack4
if (webpackVersion === 'webpack-5') return webpack5
})
const { IsomorphicEnvWebpackPlugin } = require('../../src/plugin.ts')
test('default config works', async () => {
await build([new IsomorphicEnvWebpackPlugin()], 'default.js')
await checkBundleEquality('default.js')
})
test('variableName option works', async () => {
await build(
[new IsomorphicEnvWebpackPlugin({ variableName: '__SOME_VARIABLE__' })],
'variable-name.js'
)
await checkBundleEquality('variable-name.js')
})
test("validate plugin's options", async () => {
expect.assertions(2)
try {
build(
[new IsomorphicEnvWebpackPlugin({ wrongParameter: true })],
'validation-error.js'
)
} catch (err) {
expect(err.name).toBe('ValidationError')
expect(err.message).toBe(
`Invalid configuration object. IsomorphicEnvWebpackPlugin has been initialized using a configuration object that does not match the API schema.
- configuration has an unknown property 'wrongParameter'. These properties are valid:
object { variableName? }`
)
}
})
test('DefinePlugin before plugin works', async () => {
await build(
[
new webpack.DefinePlugin({
'process.env.FOO': JSON.stringify(
'process.env.FOO before isomorphic plugin'
),
}),
new IsomorphicEnvWebpackPlugin(),
],
'define-plugin-before.js'
)
await checkBundleEquality('define-plugin-before.js')
})
test('webpack has error if DefinePlugin used after plugin', async () => {
const stats = await build(
[
new IsomorphicEnvWebpackPlugin(),
new webpack.DefinePlugin({
'process.env.FOO': JSON.stringify('Replaced FOO by DefinePlugin'),
}),
],
'error-define-plugin-after.js'
)
expect(stats.hasErrors()).toBeTruthy()
expect(stats.compilation.errors.length).toEqual(1)
const error = stats.compilation.errors[0]
expect(error.name).toBe('IsomorphicEnvWebpackPlugin')
expect(error.message).toBe(
"IsomorphicEnvWebpackPlugin — Don't use DefinePlugin after IsomorphicEnvWebpackPlugin"
)
})
})