|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const RuntimeModule = require("../lib/RuntimeModule"); |
| 4 | +const createHash = require("../lib/util/createHash"); |
| 5 | + |
| 6 | +describe("RuntimeModule", () => { |
| 7 | + class TestRuntimeModule extends RuntimeModule { |
| 8 | + constructor(name, stage) { |
| 9 | + super(name, stage); |
| 10 | + this.code = "var test = 1;"; |
| 11 | + } |
| 12 | + |
| 13 | + generate() { |
| 14 | + return this.code; |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + it("should update hash with name, stage and generated code", () => { |
| 19 | + const m = new TestRuntimeModule("test", 10); |
| 20 | + const hash = createHash("md4"); |
| 21 | + m.updateHash(hash, { |
| 22 | + chunkGraph: { |
| 23 | + getModuleGraphHash: () => "graph-hash" |
| 24 | + } |
| 25 | + }); |
| 26 | + |
| 27 | + const expectedHash = createHash("md4"); |
| 28 | + expectedHash.update("test"); |
| 29 | + expectedHash.update("10"); |
| 30 | + expectedHash.update("var test = 1;"); |
| 31 | + expectedHash.update("graph-hash"); |
| 32 | + |
| 33 | + expect(hash.digest("hex")).toBe(expectedHash.digest("hex")); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should cache generated code during updateHash when not fullHash or dependentHash", () => { |
| 37 | + const m = new TestRuntimeModule("test", 0); |
| 38 | + expect(m._cachedGeneratedCode).toBeUndefined(); |
| 39 | + |
| 40 | + const hash = createHash("md4"); |
| 41 | + m.updateHash(hash, { |
| 42 | + chunkGraph: { |
| 43 | + getModuleGraphHash: () => "" |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + expect(m._cachedGeneratedCode).toBe("var test = 1;"); |
| 48 | + expect(m.getGeneratedCode()).toBe("var test = 1;"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should not cache generated code during updateHash when fullHash is true", () => { |
| 52 | + const m = new TestRuntimeModule("test", 0); |
| 53 | + m.fullHash = true; |
| 54 | + expect(m._cachedGeneratedCode).toBeUndefined(); |
| 55 | + |
| 56 | + const hash = createHash("md4"); |
| 57 | + m.updateHash(hash, { |
| 58 | + chunkGraph: { |
| 59 | + getModuleGraphHash: () => "" |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + expect(m._cachedGeneratedCode).toBeUndefined(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should not cache generated code during updateHash when dependentHash is true", () => { |
| 67 | + const m = new TestRuntimeModule("test", 0); |
| 68 | + m.dependentHash = true; |
| 69 | + expect(m._cachedGeneratedCode).toBeUndefined(); |
| 70 | + |
| 71 | + const hash = createHash("md4"); |
| 72 | + m.updateHash(hash, { |
| 73 | + chunkGraph: { |
| 74 | + getModuleGraphHash: () => "" |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + expect(m._cachedGeneratedCode).toBeUndefined(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should throw when generate() throws during updateHash instead of swallowing error", () => { |
| 82 | + class ThrowingRuntimeModule extends RuntimeModule { |
| 83 | + constructor() { |
| 84 | + super("throwing"); |
| 85 | + } |
| 86 | + |
| 87 | + generate() { |
| 88 | + throw new Error("failed to generate code"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + const m = new ThrowingRuntimeModule(); |
| 93 | + const hash = createHash("md4"); |
| 94 | + |
| 95 | + expect(() => { |
| 96 | + m.updateHash(hash, { |
| 97 | + chunkGraph: { |
| 98 | + getModuleGraphHash: () => "" |
| 99 | + } |
| 100 | + }); |
| 101 | + }).toThrow("failed to generate code"); |
| 102 | + }); |
| 103 | +}); |
0 commit comments