firebase / firebase/firebase-js-sdk

initializeTestEnvironment sometimes failed due to Node.js fetch failed by SocketError: other side closed

Open
#8,690 8 comments 1 reaction 0 assignees View on GitHub
testing-sdk type: feature request
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### Operating System

Ubuntu 22.04

### Environment (if applicable)

GitHub Actions

### Firebase SDK Version

11.1.0

### Firebase SDK Product(s)

Firestore

### Project Tooling

React Native (Expo), Jest, TypeScript.

### Detailed Problem Description

Hi Firebase team,

We recently **occasionally** have this error in our GitHub workflow, we've never had this issue in local device:
```
TypeError: fetch failed
4 |
5 | export async function initializeTestEnv() {
> 6 | const testEnv = await initializeTestEnvironment({
| ^
7 | projectId: "xxx",
8 | firestore: {
9 | rules: fs.readFileSync("firestore.rules", "utf8"),
at async discoverEmulators (node_modules/@firebase/rules-unit-testing/src/impl/discovery.ts:31:15)
at async initializeTestEnvironment (node_modules/@firebase/rules-unit-testing/src/initialize.ts:66:32)
at async initializeTestEnv (lib/utils/unit-test.ts:6:19)
at async Object. (lib/api-client/__tests__/user-report-test.node.ts:16:15)
Cause:
SocketError: other side closed
```
It seems `await fetchImpl(makeUrl(hub, '/emulators'));` in `discoverEmulators` throwed the `SocketError: other side closed` and the codes didn't catch it then the codes crashed, can we retry when `discoverEmulators` catches such an error?

Our Github workflow is similar to below:
```yaml
jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "20.15"

- name: Install dependencies
run: |
npm install -g firebase-tools
yarn

- name: Lint
run: yarn eslint .

- name: Check TypeScript errors
run: yarn tsc --noEmit

- name: Run tests
run: firebase emulators:exec --only "firestore,auth,storage" "jest --runInBand --updateSnapshot --detectOpenHandles --forceExit --coverage"
```

We invoke `initializeTestEnv` like below:
```javascript
await initializeTestEnvironment({
projectId: "xxx",
firestore: {
rules: fs.readFileSync("firestore.rules", "utf8"),
host: 'localhost',
port: 8080,
},
storage: {
rules: fs.readFileSync("storage.rules", "utf8"),
host: 'localhost',
port: 9199,
},
hub: {
host: 'localhost',
port: 4400
}
})
```

We invoke it in each Jest unit test file in `beforeAll`, there are **5** Jest test files in our codebase, so this function is called 5 consecutive times.

### Steps and code to reproduce issue

N/A, this is an **occasional** issue and only observed in GitHub Actions workflows.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.