firebase / firebase/firebase-js-sdk

initializeTestEnvironment @firebase/rules-unit-testing should not require node process.env

Open
#8,795 10 comments 0 reactions 0 assignees View on GitHub
feature request needs-attention question testing-sdk
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### Operating System

macOS

### Environment (if applicable)

Any Modern Browser, Chrome

### Firebase SDK Version

@firebase/rules-unit-testing@4.0.1

### Firebase SDK Product(s)

Firestore, Storage

### Project Tooling

Angular App

```
$ ng version

_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/

Angular CLI: 19.1.7
Node: 20.18.1
Package Manager: npm 11.1.0
OS: darwin arm64

Angular: 19.1.6
... animations, common, compiler, compiler-cli, core, forms
... localize, platform-browser, platform-browser-dynamic, router
... service-worker

Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1901.7
@angular-devkit/build-angular 19.1.7
@angular-devkit/core 19.1.7
@angular-devkit/schematics 19.1.7
@angular/cli 19.1.7
@angular/fire 19.0.0
@schematics/angular 19.1.7
rxjs 7.8.1
typescript 5.7.3
zone.js 0.15.0
```

### Detailed Problem Description

[`emulatorFromEnvVar`](https://github.com/firebase/firebase-js-sdk/blob/b92592d98ba2e7f861ed02a59cfa81366c4f7231/packages/rules-unit-testing/src/impl/discovery.ts#L136) assumes that it is running in a Node.js environment with access to `process.env`, however this test library is useful for browser based applications.

It would be nice if this method could check for `process` to be defined before attempting to use it. It would also be nice if `initializeTestEnvironment` config (`TestEnvironmentConfig`) would allow you to skip configurations that you don't need. For example, I am running the emulator with `--only auth,firestore,storage`. So if `hub` could figure out that I have `firestore` and `storage` but not `database` running and just configure those that would be less noise in my code.

### Steps and code to reproduce issue

```
const testEnv = await initializeTestEnvironment({
// @firebase/rules-unit-testing assumes it is running in a node.js environment, but Angular
// is running in a browser with webpack and so `process is not defined` will be thrown here
// unless _every_ configuration is set so the code doesn't try to load one using environment
// variables.
// Also note that in order to add debugging statements to node_modules/@firebase/rules-unit-testing/dist/esm/index.esm.js
// that will actually be picked up by webpack you need to run `ng cache clean` after every
// change.
database: {
host: '125.0.0.1',
port: 9000,
},
firestore: {
host: '127.0.0.1',
port: 8080,
},
hub: {
host: '127.0.0.1',
port: 4400,
},
projectId: 'brainfry-app',
storage: {
host: '127.0.0.1',
port: 9199,
},
});
```

See the full app: https://github.com/rgant/brainfry/blob/47c25d00814520c4f90e7565bd9d601d2cb5c9dd/src/testing/firestore-data.ts#L28

Contributor guide

Open the contributing guide

Research direction

Start in packages/rules-unit-testing/src/impl/discovery.ts at emulatorFromEnvVar, then trace initializeTestEnvironment and TestEnvironmentConfig. Reproduce the Angular browser failure and determine how configurations are discovered when values are omitted. Done means browser use no longer requires process.env and the requested emulator configurations can be supplied without unrelated configuration noise.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, typescript
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.