firebase / firebase/firebase-js-sdk

Karma browser-based code with rules unit testing

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

Description

### [REQUIRED] Describe your environment

* Operating System version: ___MacOS Monterey__
* Browser version: __Chrome Version 103.0.5060.53 (Official Build) (x86_64)___
* Firebase SDK version: _@firebase/rules-unit-testing _2.0.3___
* Firebase Product: _database____ (auth, database, storage, etc)

#### Steps to reproduce:

- We are using Angular Material and AngularFire Compat which with Jasmine & Karma
- First of all, we can't use `fs` to read the rule.json while initializing the test because Karma is only for testing browser-based code
- And, we couldn't do a plain string way because the below error which is the problem of browser-based webApp won’t be able to use [‘node-fetch’](https://github.com/firebase/firebase-js-sdk/blob/master/packages/rules-unit-testing/src/impl/rules.ts#L32) => const resp = await fetch(url, {

#### Relevant Code:

```javascript
this.testEnv = await initializeTestEnvironment({
projectId: environment.firebase.projectId,
hub: {
host: 'localhost',
port: 4400
},
database: {
rules: '{ "rules": {} }',
}
});
```

```
TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation

at http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:1465:1
at Object.proto. [as default] (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:975:1)
at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@firebase/rules-unit-testing/dist/index.cjs.js:507:1
at step (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@firebase/rules-unit-testing/dist/index.cjs.js:74:1)
at Object.next (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@firebase/rules-unit-testing/dist/index.cjs.js:55:46)
at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@firebase/rules-unit-testing/dist/index.cjs.js:48:1
at new ZoneAwarePromise (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/fesm2015/zone.js:1387:1)
at __awaiter (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@firebase/rules-unit-testing/dist/index.cjs.js:44:1)
at loadDatabaseRules (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@firebase/rules-unit-testing/dist/index.cjs.js:500:1)
at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@firebase/rules-unit-testing/dist/index.cjs.js:655:1
```

Is any better way to support "webApp" for @firebase/rules-unit-testing? Also, is "js-sdk" should be "plain" JS without NodeJs?

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.