firebase / firebase-functions-test

MIT License
232 stars 48 forks source link

fix(wrap): Extensions `functions.handler` -> `eventType` key exists but is `undefined` value #49

Closed Salakar closed 4 years ago

Salakar commented 4 years ago

Description

I've been working on adding tests to firebase/extensions and when trying to wrap the function handlers used by Extensions (e.g. functions.handler.firestore.document.onWrite) it errors with TypeError: Cannot read property 'match' of undefined as the eventType key 'does' exist on the defaultContext object, but has an undefined value assigned.

This small PR adds an additional undefined check.

export const fstranslate = functions.handler.firestore.document.onWrite(
  async (change): Promise<void> => {
  console.warn('Hello from inside my wrapped handler function.');
  return;
});

// ...
import * as functionsTestInit from "firebase-functions-test";
// ...
let functionsTest = functionsTestInit();
// ...

const wrappedFn = functionsTest.wrap(exportedFns.fstranslate);
const before = functionsTest.firestore.makeDocumentSnapshot({}, "foo/id1");
const after = functionsTest.firestore.makeDocumentSnapshot({ input: "hello" }, "foo/id1");
const change = functionsTest.makeChange(before, after);

wrappedFn(change); // Errors

Before: image

After: image

Code sample

N/A


cc @laurenzlong @karayu

Salakar commented 4 years ago

Thanks for the fix! there seems to be some TS errors

These are on master too - but I'll look into it, thanks!

Salakar commented 4 years ago

@laurenzlong fixed the types issue that was on master - breaking change in the chai types that was non semver, I've switched the version in package.json to use approximate versioning.

Came across another issue after though with the dev dependencies; the firebase-admin SDK was quite far out of date that the native GRPC dep wouldn't compile on the latest stable Node.js version - so I updated to the latest firebase-admin & firebase-functions versions. This broke the types as typescript was too old, updated typescript to fix, that broke mocha types as too old, and so on 😅 - so I've just updated all of the dev dependencies and all is working well now.

I also had to remove Node.js v6 testing from travis - newer firebase-functions versions uses async/await which isn't available on v6. Also the functions runtimes now are only v8 & v10 so have updated travis to reflect.

Hope these additional changes to get CI working again are ok?

laurenzlong commented 4 years ago

@Salakar This has now been released in v0.1.7

Salakar commented 4 years ago

@laurenzlong Thank you 🎉