Skip to content

Commit

Permalink
Add test for multiple HtmlWebpackPlugin instances per review
Browse files Browse the repository at this point in the history
  • Loading branch information
sersorrel committed May 7, 2021
1 parent df3f1c9 commit 8bb9860
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
66 changes: 66 additions & 0 deletions plugin.jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,72 @@ describe('CspHtmlWebpackPlugin', () => {
});
});

it('only inserts hashes for linked scripts and styles from the same HtmlWebpackPlugin instance', (done) => {
const config = createWebpackConfig(
[
new HtmlWebpackPlugin({
filename: path.join(WEBPACK_OUTPUT_DIR, 'index-1.html'),
template: path.join(
__dirname,
'test-utils',
'fixtures',
'external-scripts-styles.html'
),
chunks: ['1'],
}),
new HtmlWebpackPlugin({
filename: path.join(WEBPACK_OUTPUT_DIR, 'index-2.html'),
template: path.join(
__dirname,
'test-utils',
'fixtures',
'external-scripts-styles.html'
),
chunks: ['2'],
}),
new MiniCssExtractPlugin(),
new CspHtmlWebpackPlugin(),
],
undefined,
undefined,
{
entry: {
'1': path.join(__dirname, 'test-utils', 'fixtures', 'index-1.js'),
'2': path.join(__dirname, 'test-utils', 'fixtures', 'index-2.js'),
},
module: {
rules: [
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
],
},
output: {
path: WEBPACK_OUTPUT_DIR,
filename: 'index-[name].bundle.js',
},
}
);

webpackCompile(config, (csps) => {
const expected1 =
"base-uri 'self';" +
" object-src 'none';" +
" script-src 'unsafe-inline' 'self' 'unsafe-eval' 'sha256-Y3RBVJzjgMLd/3xbsXMQc/ZEfadYzG3ndisG/ogf+jQ=' 'nonce-mockedbase64string-1' 'nonce-mockedbase64string-2';" +
" style-src 'unsafe-inline' 'self' 'unsafe-eval' 'nonce-mockedbase64string-3'";
const expected2 =
"base-uri 'self';" +
" object-src 'none';" +
" script-src 'unsafe-inline' 'self' 'unsafe-eval' 'sha256-npoLW6kyIiQHrDdOzxWCi7oMbea1fUsMVFlclhuByTY=' 'nonce-mockedbase64string-4' 'nonce-mockedbase64string-5';" +
" style-src 'unsafe-inline' 'self' 'unsafe-eval' 'nonce-mockedbase64string-6'";

expect(csps['index-1.html']).toEqual(expected1);
expect(csps['index-2.html']).toEqual(expected2);
done();
});
});

it('inserts a custom policy if one is defined', (done) => {
const config = createWebpackConfig([
new HtmlWebpackPlugin({
Expand Down
3 changes: 3 additions & 0 deletions test-utils/fixtures/index-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('./common');

document.body.innerHTML += '<p>index-1.js</p>';
3 changes: 3 additions & 0 deletions test-utils/fixtures/index-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('./common');

document.body.innerHTML += '<p>index-2.js</p>';

0 comments on commit 8bb9860

Please sign in to comment.