Skip to content

Commit

Permalink
fix tests after fs changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rlindner81 committed Dec 14, 2024
1 parent 146a9a3 commit 38c53d7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 38 deletions.
7 changes: 4 additions & 3 deletions test/__mocks/sharedNockPlayback/static.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -24,7 +24,7 @@ const mockRuntimeConfig = {
};

module.exports = {
...sharedStatic,
...realStatic,
// mock file read and access
tryReadJsonSync: jest.fn((filepath) => {
if (filepath.endsWith("config.json")) {
Expand All @@ -40,6 +40,7 @@ module.exports = {
return true;
}
}),
writeJsonSync: jest.fn(),
// mock spawn
spawnAsync: jest.fn(async (command, args, options) => {
switch (`${command} ${args.join(" ")}`) {
Expand All @@ -50,5 +51,5 @@ module.exports = {
}
}),
// speed up sleep
sleep: jest.fn(async () => await sharedStatic.sleep(0)),
sleep: jest.fn(),
};
1 change: 1 addition & 0 deletions test/context.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jest.mock("../src/shared/static", () => {
makeOneTime,
tryAccessSync: jest.fn(),
tryReadJsonSync: jest.fn(),
writeJsonSync: jest.fn(),
spawnAsync: jest.fn(),
};
});
Expand Down
58 changes: 23 additions & 35 deletions test/submodules/setup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}));

Expand All @@ -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");
Expand Down Expand Up @@ -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(`
Expand Down Expand Up @@ -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");
});

Expand All @@ -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(`
Expand All @@ -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",
Expand All @@ -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");
});

Expand Down

0 comments on commit 38c53d7

Please sign in to comment.