|
| 1 | +import { Yok } from "../../../lib/common/yok"; |
| 2 | +import { WebpackCompilerService } from "../../../lib/services/webpack/webpack-compiler-service"; |
| 3 | +import { assert } from "chai"; |
| 4 | + |
| 5 | +const chunkFiles = ["bundle.js", "runtime.js", "vendor.js"]; |
| 6 | + |
| 7 | +function getAllEmittedFiles(hash: string) { |
| 8 | + return ["bundle.js", "runtime.js", `bundle.${hash}.hot-update.js`, `${hash}.hot-update.json`]; |
| 9 | +} |
| 10 | + |
| 11 | +function createTestInjector(): IInjector { |
| 12 | + const testInjector = new Yok(); |
| 13 | + testInjector.register("webpackCompilerService", WebpackCompilerService); |
| 14 | + testInjector.register("childProcess", {}); |
| 15 | + testInjector.register("hooksService", {}); |
| 16 | + testInjector.register("hostInfo", {}); |
| 17 | + testInjector.register("logger", {}); |
| 18 | + testInjector.register("mobileHelper", {}); |
| 19 | + testInjector.register("cleanupService", {}); |
| 20 | + |
| 21 | + return testInjector; |
| 22 | +} |
| 23 | + |
| 24 | +describe("WebpackCompilerService", () => { |
| 25 | + let testInjector: IInjector = null; |
| 26 | + let webpackCompilerService: WebpackCompilerService = null; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + testInjector = createTestInjector(); |
| 30 | + webpackCompilerService = testInjector.resolve(WebpackCompilerService); |
| 31 | + }); |
| 32 | + |
| 33 | + describe.only("getUpdatedEmittedFiles", () => { |
| 34 | + // backwards compatibility with old versions of nativescript-dev-webpack |
| 35 | + it("should return only hot updates when nextHash is not provided", async () => { |
| 36 | + const result = webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash1"), chunkFiles, null); |
| 37 | + const expectedEmittedFiles = ['bundle.hash1.hot-update.js', 'hash1.hot-update.json']; |
| 38 | + |
| 39 | + assert.deepEqual(result.emittedFiles, expectedEmittedFiles); |
| 40 | + }); |
| 41 | + // 2 successful webpack compilations |
| 42 | + it("should return only hot updates when nextHash is provided", async () => { |
| 43 | + webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash1"), chunkFiles, "hash2"); |
| 44 | + const result = webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash2"), chunkFiles, "hash3"); |
| 45 | + |
| 46 | + assert.deepEqual(result.emittedFiles, ['bundle.hash2.hot-update.js', 'hash2.hot-update.json']); |
| 47 | + }); |
| 48 | + // 1 successful webpack compilation, n compilations with no emitted files |
| 49 | + it("should return all files when there is a webpack compilation with no emitted files", () => { |
| 50 | + webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash1"), chunkFiles, "hash2"); |
| 51 | + const result = webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash4"), chunkFiles, "hash5"); |
| 52 | + |
| 53 | + assert.deepEqual(result.emittedFiles, ['bundle.js', 'runtime.js', 'bundle.hash4.hot-update.js', 'hash4.hot-update.json']); |
| 54 | + }); |
| 55 | + // 1 successful webpack compilation, n compilations with no emitted files, 1 successful webpack compilation |
| 56 | + it("should return only hot updates after fixing the compilation error", () => { |
| 57 | + webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash1"), chunkFiles, "hash2"); |
| 58 | + webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash5"), chunkFiles, "hash6"); |
| 59 | + const result = webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash6"), chunkFiles, "hash7"); |
| 60 | + |
| 61 | + assert.deepEqual(result.emittedFiles, ['bundle.hash6.hot-update.js', 'hash6.hot-update.json']); |
| 62 | + }); |
| 63 | + // 1 webpack compilation with no emitted files |
| 64 | + it("should return all files when first compilation on livesync change is not successful", () => { |
| 65 | + (<any>webpackCompilerService).expectedHash = "hash1"; |
| 66 | + const result = webpackCompilerService.getUpdatedEmittedFiles(getAllEmittedFiles("hash1"), chunkFiles, "hash2"); |
| 67 | + |
| 68 | + assert.deepEqual(result.emittedFiles, ["bundle.hash1.hot-update.js", "hash1.hot-update.json"]); |
| 69 | + }); |
| 70 | + }); |
| 71 | +}); |
0 commit comments