Sentry-javascript: Cannot spyOn captureException in v5

Created on 2 Apr 2019  路  4Comments  路  Source: getsentry/sentry-javascript

Package + Version

  • [x] @sentry/browser
  • [ ] @sentry/node
  • [ ] raven-js
  • [ ] raven-node _(raven for node)_
  • [ ] other:

Version:

5.0.3

Description

I can no longer use jasmine spyOn for the captureException call.

Error: <spyOn> : captureException is not declared writable or has no setter

This worked in 4.6.6, and I think it's ideal to have this possible for purposes of testing my integration with Sentry and my Angular 7 app.

These are the tests that fail:

import { ErrorHandler, Injector } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import * as Sentry from '@sentry/browser';

import { AppModule } from './app.module';
import { ENV, Environment } from './env.provider';

import { SentryErrorHandler } from './error-handler';

fdescribe('SentryErrorHandler', () => {
  let handler: ErrorHandler;
  let mockEnv: Environment;
  let sentrySpy: jasmine.Spy;

  beforeEach(() => {
    sentrySpy = spyOn(Sentry, 'captureException');
    mockEnv = { production: false, staticAssetsUrl: 'foo-bar-baz' };

    const injector = Injector.create({ providers: [
      { provide: ENV, useValue: mockEnv },
      { provide: ErrorHandler, useClass: SentryErrorHandler, deps: [ ENV ] },
    ] });
    handler = injector.get(ErrorHandler);
  });

  it('should be registered on the AppModule', () => {
    handler = TestBed.configureTestingModule({ imports: [ AppModule ] }).get(ErrorHandler);
    expect(handler).toEqual(jasmine.any(SentryErrorHandler));
  });

  it('handleError should call Sentry.captureException in production', () => {
    mockEnv.production = true;
    const error = new Error();
    handler.handleError(error);
    expect(sentrySpy).toHaveBeenCalledWith(error);
  });

  it('handleError should call console.error in dev mode', () => {
    spyOn(console, 'error');
    const error = new Error();
    handler.handleError(error);
    expect(console.error).toHaveBeenCalledWith(error);
  });
});

Is there another way to write tests for Sentry integration?

Needs Reproduction

Most helpful comment

@HazAT I was having the same issue. It's because captureException, with many others, is a direct export from @sentry/browser and because of this it's treated as a readonly constant. I believe Babel/TypeScript enforces this which may be why you're not getting the same problem by running it directly in node.

I don't necessarily think this is a problem. It appears to be by design. I've encountered it before a while back.

@rgant To solve your problem, you must use spyOnProperty instead. See here:

image

You can see the docs for spyOnProperty here: https://jasmine.github.io/api/edge/global.html#spyOnProperty

All 4 comments

I just tested the simple spyOn scenario using jasmine and @sentry/browser with no issue.

image

Can you provide a fully working repo with repro-case that I could use to debug this?

@HazAT I was having the same issue. It's because captureException, with many others, is a direct export from @sentry/browser and because of this it's treated as a readonly constant. I believe Babel/TypeScript enforces this which may be why you're not getting the same problem by running it directly in node.

I don't necessarily think this is a problem. It appears to be by design. I've encountered it before a while back.

@rgant To solve your problem, you must use spyOnProperty instead. See here:

image

You can see the docs for spyOnProperty here: https://jasmine.github.io/api/edge/global.html#spyOnProperty

Thank you @HazAT

Oops, thank you @ashpr :-D

Was this page helpful?
0 / 5 - 0 ratings