|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import { type StatsAsset, type Compilation } from "webpack"; |
| 3 | + |
| 4 | +import { processAssets } from "../processAssets"; |
| 5 | + |
| 6 | +describe("processAssets", () => { |
| 7 | + describe("there are output options", () => { |
| 8 | + it("should process assets", async () => { |
| 9 | + const assets = [ |
| 10 | + { |
| 11 | + name: "file1.js", |
| 12 | + size: 1000, |
| 13 | + }, |
| 14 | + { |
| 15 | + name: "file2.js", |
| 16 | + size: 2000, |
| 17 | + }, |
| 18 | + ] as StatsAsset[]; |
| 19 | + |
| 20 | + const compilation = { |
| 21 | + hooks: {}, |
| 22 | + outputOptions: { |
| 23 | + filename: "filename", |
| 24 | + assetModuleFilename: "assetModuleFilename", |
| 25 | + chunkFilename: "chunkFilename", |
| 26 | + cssFilename: "cssFilename", |
| 27 | + cssChunkFilename: "cssChunkFilename", |
| 28 | + }, |
| 29 | + getAsset: vi.fn().mockReturnValue({ |
| 30 | + source: { |
| 31 | + source: vi.fn().mockReturnValue("source"), |
| 32 | + }, |
| 33 | + }), |
| 34 | + // This is a fairly complex type to mock out, so we're just using the values that are needed for this test |
| 35 | + } as unknown as Compilation; |
| 36 | + |
| 37 | + const processedAssets = await processAssets({ assets, compilation }); |
| 38 | + |
| 39 | + expect(processedAssets).toEqual([ |
| 40 | + { |
| 41 | + name: "file1.js", |
| 42 | + normalized: "file1.js", |
| 43 | + size: 1000, |
| 44 | + gzipSize: 26, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "file2.js", |
| 48 | + normalized: "file2.js", |
| 49 | + size: 2000, |
| 50 | + gzipSize: 26, |
| 51 | + }, |
| 52 | + ]); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe("there are no output options", () => { |
| 57 | + it("should process assets", async () => { |
| 58 | + const assets = [ |
| 59 | + { |
| 60 | + name: "file1.js", |
| 61 | + size: 1000, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "file2.js", |
| 65 | + size: 2000, |
| 66 | + }, |
| 67 | + ] as StatsAsset[]; |
| 68 | + |
| 69 | + const compilation = { |
| 70 | + hooks: {}, |
| 71 | + outputOptions: {}, |
| 72 | + getAsset: vi.fn().mockReturnValue({ |
| 73 | + source: { |
| 74 | + source: vi.fn().mockReturnValue("source"), |
| 75 | + }, |
| 76 | + }), |
| 77 | + // This is a fairly complex type to mock out, so we're just using the values that are needed for this test |
| 78 | + } as unknown as Compilation; |
| 79 | + |
| 80 | + const processedAssets = await processAssets({ assets, compilation }); |
| 81 | + |
| 82 | + expect(processedAssets).toEqual([ |
| 83 | + { |
| 84 | + name: "file1.js", |
| 85 | + normalized: "file1.js", |
| 86 | + size: 1000, |
| 87 | + gzipSize: 26, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "file2.js", |
| 91 | + normalized: "file2.js", |
| 92 | + size: 2000, |
| 93 | + gzipSize: 26, |
| 94 | + }, |
| 95 | + ]); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe("when there are source maps", () => { |
| 100 | + it("should not process the asset", async () => { |
| 101 | + const assets = [ |
| 102 | + { |
| 103 | + name: "file1.js", |
| 104 | + size: 1000, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "file1.js.map", |
| 108 | + size: 2000, |
| 109 | + }, |
| 110 | + ] as StatsAsset[]; |
| 111 | + |
| 112 | + const compilation = { |
| 113 | + hooks: {}, |
| 114 | + outputOptions: { |
| 115 | + filename: "filename", |
| 116 | + assetModuleFilename: "assetModuleFilename", |
| 117 | + chunkFilename: "chunkFilename", |
| 118 | + cssFilename: "cssFilename", |
| 119 | + cssChunkFilename: "cssChunkFilename", |
| 120 | + }, |
| 121 | + getAsset: vi.fn().mockReturnValue({ |
| 122 | + source: { |
| 123 | + source: vi.fn().mockReturnValue("source"), |
| 124 | + }, |
| 125 | + }), |
| 126 | + // This is a fairly complex type to mock out, so we're just using the values that are needed for this test |
| 127 | + } as unknown as Compilation; |
| 128 | + |
| 129 | + const processedAssets = await processAssets({ assets, compilation }); |
| 130 | + |
| 131 | + expect(processedAssets).toEqual([ |
| 132 | + { |
| 133 | + name: "file1.js", |
| 134 | + normalized: "file1.js", |
| 135 | + size: 1000, |
| 136 | + gzipSize: 26, |
| 137 | + }, |
| 138 | + ]); |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments