Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否可以为Node.js fs.readFile()编写Jest单元测试?_Javascript_Node.js_Unit Testing_Jestjs - Fatal编程技术网

Javascript 是否可以为Node.js fs.readFile()编写Jest单元测试?

Javascript 是否可以为Node.js fs.readFile()编写Jest单元测试?,javascript,node.js,unit-testing,jestjs,Javascript,Node.js,Unit Testing,Jestjs,我不熟悉Jest单元测试,想知道Jest是否可以用于测试Node.js文件系统模块 我目前有一个包含一首短诗的文本文件,viewText函数控制台会在我的终端上记录这首诗 使用Jest,我的目标是编写一个测试,检查viewText函数是否确实在工作 const viewText = () => { fs.readFile('poem.txt', 'utf8', (err, data) => { if (err) throw err; console.log(dat

我不熟悉Jest单元测试,想知道Jest是否可以用于测试Node.js文件系统模块

我目前有一个包含一首短诗的文本文件,viewText函数控制台会在我的终端上记录这首诗

使用Jest,我的目标是编写一个测试,检查viewText函数是否确实在工作

const viewText = () => {
  fs.readFile('poem.txt', 'utf8', (err, data) => {
    if (err) throw err;
    console.log(data);
  });
};
我开玩笑地试过:

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:

从“fs”导入fs; 导出常量viewText==>{ fs.readFile'poem.txt',utf8',err,data=>{ 如果犯了错误,就扔出错误; 控制台日志数据; }; }; index.spec.ts:

从“/”导入{viewText}; 从“fs”导入fs; 康斯特讽刺诗= 有人说世界将在火中终结,有人说将在冰中终结。从我尝到的欲望中,我和那些喜欢火的人在一起。。。就足够了。”; 描述“视图文本”,=>{ 之后=>{ 开玩笑,恢复活力; }; 测试“将诗歌打印到控制台”,完成=>{ const logSpy=jest.spyOnconsole,“log”; 让readFileCallback; //@ts忽略 jest.spyOnfs,“readFile”.mockImplementationpath,选项,回调=>{ readFileCallback=callback; }; 视图文本; readFileCallbacknull,mockPoem; 期望用模拟诗来收集; expectfs.readFile.toBeCalledWith'poem.txt',utf8',readFileCallback; 完成; }; 测试“读取文件失败时应引发错误”,完成=>{ 让readFileCallback; //@ts忽略 jest.spyOnfs,“readFile”.mockImplementationpath,选项,回调=>{ readFileCallback=callback; }; 视图文本; const mError=新错误“读取文件失败”; expect=>readFileCallbackmError,null.tothrowerError; expectfs.readFile.toBeCalledWith'poem.txt',utf8',readFileCallback; 完成; }; }; 100%覆盖率的单元测试结果:

通过src/stackoverflow/58810079/index.spec.ts 11.118s 视图文本 ✓ 将诗歌打印到控制台23ms ✓ 读取文件失败3ms时应引发错误 console.log node_modules/jest mock/build/index.js:860 有人说世界将在火中终结,有人说将在冰中终结。从我尝到的欲望中,我和那些喜欢火的人在一起。。。这样就足够了。 -----|-----|-----|-----|-----|----------| 文件|%Stmts |%Branch |%Funcs |%Lines |未覆盖的行| -----|-----|-----|-----|-----|----------| 所有文件| 100 | 100 | 100 | 100 || index.ts | 100 | 100 | 100 | 100 || -----|-----|-----|-----|-----|----------| 测试套件:1个通过,共1个 测试:2次通过,共2次 快照:共0个 时间:13.129秒
源代码:

这是一个UT解决方案:

index.ts:

从“fs”导入fs; 导出常量viewText==>{ fs.readFile'poem.txt',utf8',err,data=>{ 如果犯了错误,就扔出错误; 控制台日志数据; }; }; index.spec.ts:

从“/”导入{viewText}; 从“fs”导入fs; 康斯特讽刺诗= 有人说世界将在火中终结,有人说将在冰中终结。从我尝到的欲望中,我和那些喜欢火的人在一起。。。就足够了。”; 描述“视图文本”,=>{ 之后=>{ 开玩笑,恢复活力; }; 测试“将诗歌打印到控制台”,完成=>{ const logSpy=jest.spyOnconsole,“log”; 让readFileCallback; //@ts忽略 jest.spyOnfs,“readFile”.mockImplementationpath,选项,回调=>{ readFileCallback=callback; }; 视图文本; readFileCallbacknull,mockPoem; 期望用模拟诗来收集; expectfs.readFile.toBeCalledWith'poem.txt',utf8',readFileCallback; 完成; }; 测试“读取文件失败时应引发错误”,完成=>{ 让readFileCallback; //@ts忽略 jest.spyOnfs,“readFile”.mockImplementationpath,选项,回调=>{ readFileCallback=callback; }; 视图文本; const mError=新错误“读取文件失败”; expect=>readFileCallbackmError,null.tothrowerError; expectfs.readFile.toBeCalledWith'poem.txt',utf8',readFileCallback; 完成; }; }; 100%覆盖率的单元测试结果:

通过src/stackoverflow/58810079/index.spec.ts 11.118s 视图文本 ✓ 将诗歌打印到控制台23ms ✓ 读取文件失败3ms时应引发错误 console.log node_modules/jest mock/build/index.js:860 有人说世界将在火中终结,有人说将在冰中终结。从我尝到的欲望中,我和那些喜欢火的人在一起。。。这样就足够了。 -----|-----|-----|-----|-----|----------| 文件|%Stmts |%Branch |%Funcs |%Lines |未覆盖的行| -----|-----|-----|-----|-----|----------| 所有文件| 100 | 100 | 100 | 100 || index.ts | 100 | 100 | 100| 100 | | -----|-----|-----|-----|-----|----------| 测试套件:1个通过,共1个 测试:2次通过,共2次 快照:共0个 时间:13.129秒
源代码:

FWIW,我不知道。我将I/o与逻辑分开,只测试逻辑FWIW,不测试。我将I/o与逻辑分开,并且只测试逻辑