Skip to content

Commit a080fbd

Browse files
test(concatenate-modules): snapshot the emitted bundles instead of pinning substrings
The generated code is what these cases check, so review it as a whole rather than one matched fragment at a time. Runtime assertions stay.
1 parent 9f6628e commit a080fbd

12 files changed

Lines changed: 1864 additions & 42 deletions

File tree

‎test/configCases/concatenate-modules/commonjs-wrapped-external-new-expression/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 429 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases concatenate-modules commonjs-wrapped-external-new-expression commonjs-wrapped-external-new-expression should compile 1`] = `
4+
"/******/ (() => { // webpackBootstrap
5+
/******/ \\"use strict\\";
6+
/******/ // The require scope
7+
/******/ const __webpack_require__ = {};
8+
/******/
9+
/************************************************************************/
10+
/******/ /* webpack/runtime/concatenation wrap */
11+
/******/ // wrap a concatenated module body as a lazy, memoized accessor; mod is
12+
/******/ // set before the body runs so re-entrant calls (require cycles) observe
13+
/******/ // the partial exports like Node.js
14+
/******/ __webpack_require__.cw = (body) => {
15+
/******/ var mod;
16+
/******/ return () => {
17+
/******/ if (body) {
18+
/******/ var fn = body;
19+
/******/ body = 0;
20+
/******/ mod = { exports: {} };
21+
/******/ fn.call(mod.exports, mod, mod.exports);
22+
/******/ }
23+
/******/ return mod.exports;
24+
/******/ };
25+
/******/ };
26+
/******/
27+
/******/ /* webpack/runtime/define property getters */
28+
/******/ // define getter/value functions for harmony exports
29+
/******/ __webpack_require__.d = (exports, definition) => {
30+
/******/ if(Array.isArray(definition)) {
31+
/******/ var i = 0;
32+
/******/ while(i < definition.length) {
33+
/******/ var key = definition[i++];
34+
/******/ var binding = definition[i++];
35+
/******/ var descriptor = binding === 0 ? { enumerable: true, value: definition[i++] } : { enumerable: true, get: binding };
36+
/******/ if(!__webpack_require__.o(exports, key)) Object.defineProperty(exports, key, descriptor);
37+
/******/ }
38+
/******/ } else {
39+
/******/ for(var key in definition) {
40+
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
41+
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
42+
/******/ }
43+
/******/ }
44+
/******/ }
45+
/******/ };
46+
/******/
47+
/******/ /* webpack/runtime/hasOwnProperty shorthand */
48+
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop));
49+
/******/
50+
/************************************************************************/
51+
/*!******************************!*\\\\
52+
!*** ./index.js + 3 modules ***!
53+
\\\\******************************/
54+
55+
// MODULE: external \\"events\\"
56+
var external_events_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(module, exports) {
57+
module.exports = require(\\"events\\");
58+
});
59+
60+
// MODULE: ./mid.cjs
61+
var mid_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(module, exports) {
62+
63+
64+
const { make, raw } = (esm_user_namespaceFn());
65+
66+
exports.L = make;
67+
exports.l = raw;
68+
69+
});
70+
71+
// MODULE: ./esm-user.mjs
72+
var esm_user_namespaceFn = /*#__PURE__*/__webpack_require__.cw(function(__webpack_module__, __webpack_exports__) {
73+
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
74+
/* harmony export */ make: () => (/* binding */ make)
75+
/* harmony export */ });
76+
(external_events_namespaceFn());
77+
// required by a CommonJS module, so wrapping propagates from here to the
78+
// external: the default import becomes the external's accessor call
79+
80+
81+
function make() {
82+
return new (external_events_namespaceFn())();
83+
}
84+
85+
const raw = (external_events_namespaceFn());
86+
87+
/* harmony export */ __webpack_require__.d(__webpack_exports__, [
88+
/* harmony export */ \\"raw\\", 0, /* binding */ raw
89+
/* harmony export */ ]);
90+
91+
});
92+
93+
;// ./mid.cjs
94+
mid_namespaceFn();
95+
96+
;// ./index.js
97+
98+
99+
it(\\"should apply \`new\` to a wrapped external's accessor result\\", () => {
100+
const emitter = (0,mid_namespaceFn().L)();
101+
102+
expect(emitter).toBeInstanceOf((mid_namespaceFn().l));
103+
expect(typeof emitter.on).toBe(\\"function\\");
104+
});
105+
106+
/******/ })()
107+
;"
108+
`;
Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { readFileSync } from "fs";
21
import { make, raw } from "./mid.cjs";
32

43
it("should apply `new` to a wrapped external's accessor result", () => {
@@ -7,12 +6,3 @@ it("should apply `new` to a wrapped external's accessor result", () => {
76
expect(emitter).toBeInstanceOf(raw);
87
expect(typeof emitter.on).toBe("function");
98
});
10-
11-
it("should really reach the external through a wrapper accessor", () => {
12-
const source = readFileSync(__filename, "utf-8");
13-
14-
expect(source).toMatch(
15-
/external_events_namespaceFn = \/\*#__PURE__\*\/__webpack_require__\.cw\(/
16-
);
17-
expect(source).toMatch(/return new \(external_events_namespaceFn\(\)\)\(\)/);
18-
});

‎test/configCases/concatenate-modules/commonjs-wrapped-external-new-expression/webpack.config.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"use strict";
22

3+
const PLUGIN_NAME = "SnapshotBundlePlugin";
4+
35
/** @type {import("../../../../").Configuration} */
46
module.exports = {
57
mode: "production",
@@ -11,5 +13,19 @@ module.exports = {
1113
usedExports: true,
1214
moduleIds: "named",
1315
chunkIds: "named"
14-
}
16+
},
17+
plugins: [
18+
/**
19+
* `new` on a wrapped module's accessor call is printed code, so the whole
20+
* emitted bundle is reviewed as one rather than pinned substring by substring.
21+
* @param {import("../../../../").Compiler} compiler compiler
22+
*/
23+
(compiler) => {
24+
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
25+
compilation.hooks.afterProcessAssets.tap(PLUGIN_NAME, (assets) => {
26+
expect(assets["bundle0.js"].source()).toMatchSnapshot();
27+
});
28+
});
29+
}
30+
]
1531
};

0 commit comments

Comments
 (0)