From 38c53d753c3b6551536d85a09535039ee7a4eaa6 Mon Sep 17 00:00:00 2001 From: Richard Lindner Date: Sat, 14 Dec 2024 01:46:49 +0100 Subject: [PATCH] fix tests after fs changes --- test/__mocks/sharedNockPlayback/static.js | 7 +-- test/context.test.js | 1 + test/submodules/setup.test.js | 58 +++++++++-------------- 3 files changed, 28 insertions(+), 38 deletions(-) diff --git a/test/__mocks/sharedNockPlayback/static.js b/test/__mocks/sharedNockPlayback/static.js index c030c27..27cf82f 100644 --- a/test/__mocks/sharedNockPlayback/static.js +++ b/test/__mocks/sharedNockPlayback/static.js @@ -1,7 +1,7 @@ "use strict"; const { format } = require("util"); -const sharedStatic = jest.requireActual("../../../src/shared/static"); +const realStatic = jest.requireActual("../../../src/shared/static"); const mockCfConfig = { OrganizationFields: { @@ -24,7 +24,7 @@ const mockRuntimeConfig = { }; module.exports = { - ...sharedStatic, + ...realStatic, // mock file read and access tryReadJsonSync: jest.fn((filepath) => { if (filepath.endsWith("config.json")) { @@ -40,6 +40,7 @@ module.exports = { return true; } }), + writeJsonSync: jest.fn(), // mock spawn spawnAsync: jest.fn(async (command, args, options) => { switch (`${command} ${args.join(" ")}`) { @@ -50,5 +51,5 @@ module.exports = { } }), // speed up sleep - sleep: jest.fn(async () => await sharedStatic.sleep(0)), + sleep: jest.fn(), }; diff --git a/test/context.test.js b/test/context.test.js index 453d3fb..ef0ca09 100644 --- a/test/context.test.js +++ b/test/context.test.js @@ -13,6 +13,7 @@ jest.mock("../src/shared/static", () => { makeOneTime, tryAccessSync: jest.fn(), tryReadJsonSync: jest.fn(), + writeJsonSync: jest.fn(), spawnAsync: jest.fn(), }; }); diff --git a/test/submodules/setup.test.js b/test/submodules/setup.test.js index 8be75ee..8b3d4a4 100644 --- a/test/submodules/setup.test.js +++ b/test/submodules/setup.test.js @@ -7,6 +7,8 @@ jest.mock("../../src/shared/logger", () => require("../__mocks/shared/logger")); const mockStatic = require("../../src/shared/static"); jest.mock("../../src/shared/static", () => ({ tryAccessSync: jest.fn(), + writeJsonSync: jest.fn(), + deleteFileSync: jest.fn(), question: jest.fn(), })); @@ -15,13 +17,6 @@ jest.mock("../../src/context", () => ({ readRuntimeConfig: jest.fn(), })); -const mockFs = require("fs"); -jest.mock("fs", () => ({ - writeFileSync: jest.fn(), - unlinkSync: jest.fn(), - constants: { R_OK: 4 }, -})); - const processCwdSpy = jest.spyOn(process, "cwd"); const { outputFromLogger } = require("../test-util/static"); @@ -71,18 +66,17 @@ describe("set tests", () => { expect(await set.setup()).toMatchInlineSnapshot(`undefined`); expect(mockStatic.tryAccessSync).toHaveBeenCalledTimes(0); expect(mockContextModule.readRuntimeConfig).toHaveBeenCalledTimes(1); - expect(mockFs.writeFileSync).toHaveBeenCalledTimes(1); - expect(mockFs.writeFileSync.mock.calls[0]).toMatchInlineSnapshot(` + expect(mockStatic.writeJsonSync).toHaveBeenCalledTimes(1); + expect(mockStatic.writeJsonSync.mock.calls[0]).toMatchInlineSnapshot(` [ "/root/home-dir/.mtxrc.json", - "{ - "uaaAppName": "answer 1", - "regAppName": "answer 2", - "cdsAppName": "answer 3", - "hdiAppName": "answer 4", - "srvAppName": "answer 5" - } - ", + { + "cdsAppName": "answer 3", + "hdiAppName": "answer 4", + "regAppName": "answer 2", + "srvAppName": "answer 5", + "uaaAppName": "answer 1", + }, ] `); expect(outputFromLogger(mockLogger.info.mock.calls)).toMatchInlineSnapshot(` @@ -115,7 +109,7 @@ describe("set tests", () => { for (let i = 0; i < Object.keys(mockRuntimeConfig).length; i++) { mockStatic.question.mockReturnValueOnce(`answer ${i + 1}`); } - mockFs.writeFileSync.mockImplementationOnce(() => { + mockStatic.writeJsonSync.mockImplementationOnce(() => { throw new Error("cannot write"); }); @@ -133,18 +127,17 @@ describe("set tests", () => { expect(await set.setupLocal()).toMatchInlineSnapshot(`undefined`); expect(mockStatic.tryAccessSync).toHaveBeenCalledTimes(0); expect(mockContextModule.readRuntimeConfig).toHaveBeenCalledTimes(1); - expect(mockFs.writeFileSync).toHaveBeenCalledTimes(1); - expect(mockFs.writeFileSync.mock.calls[0]).toMatchInlineSnapshot(` + expect(mockStatic.writeJsonSync).toHaveBeenCalledTimes(1); + expect(mockStatic.writeJsonSync.mock.calls[0]).toMatchInlineSnapshot(` [ "/root/local-dir/.mtxrc.json", - "{ - "uaaAppName": "answer 1", - "regAppName": "answer 2", - "cdsAppName": "answer 3", - "hdiAppName": "answer 4", - "srvAppName": "answer 5" - } - ", + { + "cdsAppName": "answer 3", + "hdiAppName": "answer 4", + "regAppName": "answer 2", + "srvAppName": "answer 5", + "uaaAppName": "answer 1", + }, ] `); expect(outputFromLogger(mockLogger.info.mock.calls)).toMatchInlineSnapshot(` @@ -165,27 +158,22 @@ describe("set tests", () => { [ [ "/root/local-dir/.mtxcache.json", - 4, ], [ "/root/local-dir/.mtxcache.json", - 4, ], [ "/root/.mtxcache.json", - 4, ], [ "/.mtxcache.json", - 4, ], [ "/root/home-dir/.mtxcache.json", - 4, ], ] `); - expect(mockFs.unlinkSync.mock.calls).toMatchInlineSnapshot(` + expect(mockStatic.deleteFileSync.mock.calls).toMatchInlineSnapshot(` [ [ "/root/local-dir/.mtxcache.json", @@ -203,7 +191,7 @@ describe("set tests", () => { for (let i = 0; i < 4; i++) { mockStatic.tryAccessSync.mockReturnValueOnce(false); } - mockFs.unlinkSync.mockImplementationOnce(() => { + mockStatic.deleteFileSync.mockImplementationOnce(() => { throw new Error("failing delete"); });