firebase/quickstart-testing

Can't run unit testing due to short mocha timeout

Closed this issue · 9 comments

Problem

Been trying to get unit-testing running for hours now. Still can't figure it out.

Steps

First followed the video here: https://www.youtube.com/watch?v=VDulvfBpzZE by Todd Kerpelman and Rachel Myers. Got some errors from that
FirebaseError: No matching allow statements

Then found this repo and downloaded the quick start to run. I followed the README exactly to run
firebase emulators:exec --only firestore "npm run test-firestore"

and now I got

$ firebase emulators:exec --only firestore "npm run test-firestore"
i  emulators: Starting emulators: firestore
i  firestore: Firestore Emulator logging to firestore-debug.log
i  Running script: npm run test-firestore

> unit-test-security-rules@1.0.0 test-firestore /Users/-/Downloads/quickstart-testing-master/unit-test-security-rules
> mocha --exit test/firestore.spec.js



  1) "before all" hook in "{root}"
View firestore rule coverage information at firestore-coverage.html


  0 passing (3s)
  1 failing

  1) "before all" hook in "{root}":
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
      at listOnTimeout (internal/timers.js:554:17)
      at processTimers (internal/timers.js:497:7)



npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! unit-test-security-rules@1.0.0 test-firestore: `mocha --exit test/firestore.spec.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the unit-test-security-rules@1.0.0 test-firestore script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/-/.npm/_logs/2020-10-15T21_07_25_807Z-debug.log
⚠  Script exited unsuccessfully (code 1)
i  emulators: Shutting down emulators.
i  firestore: Stopping Firestore Emulator
i  hub: Stopping emulator hub

Error: Script "npm run test-firestore" exited with code 1

A little help please?

I tried running it a couple more times with firebase emulators:start --only firestore in another terminal, and it ran. I don't know why it worked.

But now I'm getting another error

  7 passing (1s)
  1 failing

  1) "after all" hook in "{root}":
     Uncaught Error: getaddrinfo ENOTFOUND undefined
      at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26)

I've just disabled the coverage report for now

I figured parts of it out.

Turns out FirebaseError: No matching allow statements basically means PERMISSION_DENIED, and your assertSucceeds failed.

Also, this line is NECESSARY for firestore, otherwise nothing in your security rules will work.
match /databases/{database}/documents {

@youjinp hmm I'm not sure what's going on with your system but I was able to run this succesfully:

From within unit-test-security-rules

samstern@samstern-macbookpro3 unit-test-security-rules (master)
$ firebase emulators:exec --only firestore "npm run test-firestore"
i  emulators: Starting emulators: firestore
i  firestore: Firestore Emulator logging to firestore-debug.log
i  Running script: npm run test-firestore

> unit-test-security-rules@1.0.0 test-firestore /Users/samstern/Projects/quickstart-testing/unit-test-security-rules
> mocha --exit test/firestore.spec.js



  My app
    ✓ require users to log in before creating a profile (543ms)
    ✓ should enforce the createdAt date in user profiles (188ms)
    ✓ should only let users create their own profile (81ms)
    ✓ should let anyone read any profile (83ms)
    ✓ should let anyone create a room (47ms)
    ✓ should force people to name themselves as room owner when creating a room (50ms)
    ✓ should not let one user steal a room from another user (89ms)

View firestore rule coverage information at firestore-coverage.html


  7 passing (3s)

✔  Script exited successfully (code 0)
i  emulators: Shutting down emulators.
i  firestore: Stopping Firestore Emulator
i  hub: Stopping emulator hub

Maybe you don't have the right version of the Firebase CLI? What do you get when you run firebase --version? Also if you want you can replace firebase emulators:exec --only firestore "npm run test-firestore" with npx firebase emulators:exec --only firestore "npm run test-firestore" (just npx in front) to use the version inside this repo.

firebase --version returns 8.12.1

Running npx ... still gives me the timeout error

I've been able to resolve it by modifying the npm test script to increase the timeout for mocha

"test-firestore": "mocha --exit test/firestore.spec.js --timeout 5000"

Thanks!

@youjinp oh that's very interesting about the --timeout issue, thank you! I will re-open this issue to track adding better test timeouts.

I added the timeout to the mocha command and also get...

  1. "after all" hook in "{root}":
    Uncaught Error: getaddrinfo ENOTFOUND undefined
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26)

@jmagaram if you're getting ENOTFOUND you have a different issue, likely that the library can't find the host/port of your emulators. If you need help with that please file a new issue.

@samtstern just btw I have a similar issue, a more detailed thread can be found here: firebase/firebase-js-sdk#4674

I just did a big update of the test suite that handles multiple ways to run tests either with exec or with start. This should handle either use cases now.