Skip to content

Commit 8bb9860

Browse files
committed
Add test for multiple HtmlWebpackPlugin instances per review
1 parent df3f1c9 commit 8bb9860

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

plugin.jest.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,72 @@ describe('CspHtmlWebpackPlugin', () => {
186186
});
187187
});
188188

189+
it('only inserts hashes for linked scripts and styles from the same HtmlWebpackPlugin instance', (done) => {
190+
const config = createWebpackConfig(
191+
[
192+
new HtmlWebpackPlugin({
193+
filename: path.join(WEBPACK_OUTPUT_DIR, 'index-1.html'),
194+
template: path.join(
195+
__dirname,
196+
'test-utils',
197+
'fixtures',
198+
'external-scripts-styles.html'
199+
),
200+
chunks: ['1'],
201+
}),
202+
new HtmlWebpackPlugin({
203+
filename: path.join(WEBPACK_OUTPUT_DIR, 'index-2.html'),
204+
template: path.join(
205+
__dirname,
206+
'test-utils',
207+
'fixtures',
208+
'external-scripts-styles.html'
209+
),
210+
chunks: ['2'],
211+
}),
212+
new MiniCssExtractPlugin(),
213+
new CspHtmlWebpackPlugin(),
214+
],
215+
undefined,
216+
undefined,
217+
{
218+
entry: {
219+
'1': path.join(__dirname, 'test-utils', 'fixtures', 'index-1.js'),
220+
'2': path.join(__dirname, 'test-utils', 'fixtures', 'index-2.js'),
221+
},
222+
module: {
223+
rules: [
224+
{
225+
test: /\.css$/,
226+
use: [MiniCssExtractPlugin.loader, 'css-loader'],
227+
},
228+
],
229+
},
230+
output: {
231+
path: WEBPACK_OUTPUT_DIR,
232+
filename: 'index-[name].bundle.js',
233+
},
234+
}
235+
);
236+
237+
webpackCompile(config, (csps) => {
238+
const expected1 =
239+
"base-uri 'self';" +
240+
" object-src 'none';" +
241+
" script-src 'unsafe-inline' 'self' 'unsafe-eval' 'sha256-Y3RBVJzjgMLd/3xbsXMQc/ZEfadYzG3ndisG/ogf+jQ=' 'nonce-mockedbase64string-1' 'nonce-mockedbase64string-2';" +
242+
" style-src 'unsafe-inline' 'self' 'unsafe-eval' 'nonce-mockedbase64string-3'";
243+
const expected2 =
244+
"base-uri 'self';" +
245+
" object-src 'none';" +
246+
" script-src 'unsafe-inline' 'self' 'unsafe-eval' 'sha256-npoLW6kyIiQHrDdOzxWCi7oMbea1fUsMVFlclhuByTY=' 'nonce-mockedbase64string-4' 'nonce-mockedbase64string-5';" +
247+
" style-src 'unsafe-inline' 'self' 'unsafe-eval' 'nonce-mockedbase64string-6'";
248+
249+
expect(csps['index-1.html']).toEqual(expected1);
250+
expect(csps['index-2.html']).toEqual(expected2);
251+
done();
252+
});
253+
});
254+
189255
it('inserts a custom policy if one is defined', (done) => {
190256
const config = createWebpackConfig([
191257
new HtmlWebpackPlugin({

test-utils/fixtures/index-1.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require('./common');
2+
3+
document.body.innerHTML += '<p>index-1.js</p>';

test-utils/fixtures/index-2.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require('./common');
2+
3+
document.body.innerHTML += '<p>index-2.js</p>';

0 commit comments

Comments
 (0)