Skip to content

AngularFirePerformance incompatible with Jest #2812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spicemix opened this issue May 4, 2021 · 4 comments
Closed

AngularFirePerformance incompatible with Jest #2812

spicemix opened this issue May 4, 2021 · 4 comments

Comments

@spicemix
Copy link

spicemix commented May 4, 2021

If I import AngularFirePerformance anywhere reachable by my Jest unit tests (part of @nrwl/nx) I get

TypeError: window.performance.mark is not a function

The error is right on the import, no additional code needed.

The likelihood is this is an incompatibility with Jest's performance-related code. Trying to copy over values from perf_hooks didn't work although it changed the error reports. I am opening the issue here to see if anyone has ideas how to fix this. I am using the latest stable versions of everything.

@google-oss-bot
Copy link

This issue does not seem to follow the issue template. Make sure you provide all the required information.

@spicemix
Copy link
Author

spicemix commented May 4, 2021

Line 61 of performance.ts looks like it may be the culprit: it's assumed here that if you have window.performance you will also have mark, but it seems Jest strips out the mark function. So checking that mark is also there on line 56 might fix the issue although it might prevent the code from being run in a Jest test. Of course I don't need AngularFirePerformance on my Jest runs, but I did want to work with the emulators rather than mocking out all of firebase for those tests. I'm not sure I could mock that function out before the error is thrown as a means of preventing it without modifying this code.

const trace$ = (traceId: string) => {
  if (typeof window !== 'undefined' && window.performance) {
    const entries = window.performance.getEntriesByName(traceId, 'measure') || [];
    const startMarkName = `_${traceId}Start[${entries.length}]`;
    const endMarkName = `_${traceId}End[${entries.length}]`;
    return new Observable<void>(emitter => {
      window.performance.mark(startMarkName);
      emitter.next();
      return {
        unsubscribe: () => {
          window.performance.mark(endMarkName);
          window.performance.measure(traceId, startMarkName, endMarkName);
        }
      };
    });
  } else {
    return EMPTY;
  }
};

@stephen-wigginton
Copy link

as a work around, adding the following to your jest.ts will fix it as well:

Object.defineProperty(window.performance, 'mark', {
  value: () => mock()
});```

@jamesdaniels
Copy link
Member

Will be addressed in next patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants