javascript - 是否可以为 Node.js fs.readFile() 编写 Jest 单元测试?
问题描述
我是 Jest 单元测试的新手,想知道 Jest 是否可用于测试 Node.js 文件系统模块。
我目前有一个包含一首短诗的文本文件,viewText 函数控制台将这首诗记录在我的终端上。
使用 Jest,我的目标是编写一个测试来检查viewText
功能是否确实有效。
const viewText = () => {
fs.readFile('poem.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
};
使用 Jest,我尝试过:
jest.spyOn(global.console, 'log');
const mockPoem = 'Some say the world will end in fire, Some say in ice. From what I’ve tasted of desire I hold with those who favor fire ... And would suffice.';
describe('viewText', () => {
const mockReadFile = jest.fn();
mockReadFile.mockReturnValue(mockPoem);
it('prints poem to console', () => {
viewText();
expect(global.console.log).toHaveBeenCalledWith(mockPoem);
});
});
通过测试,我要做的就是检查我的viewText
函数是否通过了测试——能够查看mockPoem
. 我真的很困惑我应该如何使用文件系统模块为函数编写单元测试。
解决方案
这是一个UT解决方案:
index.ts
:
import fs from 'fs';
export const viewText = () => {
fs.readFile('poem.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
};
index.spec.ts
:
import { viewText } from './';
import fs from 'fs';
const mockPoem =
'Some say the world will end in fire, Some say in ice. From what I’ve tasted of desire I hold with those who favor fire ... And would suffice.';
describe('viewText', () => {
afterEach(() => {
jest.restoreAllMocks();
});
test('prints poem to console', done => {
const logSpy = jest.spyOn(console, 'log');
let readFileCallback;
// @ts-ignore
jest.spyOn(fs, 'readFile').mockImplementation((path, options, callback) => {
readFileCallback = callback;
});
viewText();
readFileCallback(null, mockPoem);
expect(logSpy).toBeCalledWith(mockPoem);
expect(fs.readFile).toBeCalledWith('poem.txt', 'utf8', readFileCallback);
done();
});
test('should throw error when read file failed', done => {
let readFileCallback;
// @ts-ignore
jest.spyOn(fs, 'readFile').mockImplementation((path, options, callback) => {
readFileCallback = callback;
});
viewText();
const mError = new Error('read file failed');
expect(() => readFileCallback(mError, null)).toThrowError(mError);
expect(fs.readFile).toBeCalledWith('poem.txt', 'utf8', readFileCallback);
done();
});
});
覆盖率 100% 的单元测试结果:
PASS src/stackoverflow/58810079/index.spec.ts (11.118s)
viewText
✓ prints poem to console (23ms)
✓ should throw error when read file failed (3ms)
console.log node_modules/jest-mock/build/index.js:860
Some say the world will end in fire, Some say in ice. From what I’ve tasted of desire I hold with those who favor fire ... And would suffice.
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 13.129s
源代码:https ://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/58810079
推荐阅读
- javascript - 将数据从 C# 发送到 javascript 的最快方法?
- python - 如何在 Python 中的电报发送消息机器人中使用解析模式
- python - how to click on item in UL using pythong selenium
- r - 如何使用 dplyr 访问/选择嵌套数据框列
- x86 - 如何在 QEMU 会话 gdb 中检查 xcr0 寄存器?
- c++ - 关于C++设计模式的问题,纯虚方法调用
- javascript - TypeError:filePaths.map 不是 puppeteer 中的函数
- twilio - 使用 Twilio API 拨打电话时如何收集用户输入?
- swift - 如何制作一个采用 Binding 并将其传递给视图中的另一个 Binding 的视图 - SwiftUI
- java - 部署有效,JPA 有效,JawsDB 有效,但它们不能一起工作