From 23935ec378d6dfc4f8009deb528585fcf8128056 Mon Sep 17 00:00:00 2001 From: "Pelaez, Daniel" Date: Thu, 22 Feb 2024 14:38:59 -0300 Subject: [PATCH] feat: add nonce tests --- test/__snapshots__/nonce.test.js.snap | 37 ++++++++++++++ test/fixtures/no-nonce.js | 2 + test/fixtures/nonce.js | 4 ++ test/nonce.test.js | 70 +++++++++++++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 test/__snapshots__/nonce.test.js.snap create mode 100644 test/fixtures/no-nonce.js create mode 100644 test/fixtures/nonce.js create mode 100644 test/nonce.test.js diff --git a/test/__snapshots__/nonce.test.js.snap b/test/__snapshots__/nonce.test.js.snap new file mode 100644 index 00000000..883f60b2 --- /dev/null +++ b/test/__snapshots__/nonce.test.js.snap @@ -0,0 +1,37 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`nonce should work when __webpack_nonce__ is defined: DOM 1`] = ` +" + style-loader test + + + +

Body

+
+ + + +" +`; + +exports[`nonce should work when __webpack_nonce__ is defined: errors 1`] = `Array []`; + +exports[`nonce should work when __webpack_nonce__ is defined: warnings 1`] = `Array []`; + +exports[`nonce should work when __webpack_nonce__ is not defined: DOM 1`] = ` +" + style-loader test + + + +

Body

+
+ + + +" +`; + +exports[`nonce should work when __webpack_nonce__ is not defined: errors 1`] = `Array []`; + +exports[`nonce should work when __webpack_nonce__ is not defined: warnings 1`] = `Array []`; diff --git a/test/fixtures/no-nonce.js b/test/fixtures/no-nonce.js new file mode 100644 index 00000000..1b9d3ae6 --- /dev/null +++ b/test/fixtures/no-nonce.js @@ -0,0 +1,2 @@ +/* eslint-disable-next-line no-unused-expressions */ +import(/* webpackChunkName: "simple" */ './simple.css'); diff --git a/test/fixtures/nonce.js b/test/fixtures/nonce.js new file mode 100644 index 00000000..f7e89de0 --- /dev/null +++ b/test/fixtures/nonce.js @@ -0,0 +1,4 @@ +__webpack_nonce__ = 'THE_NONCE'; + +/* eslint-disable-next-line no-unused-expressions */ +import(/* webpackChunkName: "simple" */ './simple.css'); diff --git a/test/nonce.test.js b/test/nonce.test.js new file mode 100644 index 00000000..b6a7993c --- /dev/null +++ b/test/nonce.test.js @@ -0,0 +1,70 @@ +/* eslint-env browser */ +import path from "path"; + +import MiniCssExtractPlugin from "../src"; + +import { + compile, + getCompiler, + getErrors, + getWarnings, + runInJsDom, +} from "./helpers/index"; + +describe("nonce", () => { + it(`should work when __webpack_nonce__ is not defined`, async () => { + const compiler = getCompiler( + "no-nonce.js", + {}, + { + mode: "none", + output: { + publicPath: "", + path: path.resolve(__dirname, "../outputs"), + filename: "[name].bundle.js", + }, + plugins: [ + new MiniCssExtractPlugin({ + filename: "[name].css", + }), + ], + } + ); + const stats = await compile(compiler); + + runInJsDom("main.bundle.js", compiler, stats, (dom) => { + expect(dom.serialize()).toMatchSnapshot("DOM"); + }); + + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); + + it(`should work when __webpack_nonce__ is defined`, async () => { + const compiler = getCompiler( + "nonce.js", + {}, + { + mode: "none", + output: { + publicPath: "", + path: path.resolve(__dirname, "../outputs"), + filename: "[name].bundle.js", + }, + plugins: [ + new MiniCssExtractPlugin({ + filename: "[name].css", + }), + ], + } + ); + const stats = await compile(compiler); + + runInJsDom("main.bundle.js", compiler, stats, (dom) => { + expect(dom.serialize()).toMatchSnapshot("DOM"); + }); + + expect(getWarnings(stats)).toMatchSnapshot("warnings"); + expect(getErrors(stats)).toMatchSnapshot("errors"); + }); +});