|
| 1 | +import * as logger from "@opennextjs/aws/adapters/logger.js"; |
| 2 | +import { |
| 3 | + FatalError, |
| 4 | + IgnorableError, |
| 5 | + RecoverableError, |
| 6 | +} from "@opennextjs/aws/utils/error.js"; |
| 7 | +import { vi } from "vitest"; |
| 8 | + |
| 9 | +describe("logger adapter", () => { |
| 10 | + describe("Open Next errors", () => { |
| 11 | + const debug = vi.spyOn(console, "log").mockImplementation(() => null); |
| 12 | + const warn = vi.spyOn(console, "warn").mockImplementation(() => null); |
| 13 | + const error = vi.spyOn(console, "error").mockImplementation(() => null); |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + // Direct debug to console.log |
| 17 | + globalThis.openNextDebug = true; |
| 18 | + debug.mockClear(); |
| 19 | + warn.mockClear(); |
| 20 | + error.mockClear(); |
| 21 | + }); |
| 22 | + |
| 23 | + afterAll(() => { |
| 24 | + globalThis.openNextDebug = false; |
| 25 | + }); |
| 26 | + |
| 27 | + const ignorableError = new IgnorableError("ignorable"); |
| 28 | + const recoverableError = new RecoverableError("recoverable"); |
| 29 | + const fatalError = new FatalError("fatal"); |
| 30 | + |
| 31 | + it("default to warn when OPEN_NEXT_ERROR_LOG_LEVEL is undefined", () => { |
| 32 | + delete process.env.OPEN_NEXT_ERROR_LOG_LEVEL; |
| 33 | + logger.error(ignorableError); |
| 34 | + logger.error(recoverableError); |
| 35 | + logger.error(fatalError); |
| 36 | + expect(debug).not.toHaveBeenCalled(); |
| 37 | + expect(warn).toHaveBeenCalledWith("RecoverableError: recoverable"); |
| 38 | + expect(error).toHaveBeenCalledWith(fatalError); |
| 39 | + }); |
| 40 | + |
| 41 | + it("OPEN_NEXT_ERROR_LOG_LEVEL is 'debug'/'0'", () => { |
| 42 | + process.env.OPEN_NEXT_ERROR_LOG_LEVEL = "0"; |
| 43 | + logger.error(ignorableError); |
| 44 | + logger.error(recoverableError); |
| 45 | + logger.error(fatalError); |
| 46 | + expect(debug).toHaveBeenCalledWith("IgnorableError: ignorable"); |
| 47 | + expect(warn).toHaveBeenCalledWith("RecoverableError: recoverable"); |
| 48 | + expect(error).toHaveBeenCalledWith(fatalError); |
| 49 | + process.env.OPEN_NEXT_ERROR_LOG_LEVEL = "debug"; |
| 50 | + logger.error(ignorableError); |
| 51 | + logger.error(recoverableError); |
| 52 | + logger.error(fatalError); |
| 53 | + expect(debug).toHaveBeenCalledWith("IgnorableError: ignorable"); |
| 54 | + expect(warn).toHaveBeenCalledWith("RecoverableError: recoverable"); |
| 55 | + expect(error).toHaveBeenCalledWith(fatalError); |
| 56 | + }); |
| 57 | + |
| 58 | + it("OPEN_NEXT_ERROR_LOG_LEVEL is 'warn'/'1'", () => { |
| 59 | + process.env.OPEN_NEXT_ERROR_LOG_LEVEL = "1"; |
| 60 | + logger.error(ignorableError); |
| 61 | + logger.error(recoverableError); |
| 62 | + logger.error(fatalError); |
| 63 | + expect(debug).not.toHaveBeenCalled(); |
| 64 | + expect(warn).toHaveBeenCalledWith("RecoverableError: recoverable"); |
| 65 | + expect(error).toHaveBeenCalledWith(fatalError); |
| 66 | + process.env.OPEN_NEXT_ERROR_LOG_LEVEL = "warn"; |
| 67 | + logger.error(ignorableError); |
| 68 | + logger.error(recoverableError); |
| 69 | + logger.error(fatalError); |
| 70 | + expect(debug).not.toHaveBeenCalled(); |
| 71 | + expect(warn).toHaveBeenCalledWith("RecoverableError: recoverable"); |
| 72 | + expect(error).toHaveBeenCalledWith(fatalError); |
| 73 | + }); |
| 74 | + |
| 75 | + it("OPEN_NEXT_ERROR_LOG_LEVEL is 'error'/'2'", () => { |
| 76 | + process.env.OPEN_NEXT_ERROR_LOG_LEVEL = "2"; |
| 77 | + logger.error(ignorableError); |
| 78 | + logger.error(recoverableError); |
| 79 | + logger.error(fatalError); |
| 80 | + expect(debug).not.toHaveBeenCalled(); |
| 81 | + expect(warn).not.toHaveBeenCalled(); |
| 82 | + expect(error).toHaveBeenCalledWith(fatalError); |
| 83 | + process.env.OPEN_NEXT_ERROR_LOG_LEVEL = "error"; |
| 84 | + logger.error(ignorableError); |
| 85 | + logger.error(recoverableError); |
| 86 | + logger.error(fatalError); |
| 87 | + expect(debug).not.toHaveBeenCalled(); |
| 88 | + expect(warn).not.toHaveBeenCalled(); |
| 89 | + expect(error).toHaveBeenCalledWith(fatalError); |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments