webpack/test/NormalModule.unittest.js

380 lines
9.2 KiB
JavaScript
Raw Normal View History

2017-02-14 17:38:47 +08:00
"use strict";
2018-01-24 20:17:21 +08:00
2017-02-14 17:38:47 +08:00
const SourceMapSource = require("webpack-sources").SourceMapSource;
const OriginalSource = require("webpack-sources").OriginalSource;
const RawSource = require("webpack-sources").RawSource;
2025-07-03 17:06:45 +08:00
const NormalModule = require("../lib/NormalModule");
2017-02-14 17:38:47 +08:00
2018-01-24 20:17:21 +08:00
describe("NormalModule", () => {
2017-02-14 17:38:47 +08:00
let normalModule;
let request;
let userRequest;
let rawRequest;
let loaders;
let resource;
let parser;
2018-01-24 20:17:21 +08:00
beforeEach(() => {
request = "/some/request";
userRequest = "/some/userRequest";
2017-02-14 17:38:47 +08:00
rawRequest = "some/rawRequest";
loaders = [];
resource = "/some/resource";
2017-02-14 17:38:47 +08:00
parser = {
2017-02-16 05:01:09 +08:00
parse() {}
2017-02-14 17:38:47 +08:00
};
normalModule = new NormalModule({
type: "javascript/auto",
2017-02-14 17:38:47 +08:00
request,
userRequest,
rawRequest,
loaders,
resource,
parser,
generator: null,
resolveOptions: {}
});
2017-12-13 04:21:02 +08:00
normalModule.buildInfo = {
cacheable: true
};
normalModule.useSimpleSourceMap = true;
2017-02-14 17:38:47 +08:00
});
2018-01-24 20:17:21 +08:00
describe("#identifier", () => {
it("returns an identifier for this module", () => {
expect(normalModule.identifier()).toBe(request);
2017-02-14 17:38:47 +08:00
});
2018-01-24 20:17:21 +08:00
it("returns an identifier from toString", () => {
2017-04-04 06:40:13 +08:00
normalModule.debugId = 1000;
expect(normalModule.toString()).toBe("Module[1000: /some/request]");
2017-04-04 06:40:13 +08:00
});
2017-02-14 17:38:47 +08:00
});
2018-01-24 20:17:21 +08:00
describe("#readableIdentifier", () => {
it("calls the given requestShortener with the user request", () => {
2018-05-07 21:26:04 +08:00
const spy = jest.fn();
2017-02-14 17:38:47 +08:00
normalModule.readableIdentifier({
shorten: spy
});
expect(spy.mock.calls).toHaveLength(1);
2018-05-07 21:26:04 +08:00
expect(spy.mock.calls[0][0]).toBe(userRequest);
2017-02-14 17:38:47 +08:00
});
});
2018-01-24 20:17:21 +08:00
describe("#libIdent", () => {
it("contextifies the userRequest of the module", () => {
2018-02-25 18:46:17 +08:00
expect(
normalModule.libIdent({
context: "/some/context"
2018-02-25 18:46:17 +08:00
})
).toBe("../userRequest");
2017-02-14 17:38:47 +08:00
});
2018-01-24 20:17:21 +08:00
describe("given a userRequest containing loaders", () => {
beforeEach(() => {
2018-02-25 18:46:17 +08:00
userRequest =
"/some/userRequest!/some/other/userRequest!/some/thing/is/off/here";
normalModule = new NormalModule({
type: "javascript/auto",
2017-02-14 17:38:47 +08:00
request,
userRequest,
rawRequest,
loaders,
resource,
parser
});
2017-02-14 17:38:47 +08:00
});
2018-01-24 20:17:21 +08:00
it("contextifies every path in the userRequest", () => {
2018-02-25 18:46:17 +08:00
expect(
normalModule.libIdent({
context: "/some/context"
2018-02-25 18:46:17 +08:00
})
).toBe("../userRequest!../other/userRequest!../thing/is/off/here");
2017-02-14 17:38:47 +08:00
});
});
2018-01-24 20:17:21 +08:00
describe("given a userRequest containing query parameters", () => {
it("ignores paths in query parameters", () => {
// cspell:word testpath
2018-02-25 18:46:17 +08:00
userRequest =
"F:\\some\\context\\loader?query=foo\\bar&otherPath=testpath/other";
normalModule = new NormalModule({
type: "javascript/auto",
request,
userRequest,
rawRequest,
loaders,
resource,
parser
});
2018-02-25 18:46:17 +08:00
expect(
normalModule.libIdent({
context: "F:\\some\\context"
2018-02-25 18:46:17 +08:00
})
).toBe("./loader?query=foo\\bar&otherPath=testpath/other");
});
});
2017-02-14 17:38:47 +08:00
});
2018-01-24 20:17:21 +08:00
describe("#nameForCondition", () => {
it("return the resource", () => {
expect(normalModule.nameForCondition()).toBe(resource);
2017-02-14 17:38:47 +08:00
});
2018-01-24 20:17:21 +08:00
describe("given a resource containing a ?-sign", () => {
2017-02-14 17:38:47 +08:00
const baseResource = "some/resource";
2018-01-24 20:17:21 +08:00
beforeEach(() => {
2024-07-31 10:39:30 +08:00
resource = `${baseResource}?some=query`;
normalModule = new NormalModule({
type: "javascript/auto",
2017-02-14 17:38:47 +08:00
request,
userRequest,
rawRequest,
loaders,
resource,
parser
});
2017-02-14 17:38:47 +08:00
});
2018-01-24 20:17:21 +08:00
it("return only the part before the ?-sign", () => {
expect(normalModule.nameForCondition()).toBe(baseResource);
2017-02-14 17:38:47 +08:00
});
});
});
2018-01-24 20:17:21 +08:00
describe("#createSourceForAsset", () => {
2017-02-14 17:38:47 +08:00
let name;
let content;
let sourceMap;
2018-01-24 20:17:21 +08:00
beforeEach(() => {
2017-02-14 17:38:47 +08:00
name = "some name";
content = "some content";
sourceMap = "some sourcemap";
});
2018-01-24 20:17:21 +08:00
describe("given no sourcemap", () => {
it("returns a RawSource", () => {
expect(
normalModule.createSourceForAsset("/", name, content)
).toBeInstanceOf(RawSource);
2017-02-14 17:38:47 +08:00
});
});
2018-01-24 20:17:21 +08:00
describe("given a string as the sourcemap", () => {
it("returns a OriginalSource", () => {
2018-02-25 18:46:17 +08:00
expect(
normalModule.createSourceForAsset("/", name, content, sourceMap)
2018-02-25 18:46:17 +08:00
).toBeInstanceOf(OriginalSource);
2017-02-14 17:38:47 +08:00
});
});
describe("given a some other kind of sourcemap (source maps disabled)", () => {
2018-01-24 20:17:21 +08:00
beforeEach(() => {
2017-02-14 17:38:47 +08:00
sourceMap = () => {};
normalModule.useSimpleSourceMap = false;
});
it("returns a SourceMapSource", () => {
expect(
normalModule.createSourceForAsset("/", name, content, sourceMap)
).toBeInstanceOf(RawSource);
});
});
describe("given a some other kind of sourcemap (simple source maps enabled)", () => {
beforeEach(() => {
sourceMap = () => {};
});
it("returns a SourceMapSource", () => {
expect(
normalModule.createSourceForAsset("/", name, content, sourceMap)
).toBeInstanceOf(RawSource);
});
});
describe("given a some other kind of sourcemap (source maps enabled)", () => {
beforeEach(() => {
sourceMap = () => {};
normalModule.useSourceMap = true;
2017-02-14 17:38:47 +08:00
});
2018-01-24 20:17:21 +08:00
it("returns a SourceMapSource", () => {
2018-02-25 18:46:17 +08:00
expect(
normalModule.createSourceForAsset("/", name, content, sourceMap)
2018-02-25 18:46:17 +08:00
).toBeInstanceOf(SourceMapSource);
2017-02-14 17:38:47 +08:00
});
});
});
2018-01-24 20:17:21 +08:00
describe("#originalSource", () => {
2024-07-31 04:09:42 +08:00
const expectedSource = "some source";
2018-01-24 20:17:21 +08:00
beforeEach(() => {
normalModule._source = new RawSource(expectedSource);
});
2018-01-24 20:17:21 +08:00
it("returns an original Source", () => {
expect(normalModule.originalSource()).toBe(normalModule._source);
});
});
2018-01-24 20:17:21 +08:00
describe("#applyNoParseRule", () => {
let rule;
let content;
2018-01-24 20:17:21 +08:00
describe("given a string as rule", () => {
beforeEach(() => {
rule = "some-rule";
});
2018-01-24 20:17:21 +08:00
describe("and the content starting with the string specified in rule", () => {
beforeEach(() => {
2024-07-31 10:39:30 +08:00
content = `${rule}some-content`;
});
2018-01-24 20:17:21 +08:00
it("returns true", () => {
expect(normalModule.shouldPreventParsing(rule, content)).toBe(true);
});
});
2018-01-24 20:17:21 +08:00
describe("and the content does not start with the string specified in rule", () => {
beforeEach(() => {
content = "some-content";
});
2018-01-24 20:17:21 +08:00
it("returns false", () => {
expect(normalModule.shouldPreventParsing(rule, content)).toBe(false);
});
});
});
2018-01-24 20:17:21 +08:00
describe("given a regex as rule", () => {
beforeEach(() => {
rule = /some-rule/;
});
2018-01-24 20:17:21 +08:00
describe("and the content matches the rule", () => {
beforeEach(() => {
2024-07-31 10:39:30 +08:00
content = `${rule}some-content`;
});
2018-01-24 20:17:21 +08:00
it("returns true", () => {
expect(normalModule.shouldPreventParsing(rule, content)).toBe(true);
});
});
2018-01-24 20:17:21 +08:00
describe("and the content does not match the rule", () => {
beforeEach(() => {
content = "some-content";
});
2018-01-24 20:17:21 +08:00
it("returns false", () => {
expect(normalModule.shouldPreventParsing(rule, content)).toBe(false);
});
});
});
});
2018-01-24 20:17:21 +08:00
describe("#shouldPreventParsing", () => {
let applyNoParseRuleSpy;
2018-01-24 20:17:21 +08:00
beforeEach(() => {
2018-05-07 21:26:04 +08:00
applyNoParseRuleSpy = jest.fn();
normalModule.applyNoParseRule = applyNoParseRuleSpy;
});
2018-01-24 20:17:21 +08:00
describe("given no noParseRule", () => {
it("returns false", () => {
expect(normalModule.shouldPreventParsing()).toBe(false);
expect(applyNoParseRuleSpy.mock.calls).toHaveLength(0);
});
});
2018-01-24 20:17:21 +08:00
describe("given a noParseRule", () => {
let returnValOfSpy;
2018-01-24 20:17:21 +08:00
beforeEach(() => {
2017-02-16 05:01:09 +08:00
returnValOfSpy = true;
2018-05-07 21:26:04 +08:00
applyNoParseRuleSpy.mockReturnValue(returnValOfSpy);
});
2018-01-24 20:17:21 +08:00
describe("that is a string", () => {
it("calls and returns whatever applyNoParseRule returns", () => {
2018-02-25 18:46:17 +08:00
expect(normalModule.shouldPreventParsing("some rule")).toBe(
returnValOfSpy
);
expect(applyNoParseRuleSpy.mock.calls).toHaveLength(1);
});
});
2018-01-24 20:17:21 +08:00
describe("that is a regex", () => {
it("calls and returns whatever applyNoParseRule returns", () => {
2018-02-25 18:46:17 +08:00
expect(normalModule.shouldPreventParsing("some rule")).toBe(
returnValOfSpy
);
expect(applyNoParseRuleSpy.mock.calls).toHaveLength(1);
});
});
2018-01-24 20:17:21 +08:00
describe("that is an array", () => {
describe("of strings and or regexps", () => {
let someRules;
2018-01-24 20:17:21 +08:00
beforeEach(() => {
2018-02-25 18:46:17 +08:00
someRules = ["some rule", /some rule1/, "some rule2"];
});
2018-01-24 20:17:21 +08:00
describe("and none of them match", () => {
beforeEach(() => {
returnValOfSpy = false;
2018-05-07 21:26:04 +08:00
applyNoParseRuleSpy.mockReturnValue(returnValOfSpy);
});
2018-01-24 20:17:21 +08:00
it("returns false", () => {
2018-02-25 18:46:17 +08:00
expect(normalModule.shouldPreventParsing(someRules)).toBe(
returnValOfSpy
);
expect(applyNoParseRuleSpy.mock.calls).toHaveLength(3);
});
});
2018-01-24 20:17:21 +08:00
describe("and the first of them matches", () => {
beforeEach(() => {
returnValOfSpy = true;
2018-05-07 21:26:04 +08:00
applyNoParseRuleSpy.mockReturnValue(returnValOfSpy);
});
2018-01-24 20:17:21 +08:00
it("returns true", () => {
2018-02-25 18:46:17 +08:00
expect(normalModule.shouldPreventParsing(someRules)).toBe(
returnValOfSpy
);
expect(applyNoParseRuleSpy.mock.calls).toHaveLength(1);
});
});
2018-01-24 20:17:21 +08:00
describe("and the last of them matches", () => {
beforeEach(() => {
returnValOfSpy = true;
2018-05-07 21:26:04 +08:00
applyNoParseRuleSpy.mockReturnValueOnce(false);
applyNoParseRuleSpy.mockReturnValueOnce(false);
applyNoParseRuleSpy.mockReturnValue(true);
});
2018-01-24 20:17:21 +08:00
it("returns true", () => {
2018-02-25 18:46:17 +08:00
expect(normalModule.shouldPreventParsing(someRules)).toBe(
returnValOfSpy
);
expect(applyNoParseRuleSpy.mock.calls).toHaveLength(3);
});
});
});
});
});
});
2017-02-14 17:38:47 +08:00
});