无法使用 cypress 使用 firestore 本地模拟器测试应用程序

can't use cypress to test app using using firestore local emulator

我有一个使用 vue 和 firebase/firestore 构建的应用程序。我使用 firebase 模拟器进行本地开发,并尝试将我的开发工作流程与 cypress 集成。但是我在 cypress 中收到一个错误,如果我从浏览器访问该应用程序,则不会发生该错误。

Firebase CLI 版本为 7.9.0,Cypress 版本为“^3.8.0”

我加载所有内容的 npm 脚本如下:

"start": "firebase emulators:exec --only firestore \"npm run dev:appandtest\"",
"dev:appandtest": "concurrently -n \"app,test\" -c \"bgYellow.black,bgWhite.black\" \"npm:dev:app\" \"npm:dev:test\"",
"dev:app": "webpack-dev-server --config build/webpack.dev.js",
"dev:test": "npx cypress open", 

本地服务器 运行s 在端口 9000 上,firebase 模拟器在端口 8080 上。

在 运行ning 之后,如果我从普通浏览器访问应用程序,一切正常,如屏幕所示。

正常

然后我尝试运行使用此代码进行基本的柏树测试

    describe('The Home Page', function () {
      it('successfully loads', function () {
        cy.visit('/');
      });
    });

我收到以下错误消息:

    [2019-12-14T15:29:24.725Z]  @firebase/firestore: Firestore (6.6.2): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.
    This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

    error.ts:166 Uncaught (in promise) FirebaseError: Failed to get document because the client is offline.
        at new FirestoreError (http://localhost:9000/bundle.js:11739:149)
        at Object.next (http://localhost:9000/bundle.js:16734:8)
        at next (http://localhost:9000/bundle.js:16725:4704)
        at http://localhost:9000/bundle.js:16430:411

我也截图了: 越野车

我试图研究答案,但没能找到答案。在此先感谢您的帮助。

这个问题的解决方案,至少现在,是启用 experimentalForceLongPolling,像这样:

// NOTE: do NOT put this in production.
firebase.firestore().settings({ experimentalForceLongPolling: true })

重要提示:这是一项实验性功能,您应该将其放入环境变量的一些条件检查中。你不应该在生产环境中使用它。

最好描述其原因 here

The default behavior of Firestore's web SDK is to make use of WebChannel's streaming mode. The client makes what looks like an XHR, but then the server will hold the response open for 60 seconds and send as many server-initiated responses as it can during that time window.

The experimentalForLongPolling option forces the server to send only a single response per request.

here

That is the same workaround we are using in cypress. I think the underlying problem is that Cypress is intercepting all network traffic so it can monitor and sometimes mock. However, the webchannel protocol used by firestore has multiple replies over the same http request. The Cypress code cannot handle this and will only forward the first reply and ignore the rest.