|
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() } })); |
4 | 5 |
|
| 6 | +describe("slurpFile", () => { |
5 | 7 | const UTF8 = "utf8";
|
6 | 8 | const getMockFileContents = (path: string, options = UTF8) => JSON.stringify({ path, options });
|
7 | 9 |
|
8 | 10 | 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 | + ); |
14 | 14 | });
|
15 | 15 |
|
16 | 16 | afterEach(() => {
|
17 | 17 | jest.clearAllMocks();
|
18 |
| - jest.resetModules(); |
19 | 18 | });
|
20 | 19 |
|
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); |
24 | 25 |
|
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]); |
28 | 29 |
|
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); |
32 | 33 |
|
33 |
| - const fileContent = await slurpFile(mockPath); |
34 |
| - expect(fileContent).toStrictEqual(mockPathContent); |
| 34 | + const fileContent = await slurpFile(mockPath); |
| 35 | + expect(fileContent).toStrictEqual(mockPathContent); |
35 | 36 |
|
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 | + }); |
38 | 41 | });
|
39 | 42 |
|
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); |
43 | 49 |
|
44 |
| - const mockPath2 = "/mock/path/2"; |
45 |
| - const mockPathContent2 = getMockFileContents(mockPath2); |
| 50 | + const mockPath2 = "/mock/path/2"; |
| 51 | + const mockPathContent2 = getMockFileContents(mockPath2); |
46 | 52 |
|
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]); |
50 | 56 |
|
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); |
55 | 61 |
|
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); |
60 | 66 |
|
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 | + }); |
63 | 71 | });
|
64 | 72 | });
|
0 commit comments