firebase 如何测试模拟器是否为 运行?

firebase how do I test if emulator is running?

我正在使用以下代码连接到我的存储模拟器:

const firebaseApp = initializeApp(appConfig.firebase)
storage = getStorage(firebaseApp)
connectStorageEmulator(storage, 'localhost', 9199)

当模拟器为 运行 时,这可以正常工作。当模拟器不是 运行 时它也能正常工作!如果不是 运行 我想做这样的事情:

if (emulatorIsConnectedAndEmulatorIsRunning) {
  // do something
} else {
  // do something else
}

如何检测模拟器是否运行?

TIA

这是一个答案,不一定是最佳答案,如果有其他答案,我将不胜感激。

基本上,我不仅在模拟存储,还在模拟云功能。

我做了这样一个云函数:

export const ping = functions.https.onRequest((req, res) => {
  res.json({ ping: 'pong' })
})

所以在我的代码中我只是 ping 这个端点。例如:

axios.get(`http://localhost:5001/${storageBucket}/us-central1/ping`)

如果它响应 200,则功能模拟器为 运行(根据我的设置,这也意味着存储模拟器为 运行)。