我是 Jest 单元测试的新手,想知道 Jest 是否可以用于测试 Node.js 文件系统模块。
\n\n我目前有一个包含一首短诗的文本文件,viewText 功能控制台将这首诗记录在我的终端上。
\n\n使用 Jest,我的目标是编写一个测试来检查viewText函数是否确实正常工作。
const viewText = () => {\n fs.readFile('poem.txt', 'utf8', (err, data) => {\n if (err) throw err;\n console.log(data);\n });\n};\nRun Code Online (Sandbox Code Playgroud)\n\n对于 Jest,我尝试过:
\n\njest.spyOn(global.console, 'log');\n\nconst mockPoem = 'Some say the world will end in fire, Some say in ice. From what I\xe2\x80\x99ve tasted of desire I hold with those who favor fire ... And would suffice.';\n\ndescribe('viewText', () => {\n const mockReadFile = jest.fn();\n mockReadFile.mockReturnValue(mockPoem);\n\n it('prints …Run Code Online (Sandbox Code Playgroud)