|
| 1 | +import * as fs from "fs" |
| 2 | +import { loadEnvironment } from "../src/loadEnvironment" |
| 3 | + |
| 4 | +const contentfulEnvironment = () => ({ |
| 5 | + getContentTypes: () => [], |
| 6 | + getLocales: () => [], |
| 7 | +}) |
| 8 | + |
| 9 | +const getContentfulEnvironmentFileFactory = jest.fn((_type: string) => contentfulEnvironment) |
| 10 | + |
| 11 | +jest.mock( |
| 12 | + require("path").resolve(process.cwd(), "./getContentfulEnvironment.js"), |
| 13 | + () => getContentfulEnvironmentFileFactory("js"), |
| 14 | + { virtual: true }, |
| 15 | +) |
| 16 | + |
| 17 | +jest.mock( |
| 18 | + require("path").resolve(process.cwd(), "./getContentfulEnvironment.ts"), |
| 19 | + () => getContentfulEnvironmentFileFactory("ts"), |
| 20 | + { virtual: true }, |
| 21 | +) |
| 22 | + |
| 23 | +const tsNodeRegistererEnabled = jest.fn() |
| 24 | +const tsNodeRegister = jest.fn() |
| 25 | + |
| 26 | +jest.mock("ts-node", () => ({ register: tsNodeRegister })) |
| 27 | + |
| 28 | +describe("loadEnvironment", () => { |
| 29 | + beforeEach(() => { |
| 30 | + jest.resetAllMocks() |
| 31 | + jest.restoreAllMocks() |
| 32 | + jest.resetModules() |
| 33 | + |
| 34 | + getContentfulEnvironmentFileFactory.mockReturnValue(contentfulEnvironment) |
| 35 | + tsNodeRegister.mockReturnValue({ enabled: tsNodeRegistererEnabled }) |
| 36 | + }) |
| 37 | + |
| 38 | + describe("when getContentfulEnvironment.ts exists", () => { |
| 39 | + beforeEach(() => { |
| 40 | + jest.spyOn(fs, "existsSync").mockReturnValue(true) |
| 41 | + }) |
| 42 | + |
| 43 | + describe("when ts-node is not found", () => { |
| 44 | + beforeEach(() => { |
| 45 | + // technically this is throwing after the `require` call, |
| 46 | + // but it still tests the same code path so is fine |
| 47 | + tsNodeRegister.mockImplementation(() => { |
| 48 | + throw new (class extends Error { |
| 49 | + public code: string |
| 50 | + |
| 51 | + constructor(message?: string) { |
| 52 | + super(message) |
| 53 | + this.code = "MODULE_NOT_FOUND" |
| 54 | + } |
| 55 | + })() |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + it("throws a nice error", async () => { |
| 60 | + await expect(loadEnvironment()).rejects.toThrow( |
| 61 | + "'ts-node' is required for TypeScript configuration files", |
| 62 | + ) |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + describe("when there is another error", () => { |
| 67 | + beforeEach(() => { |
| 68 | + tsNodeRegister.mockImplementation(() => { |
| 69 | + throw new Error("something else went wrong!") |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + it("re-throws", async () => { |
| 74 | + await expect(loadEnvironment()).rejects.toThrow("something else went wrong!") |
| 75 | + }) |
| 76 | + }) |
| 77 | + |
| 78 | + describe("when called multiple times", () => { |
| 79 | + it("re-uses the registerer", async () => { |
| 80 | + await loadEnvironment() |
| 81 | + await loadEnvironment() |
| 82 | + |
| 83 | + expect(tsNodeRegister).toHaveBeenCalledTimes(1) |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + it("requires the typescript config", async () => { |
| 88 | + await loadEnvironment() |
| 89 | + |
| 90 | + expect(getContentfulEnvironmentFileFactory).toHaveBeenCalledWith("ts") |
| 91 | + expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("js") |
| 92 | + }) |
| 93 | + |
| 94 | + it("disables the registerer afterwards", async () => { |
| 95 | + await loadEnvironment() |
| 96 | + |
| 97 | + expect(tsNodeRegistererEnabled).toHaveBeenCalledWith(false) |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + it("requires the javascript config", async () => { |
| 102 | + jest.spyOn(fs, "existsSync").mockReturnValue(false) |
| 103 | + |
| 104 | + await loadEnvironment() |
| 105 | + |
| 106 | + expect(getContentfulEnvironmentFileFactory).toHaveBeenCalledWith("js") |
| 107 | + expect(getContentfulEnvironmentFileFactory).not.toHaveBeenCalledWith("ts") |
| 108 | + }) |
| 109 | +}) |
0 commit comments