fs.未使用Jest触发ReadStream

jogvjijk  于 11个月前  发布在  Jest
关注(0)|答案(1)|浏览(123)

我正在使用fs. fullReadStream读取文件。当从文件中执行函数时,它正在工作,触发事件(data,end)。我需要编写一些单元测试,但当调用函数时。它没有命中任何事件(data,end)。我不明白原因。
我正在处理的原始问题是从响应中下载文件。为了隔离,我创建了file_func.ts文件。

const response = await fetch(fileUrl);
if (!response.ok) {
  throw new Error(response.statusText);
}

const dest = fs.createWriteStream(filePath);
response.body.pipe(dest);
response.body.on("finish", async function () {
});

字符串
环境节点v18.16.0
Typescript:版本4.9.5(用作节点模块)
file_func.ts

import * as fs from 'fs';
import path from "path";

const fileName = "./tests/sample.txt";
const dirPath = path.join(__dirname, fileName);
export async function working() {
  var data = '';

  var readerStream = fs.createReadStream(dirPath); //Create a readable stream

  readerStream.setEncoding('utf8'); // Set the encoding to be utf8.

// Handle stream events --> data, end, and error
  readerStream.on('data', function(chunk) {
    console.log('data');
    data += chunk;
  });

  readerStream.on('end',function() {
    console.log('end');
    console.log(data);
  });

  readerStream.on('error', function(err) {
    console.log(err.stack);
  });

  console.log("Program Ended");
}

working();


测试结果:

t@GTKNZ072:~/Project/pl/functions/a$ ts-node file_func.ts 
Program Ended
data
end
This is a test file
Another line

单元测试文件

import {working} from "../file_func";

it("Test read files", async () => {
  await working();
});


单元测试结果

console.log
    Program Ended

      at working (functions/a/file_func.ts:28:11)

  console.log
    Program Ended

      at working (functions/a/file_func.ts:28:11)

5q4ezhmt

5q4ezhmt1#

你的代码有几个问题。
1.因为你是在你的模块内部调用working();,所以working函数将在测试期间被调用两次:第一次是在导入模块时(由于前面提到的working()调用),第二次是在你的测试await working();中。这就是你看到Program Ended日志两次的原因。
1.仅仅因为你做了working xrc并不意味着它会像你期望的那样工作。Node仍然会等待所有的流操作完成,所以你的代码在理论上应该工作,但这将发生在Program Ended执行之后。这是我在运行tsx并使用NodeJS Test runner时得到的结果:

elvis@elvis-ThinkPad-T480s:~/plm$ ./node_modules/.bin/tsx test.js 
Program Ended
Program Ended
✔ Test read files (0.80028ms)
data
data
end
some test data
end
some test data

字符串
相反,您需要在streamer代码周围添加一个Promise,如下所示:

export async function working() {
  try {
    const result = await new Promise((resolve, reject) => {
      var data = "";

      var readerStream = fs.createReadStream(dirPath); //Create a readable stream

      readerStream.setEncoding("utf8"); // Set the encoding to be utf8.

      // Handle stream events --> data, end, and error
      readerStream.on("data", function (chunk) {
        data += chunk;
      });

      readerStream.on("end", function () {
        resolve(data);
      });

      readerStream.on("error", function (err) {
        reject(err.stack);
      });
    });

    console.log(result);
  } catch (err) {
    console.log("Got error", err);
  } finally {
    console.log("Program Ended");
  }
}


这将使await working();执行其预期操作并打印

elvis@elvis-ThinkPad-T480s:~/plm$ ./node_modules/.bin/tsx test.js 
some test data
Program Ended
some test data
Program Ended
✔ Test read files (4.357794ms)

  • 注意:我还删除了console.log('data')console.log('end')。*

相关问题