Skip to content

Commit ec0c872

Browse files
authored
test(shared-ini-file-loader): use jest.isolatedModules in slurpFile (#3442)
1 parent cfdf09d commit ec0c872

File tree

1 file changed

+48
-40
lines changed

1 file changed

+48
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,72 @@
1-
describe("slurpFile", () => {
2-
let slurpFile;
3-
let mockReadFile;
1+
// ToDo: Change to "fs/promises" when supporting nodejs>=14
2+
import { promises } from "fs";
3+
4+
jest.mock("fs", () => ({ promises: { readFile: jest.fn() } }));
45

6+
describe("slurpFile", () => {
57
const UTF8 = "utf8";
68
const getMockFileContents = (path: string, options = UTF8) => JSON.stringify({ path, options });
79

810
beforeEach(() => {
9-
mockReadFile = jest.fn().mockImplementation((path, options) => Promise.resolve(getMockFileContents(path, options)));
10-
jest.mock("fs", () => ({ promises: { readFile: mockReadFile } }));
11-
12-
const slurpFileRequire = require("./slurpFile");
13-
slurpFile = slurpFileRequire.slurpFile;
11+
(promises.readFile as jest.Mock).mockImplementation((path, options) =>
12+
Promise.resolve(getMockFileContents(path, options))
13+
);
1414
});
1515

1616
afterEach(() => {
1717
jest.clearAllMocks();
18-
jest.resetModules();
1918
});
2019

21-
it("makes one readFile call for a filepath irrepsective of slurpFile calls", async () => {
22-
const mockPath = "/mock/path";
23-
const mockPathContent = getMockFileContents(mockPath);
20+
it("makes one readFile call for a filepath irrepsective of slurpFile calls", (done) => {
21+
jest.isolateModules(async () => {
22+
const { slurpFile } = require("./slurpFile");
23+
const mockPath = "/mock/path";
24+
const mockPathContent = getMockFileContents(mockPath);
2425

25-
expect(mockReadFile).not.toHaveBeenCalled();
26-
const fileContentArr = await Promise.all([slurpFile(mockPath), slurpFile(mockPath)]);
27-
expect(fileContentArr).toStrictEqual([mockPathContent, mockPathContent]);
26+
expect(promises.readFile).not.toHaveBeenCalled();
27+
const fileContentArr = await Promise.all([slurpFile(mockPath), slurpFile(mockPath)]);
28+
expect(fileContentArr).toStrictEqual([mockPathContent, mockPathContent]);
2829

29-
// There is one readFile call even through slurpFile is called in parallel twice.
30-
expect(mockReadFile).toHaveBeenCalledTimes(1);
31-
expect(mockReadFile).toHaveBeenCalledWith(mockPath, UTF8);
30+
// There is one readFile call even through slurpFile is called in parallel twice.
31+
expect(promises.readFile).toHaveBeenCalledTimes(1);
32+
expect(promises.readFile).toHaveBeenCalledWith(mockPath, UTF8);
3233

33-
const fileContent = await slurpFile(mockPath);
34-
expect(fileContent).toStrictEqual(mockPathContent);
34+
const fileContent = await slurpFile(mockPath);
35+
expect(fileContent).toStrictEqual(mockPathContent);
3536

36-
// There is one readFile call even through slurpFile is called for the third time.
37-
expect(mockReadFile).toHaveBeenCalledTimes(1);
37+
// There is one readFile call even through slurpFile is called for the third time.
38+
expect(promises.readFile).toHaveBeenCalledTimes(1);
39+
done();
40+
});
3841
});
3942

40-
it("makes multiple readFile calls with based on filepaths", async () => {
41-
const mockPath1 = "/mock/path/1";
42-
const mockPathContent1 = getMockFileContents(mockPath1);
43+
it("makes multiple readFile calls with based on filepaths", (done) => {
44+
jest.isolateModules(async () => {
45+
const { slurpFile } = require("./slurpFile");
46+
47+
const mockPath1 = "/mock/path/1";
48+
const mockPathContent1 = getMockFileContents(mockPath1);
4349

44-
const mockPath2 = "/mock/path/2";
45-
const mockPathContent2 = getMockFileContents(mockPath2);
50+
const mockPath2 = "/mock/path/2";
51+
const mockPathContent2 = getMockFileContents(mockPath2);
4652

47-
expect(mockReadFile).not.toHaveBeenCalled();
48-
const fileContentArr = await Promise.all([slurpFile(mockPath1), slurpFile(mockPath2)]);
49-
expect(fileContentArr).toStrictEqual([mockPathContent1, mockPathContent2]);
53+
expect(promises.readFile).not.toHaveBeenCalled();
54+
const fileContentArr = await Promise.all([slurpFile(mockPath1), slurpFile(mockPath2)]);
55+
expect(fileContentArr).toStrictEqual([mockPathContent1, mockPathContent2]);
5056

51-
// There are two readFile calls as slurpFile is called in parallel with different filepaths.
52-
expect(mockReadFile).toHaveBeenCalledTimes(2);
53-
expect(mockReadFile).toHaveBeenNthCalledWith(1, mockPath1, UTF8);
54-
expect(mockReadFile).toHaveBeenNthCalledWith(2, mockPath2, UTF8);
57+
// There are two readFile calls as slurpFile is called in parallel with different filepaths.
58+
expect(promises.readFile).toHaveBeenCalledTimes(2);
59+
expect(promises.readFile).toHaveBeenNthCalledWith(1, mockPath1, UTF8);
60+
expect(promises.readFile).toHaveBeenNthCalledWith(2, mockPath2, UTF8);
5561

56-
const fileContent1 = await slurpFile(mockPath1);
57-
expect(fileContent1).toStrictEqual(mockPathContent1);
58-
const fileContent2 = await slurpFile(mockPath2);
59-
expect(fileContent2).toStrictEqual(mockPathContent2);
62+
const fileContent1 = await slurpFile(mockPath1);
63+
expect(fileContent1).toStrictEqual(mockPathContent1);
64+
const fileContent2 = await slurpFile(mockPath2);
65+
expect(fileContent2).toStrictEqual(mockPathContent2);
6066

61-
// There is one readFile call even through slurpFile is called for the third time.
62-
expect(mockReadFile).toHaveBeenCalledTimes(2);
67+
// There is one readFile call even through slurpFile is called for the third time.
68+
expect(promises.readFile).toHaveBeenCalledTimes(2);
69+
done();
70+
});
6371
});
6472
});

0 commit comments

Comments
 (0)