|
| 1 | +import { EnvironmentUtils } from "./environment-utils"; |
| 2 | + |
| 3 | +describe("EnvironmentUtils", () => { |
| 4 | + const originalEnv = process.env; |
| 5 | + |
| 6 | + // Save old process.env and replace it after each test |
| 7 | + // See https://stackoverflow.com/a/48042799 for reference |
| 8 | + beforeEach(() => { |
| 9 | + jest.resetModules(); // this is important - it clears the cache |
| 10 | + process.env = { ...originalEnv }; |
| 11 | + delete process.env; |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + process.env = originalEnv; |
| 16 | + }); |
| 17 | + |
| 18 | + // ----------------------------------------------------------------------------------------- |
| 19 | + // #region isDevelopment() |
| 20 | + // ----------------------------------------------------------------------------------------- |
| 21 | + |
| 22 | + describe("isDevelopment()", () => { |
| 23 | + test("when process.env.NODE_ENV is set to 'development', it returns true", () => { |
| 24 | + // Arrange |
| 25 | + const newEnv = { ...originalEnv }; |
| 26 | + newEnv.NODE_ENV = "development"; |
| 27 | + process.env = newEnv; |
| 28 | + |
| 29 | + // Act |
| 30 | + const result = EnvironmentUtils.isDevelopment(); |
| 31 | + |
| 32 | + // Assert |
| 33 | + expect(result).toBeTrue(); |
| 34 | + }); |
| 35 | + |
| 36 | + test("when process.env.NODE_ENV is set to 'production', it returns false", () => { |
| 37 | + // Arrange |
| 38 | + const newEnv = { ...originalEnv }; |
| 39 | + newEnv.NODE_ENV = "production"; |
| 40 | + process.env = newEnv; |
| 41 | + |
| 42 | + // Act |
| 43 | + const result = EnvironmentUtils.isDevelopment(); |
| 44 | + |
| 45 | + // Assert |
| 46 | + expect(result).toBeFalse(); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + // #endregion isDevelopment() |
| 51 | + |
| 52 | + // ----------------------------------------------------------------------------------------- |
| 53 | + // #region runIfDevelopment() |
| 54 | + // ----------------------------------------------------------------------------------------- |
| 55 | + |
| 56 | + describe("runIfDevelopment()", () => { |
| 57 | + test("when process.env.NODE_ENV is set to 'development', it runs the function", () => { |
| 58 | + // Arrange |
| 59 | + const newEnv = { ...originalEnv }; |
| 60 | + newEnv.NODE_ENV = "development"; |
| 61 | + process.env = newEnv; |
| 62 | + const fn = jest.fn(); |
| 63 | + |
| 64 | + // Act |
| 65 | + EnvironmentUtils.runIfDevelopment(() => fn()); |
| 66 | + |
| 67 | + // Assert |
| 68 | + expect(fn).toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + test("when process.env.NODE_ENV is set to 'production', it does not run the function", () => { |
| 72 | + // Arrange |
| 73 | + const newEnv = { ...originalEnv }; |
| 74 | + newEnv.NODE_ENV = "production"; |
| 75 | + process.env = newEnv; |
| 76 | + const fn = jest.fn(); |
| 77 | + |
| 78 | + // Act |
| 79 | + EnvironmentUtils.runIfDevelopment(() => fn()); |
| 80 | + |
| 81 | + // Assert |
| 82 | + expect(fn).not.toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + // #endregion runIfDevelopment() |
| 87 | +}); |
0 commit comments