mirror of https://github.com/webpack/webpack.git
test: clean up tests and remove outdated functionality
This commit is contained in:
parent
9c1e12dc02
commit
fb8bd910f0
|
@ -5,7 +5,7 @@
|
|||
// Invalid fetchPriority value - should generate warning
|
||||
const invalidPriorityUrl = new URL(/* webpackPrefetch: true */ /* webpackFetchPriority: "invalid" */ "./assets/images/priority-invalid.png", import.meta.url);
|
||||
|
||||
// Both prefetch and preload specified - should generate warning
|
||||
// Both prefetch and preload specified - no warning anymore (preload takes precedence)
|
||||
const bothHintsUrl = new URL(/* webpackPrefetch: true */ /* webpackPreload: true */ /* webpackFetchPriority: "high" */ "./assets/images/both-hints.png", import.meta.url);
|
||||
|
||||
export default {};
|
||||
|
|
|
@ -2,36 +2,11 @@
|
|||
|
||||
// Warnings are generated in generate-warnings.js to avoid duplication
|
||||
|
||||
// Mock for document.head structure
|
||||
global.document = {
|
||||
head: {
|
||||
_children: [],
|
||||
appendChild: function(element) {
|
||||
this._children.push(element);
|
||||
}
|
||||
},
|
||||
createElement: function(tagName) {
|
||||
const element = {
|
||||
_type: tagName,
|
||||
_attributes: {},
|
||||
setAttribute: function(name, value) {
|
||||
this._attributes[name] = value;
|
||||
// Also set as property for fetchPriority
|
||||
if (name === 'fetchpriority') {
|
||||
this.fetchPriority = value;
|
||||
}
|
||||
},
|
||||
getAttribute: function(name) {
|
||||
return this._attributes[name];
|
||||
}
|
||||
};
|
||||
return element;
|
||||
}
|
||||
};
|
||||
|
||||
// Clear document.head before each test
|
||||
beforeEach(() => {
|
||||
document.head._children = [];
|
||||
if (global.document && global.document.head) {
|
||||
global.document.head._children = [];
|
||||
}
|
||||
});
|
||||
|
||||
it("should generate prefetch link with fetchPriority for new URL() assets", () => {
|
||||
|
|
|
@ -4,9 +4,5 @@ module.exports = [
|
|||
// Invalid fetchPriority value warning
|
||||
[
|
||||
/`webpackFetchPriority` expected "low", "high" or "auto", but received: invalid\./
|
||||
],
|
||||
// Both prefetch and preload specified
|
||||
[
|
||||
/Both webpackPrefetch and webpackPreload are specified\. webpackPreload will take precedence for immediate loading\./
|
||||
]
|
||||
];
|
||||
|
|
|
@ -18,15 +18,6 @@ it("should preload an image asset", () => {
|
|||
expect(url.href).toMatch(/preload-image\.png$/);
|
||||
});
|
||||
|
||||
it("should handle numeric prefetch values with warning", () => {
|
||||
const url = new URL(
|
||||
/* webpackPrefetch: 10 */
|
||||
"./order-image.png",
|
||||
import.meta.url
|
||||
);
|
||||
expect(url.href).toMatch(/order-image\.png$/);
|
||||
});
|
||||
|
||||
it("should preload with fetch priority", () => {
|
||||
const url = new URL(
|
||||
/* webpackPreload: true */
|
||||
|
@ -37,16 +28,6 @@ it("should preload with fetch priority", () => {
|
|||
expect(url.href).toMatch(/priority-image\.png$/);
|
||||
});
|
||||
|
||||
// Warning test cases
|
||||
it("should handle negative prefetch values", () => {
|
||||
const url1 = new URL(
|
||||
/* webpackPrefetch: -1 */
|
||||
"./negative-image.png",
|
||||
import.meta.url
|
||||
);
|
||||
expect(url1.href).toMatch(/negative-image\.png$/);
|
||||
});
|
||||
|
||||
it("should handle invalid fetch priority", () => {
|
||||
const url2 = new URL(
|
||||
/* webpackPreload: true */
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 68 B |
Binary file not shown.
Before Width: | Height: | Size: 68 B |
|
@ -1,16 +1,8 @@
|
|||
"use strict";
|
||||
|
||||
module.exports = [
|
||||
// Numeric prefetch value (not boolean)
|
||||
[/`webpackPrefetch` expected true, but received: 10\./],
|
||||
// Negative prefetch value
|
||||
[/`webpackPrefetch` expected true, but received: -1\./],
|
||||
// Invalid fetch priority
|
||||
[
|
||||
/`webpackFetchPriority` expected "low", "high" or "auto", but received: invalid\./
|
||||
],
|
||||
// Both prefetch and preload specified
|
||||
[
|
||||
/Both webpackPrefetch and webpackPreload are specified\. webpackPreload will take precedence for immediate loading\./
|
||||
]
|
||||
];
|
||||
|
|
|
@ -1,212 +0,0 @@
|
|||
"use strict";
|
||||
|
||||
const URLDependency = require("../../lib/dependencies/URLDependency");
|
||||
const RuntimeGlobals = require("../../lib/RuntimeGlobals");
|
||||
|
||||
describe("URLDependency", () => {
|
||||
describe("Template", () => {
|
||||
const mockSource = {
|
||||
replace: jest.fn()
|
||||
};
|
||||
|
||||
const mockRuntimeTemplate = {
|
||||
moduleRaw: jest.fn().mockReturnValue("__webpack_require__(123)")
|
||||
};
|
||||
|
||||
const mockModuleGraph = {
|
||||
getModule: jest.fn().mockReturnValue({
|
||||
request: "test.png"
|
||||
})
|
||||
};
|
||||
|
||||
const mockChunkGraph = {};
|
||||
const mockRuntimeRequirements = new Set();
|
||||
|
||||
beforeEach(() => {
|
||||
mockSource.replace.mockClear();
|
||||
mockRuntimeTemplate.moduleRaw.mockClear();
|
||||
mockRuntimeRequirements.clear();
|
||||
});
|
||||
|
||||
it("should handle prefetch with fetchPriority", () => {
|
||||
const dep = new URLDependency(
|
||||
"./test.png",
|
||||
[10, 20],
|
||||
[0, 30],
|
||||
"prefetch"
|
||||
);
|
||||
dep.prefetch = true;
|
||||
dep.fetchPriority = "high";
|
||||
|
||||
const template = new URLDependency.Template();
|
||||
template.apply(
|
||||
dep,
|
||||
mockSource,
|
||||
{
|
||||
runtimeTemplate: mockRuntimeTemplate,
|
||||
moduleGraph: mockModuleGraph,
|
||||
chunkGraph: mockChunkGraph,
|
||||
runtimeRequirements: mockRuntimeRequirements
|
||||
}
|
||||
);
|
||||
|
||||
expect(mockRuntimeRequirements.has(RuntimeGlobals.prefetchAsset)).toBe(true);
|
||||
expect(mockRuntimeRequirements.has(RuntimeGlobals.baseURI)).toBe(true);
|
||||
|
||||
const replacementCall = mockSource.replace.mock.calls[0];
|
||||
const replacementCode = replacementCall[2];
|
||||
|
||||
expect(replacementCode).toContain("__webpack_require__.PA(url, \"image\", \"high\");");
|
||||
expect(replacementCode).toContain("new URL(__webpack_require__(123), __webpack_require__.b)");
|
||||
});
|
||||
|
||||
it("should handle preload with fetchPriority", () => {
|
||||
const dep = new URLDependency(
|
||||
"./test.css",
|
||||
[10, 20],
|
||||
[0, 30],
|
||||
"preload"
|
||||
);
|
||||
dep.preload = true;
|
||||
dep.fetchPriority = "low";
|
||||
|
||||
const template = new URLDependency.Template();
|
||||
template.apply(
|
||||
dep,
|
||||
mockSource,
|
||||
{
|
||||
runtimeTemplate: mockRuntimeTemplate,
|
||||
moduleGraph: {
|
||||
getModule: jest.fn().mockReturnValue({
|
||||
request: "test.css"
|
||||
})
|
||||
},
|
||||
chunkGraph: mockChunkGraph,
|
||||
runtimeRequirements: mockRuntimeRequirements
|
||||
}
|
||||
);
|
||||
|
||||
expect(mockRuntimeRequirements.has(RuntimeGlobals.preloadAsset)).toBe(true);
|
||||
|
||||
const replacementCall = mockSource.replace.mock.calls[0];
|
||||
const replacementCode = replacementCall[2];
|
||||
|
||||
expect(replacementCode).toContain("__webpack_require__.LA(url, \"style\", \"low\");");
|
||||
});
|
||||
|
||||
it("should handle both prefetch and preload (preload takes precedence)", () => {
|
||||
const dep = new URLDependency(
|
||||
"./test.js",
|
||||
[10, 20],
|
||||
[0, 30],
|
||||
"both"
|
||||
);
|
||||
dep.prefetch = true;
|
||||
dep.preload = true;
|
||||
dep.fetchPriority = "high";
|
||||
|
||||
const template = new URLDependency.Template();
|
||||
template.apply(
|
||||
dep,
|
||||
mockSource,
|
||||
{
|
||||
runtimeTemplate: mockRuntimeTemplate,
|
||||
moduleGraph: {
|
||||
getModule: jest.fn().mockReturnValue({
|
||||
request: "test.js"
|
||||
})
|
||||
},
|
||||
chunkGraph: mockChunkGraph,
|
||||
runtimeRequirements: mockRuntimeRequirements
|
||||
}
|
||||
);
|
||||
|
||||
// Should only have preload, not prefetch
|
||||
expect(mockRuntimeRequirements.has(RuntimeGlobals.preloadAsset)).toBe(true);
|
||||
expect(mockRuntimeRequirements.has(RuntimeGlobals.prefetchAsset)).toBe(false);
|
||||
|
||||
const replacementCall = mockSource.replace.mock.calls[0];
|
||||
const replacementCode = replacementCall[2];
|
||||
|
||||
expect(replacementCode).toContain("__webpack_require__.LA(url, \"script\", \"high\");");
|
||||
expect(replacementCode).not.toContain("__webpack_require__.PA");
|
||||
});
|
||||
|
||||
it("should handle undefined fetchPriority", () => {
|
||||
const dep = new URLDependency(
|
||||
"./test.png",
|
||||
[10, 20],
|
||||
[0, 30],
|
||||
"prefetch"
|
||||
);
|
||||
dep.prefetch = true;
|
||||
// fetchPriority is undefined
|
||||
|
||||
const template = new URLDependency.Template();
|
||||
template.apply(
|
||||
dep,
|
||||
mockSource,
|
||||
{
|
||||
runtimeTemplate: mockRuntimeTemplate,
|
||||
moduleGraph: mockModuleGraph,
|
||||
chunkGraph: mockChunkGraph,
|
||||
runtimeRequirements: mockRuntimeRequirements
|
||||
}
|
||||
);
|
||||
|
||||
const replacementCall = mockSource.replace.mock.calls[0];
|
||||
const replacementCode = replacementCall[2];
|
||||
|
||||
expect(replacementCode).toContain("__webpack_require__.PA(url, \"image\", undefined);");
|
||||
});
|
||||
|
||||
it("should correctly determine asset types", () => {
|
||||
const testCases = [
|
||||
{ request: "test.png", expectedAs: "image" },
|
||||
{ request: "test.jpg", expectedAs: "image" },
|
||||
{ request: "test.webp", expectedAs: "image" },
|
||||
{ request: "test.css", expectedAs: "style" },
|
||||
{ request: "test.js", expectedAs: "script" },
|
||||
{ request: "test.mjs", expectedAs: "script" },
|
||||
{ request: "test.woff2", expectedAs: "font" },
|
||||
{ request: "test.ttf", expectedAs: "font" },
|
||||
{ request: "test.vtt", expectedAs: "track" },
|
||||
{ request: "test.mp4", expectedAs: "fetch" }, // video uses fetch
|
||||
{ request: "test.json", expectedAs: "fetch" },
|
||||
{ request: "test.wasm", expectedAs: "fetch" }
|
||||
];
|
||||
|
||||
testCases.forEach(({ request, expectedAs }) => {
|
||||
const dep = new URLDependency(
|
||||
`./${request}`,
|
||||
[10, 20],
|
||||
[0, 30],
|
||||
"prefetch"
|
||||
);
|
||||
dep.prefetch = true;
|
||||
dep.fetchPriority = "high";
|
||||
|
||||
const template = new URLDependency.Template();
|
||||
template.apply(
|
||||
dep,
|
||||
mockSource,
|
||||
{
|
||||
runtimeTemplate: mockRuntimeTemplate,
|
||||
moduleGraph: {
|
||||
getModule: jest.fn().mockReturnValue({ request })
|
||||
},
|
||||
chunkGraph: mockChunkGraph,
|
||||
runtimeRequirements: new Set()
|
||||
}
|
||||
);
|
||||
|
||||
const replacementCall = mockSource.replace.mock.calls[0];
|
||||
const replacementCode = replacementCall[2];
|
||||
|
||||
expect(replacementCode).toContain(`__webpack_require__.PA(url, "${expectedAs}", "high");`);
|
||||
|
||||
mockSource.replace.mockClear();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue