Node.js-启动进程(firebase模拟器)并读取其输出

Node.js-启动进程(firebase模拟器)并读取其输出,node.js,child-process,Node.js,Child Process,我想在Jest测试之前启动firebase模拟器 执行此操作,但要按程序执行: E:\my-projct>firebase emulators:start --only firestore i emulators: Starting emulators: firestore i firestore: Serving ALL traffic (including WebChannel) on http://localhost:8080 ! firestore: Support for

我想在Jest测试之前启动firebase模拟器

执行此操作,但要按程序执行:

E:\my-projct>firebase emulators:start --only firestore
i  emulators: Starting emulators: firestore
i  firestore: Serving ALL traffic (including WebChannel) on http://localhost:8080
!  firestore: Support for WebChannel on a separate port (8081) is DEPRECATED and will go away soon. Please use port above instead.
i  firestore: Emulator logging to firestore-debug.log
+  firestore: Emulator started at http://localhost:8080
i  firestore: For testing set FIRESTORE_EMULATOR_HOST=localhost:8080
+  All emulators started, it is now safe to connect.
因此,我需要:

  • 执行命令

  • 等待“所有模拟器已启动”字符串出现

如何读取输出

我试过下面的方法,它打印的都是新行

const cp = require('child_process');
const child = cp.exec('firebase emulators:start --only firestore');
child.stdout.addListener('data', data => console.log(data.toString()));

这正是我们制作
firebase模拟器:exec
命令的原因

假设您使用“npm运行测试”来运行Jest脚本,您将使用:

firebase emulators:exec "npm run test"
这将:

  • 启动模拟器并等待它们准备好
  • 运行脚本(本例中为npm运行测试)
  • 等待脚本退出
  • 完全关闭模拟器