如何用 Jest 模拟 `fs.promises.writeFile`

How to Mock `fs.promises.writeFile` with Jest

我正在尝试使用 Jest 模拟 fs.writeFilepromise 版本,但未调用模拟函数。

待测函数(createFile.js):

const { writeFile } = require("fs").promises;

const createNewFile = async () => {
    await writeFile(`${__dirname}/newFile.txt`, "Test content");
};

module.exports = {
    createNewFile,
};

开玩笑测试 (createFile.test.js):

const fs = require("fs").promises;
const { createNewFile } = require("./createFile.js");

it("Calls writeFile", async () => {
    const writeFileSpy = jest.spyOn(fs, "writeFile");

    await createNewFile();
    expect(writeFileSpy).toHaveBeenCalledTimes(1);

    writeFileSpy.mockClear();
});

我知道实际上正在调用 writeFile 因为我 运行 node -e "require(\"./createFile.js\").createNewFile()" 并且文件已创建。

依赖版本

-- 这是对 createFile.test.js 文件的另一种尝试 --

const fs = require("fs");
const { createNewFile } = require("./createFile.js");

it("Calls writeFile", async () => {
    const writeFileMock = jest.fn();

    jest.mock("fs", () => ({
        promises: {
            writeFile: writeFileMock,
        },
    }));

    await createNewFile();
    expect(writeFileMock).toHaveBeenCalledTimes(1);
});

这将引发以下错误:

ReferenceError: /Users/danlevy/Desktop/test/src/createFile.test.js: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
    Invalid variable access: writeFileMock

由于 writeFile 在导入时被解构,而不是一直被称为 fs.promises.writeFile 方法,因此它不会受到 spyOn.

的影响

它应该像任何其他模块一样被模拟:

jest.mock("fs", () => ({
  promises: {
    writeFile: jest.fn().mockResolvedValue(),
    readFile: jest.fn().mockResolvedValue(),
  },
}));

const fs = require("fs");

...

await createNewFile();
expect(fs.promises.writeFile).toHaveBeenCalledTimes(1);

模拟 fs 几乎没有意义,因为未模拟的函数会产生副作用并可能对测试环境产生负面影响。

我知道这是一个旧线程,但就我而言,我想处理来自 readFile(或 writeFile 在您的情况下)的不同结果。所以我使用了 Estus Flask 建议的解决方案,不同之处在于我在每个测试中处理 readFile 的每个实现,而不是使用 mockResolvedValue.

我也在用打字稿。

import { getFile } from './configFiles';

import fs from 'fs';
jest.mock('fs', () => {
  return {
    promises: {
      readFile: jest.fn()
    }
  };
});

describe('getFile', () => {
   beforeEach(() => {
      jest.resetAllMocks();
   });

   it('should return results from file', async () => {
      const mockReadFile = (fs.promises.readFile as jest.Mock).mockImplementation(async () =>
        Promise.resolve(JSON.stringify('some-json-value'))
      );

      const res = await getFile('some-path');

      expect(mockReadFile).toHaveBeenCalledWith('some-path', { encoding: 'utf-8' });

      expect(res).toMatchObject('some-json-value');
   });

   it('should gracefully handle error', async () => {
      const mockReadFile = (fs.promises.readFile as jest.Mock).mockImplementation(async () =>
        Promise.reject(new Error('not found'))
      );

      const res = await getFile('some-path');

      expect(mockReadFile).toHaveBeenCalledWith('some-path', { encoding: 'utf-8' });

      expect(res).toMatchObject('whatever-your-fallback-is');
   });
});

请注意,我必须将 fs.promises.readFile 转换为 jest.Mock 才能使其适用于 TS。

此外,我的 configFiles.ts 看起来像这样:

import { promises as fsPromises } from 'fs';

const readConfigFile = async (filePath: string) => {
  const res = await fsPromises.readFile(filePath, { encoding: 'utf-8' });
  return JSON.parse(res);
};

export const getFile = async (path: string): Promise<MyType[]> => {
  try {
    const fileName = 'some_config.json';
    return readConfigFile(`${path}/${fileName}`);
  } catch (e) {
    // some fallback value
    return [{}];
  }
};

开玩笑地模拟“fs/promises”异步函数

这是一个使用 fs.readdir() 的简单示例,但它也适用于任何其他异步 fs/promises 函数。

files.service.test.js

import fs from "fs/promises";
import FileService from "./files.service";

jest.mock("fs/promises");

describe("FileService", () => {
  var fileService: FileService;

  beforeEach(() => {
    // Create a brand new FileService before running each test
    fileService = new FileService();

    // Reset mocks
    jest.resetAllMocks();
  });

  describe("getJsonFiles", () => {
    it("throws an error if reading the directory fails", async () => {
      // Mock the rejection error
      fs.readdir = jest.fn().mockRejectedValueOnce(new Error("mock error"));

      // Call the function to get the promise
      const promise = fileService.getJsonFiles({ folderPath: "mockPath", logActions: false });

      expect(fs.readdir).toHaveBeenCalled();
      await expect(promise).rejects.toEqual(new Error("mock error"));
    });

    it("returns an array of the .json file name strings in the test directory (and not any other files)", async () => {
      const allPotentialFiles = ["non-json.txt", "test-json-1.json", "test-json-2.json"];
      const onlyJsonFiles = ["test-json-1.json", "test-json-2.json"];

      // Mock readdir to return all potential files from the dir
      fs.readdir = jest.fn().mockResolvedValueOnce(allPotentialFiles);

      // Get the promise
      const promise = fileService.getJsonFiles({ folderPath: "mockPath", logActions: false });

      expect(fs.readdir).toBeCalled();
      await expect(promise).resolves.toEqual(onlyJsonFiles); // function should only return the json files
    });
  });
});

files.service.ts

import fs from "fs/promises";

export default class FileService {
  constructor() {}

  async getJsonFiles(args: FilesListArgs): Promise<string[]> {
    const { folderPath, logActions } = args;
    try {
      // Get list of all files
      const files = await fs.readdir(folderPath);

      // Filter to only include JSON files
      const jsonFiles = files.filter((file) => {
        return file.includes(".json");
      });

      return jsonFiles;
    } catch (e) {
      throw e;
    }
  }
}