Jest unit-testing if super() is called

一个人想着一个人 提交于 2019-12-10 15:46:06

问题


I have a custom error class that extends the built-in Error class in Javascript. The problem I came up with is that "super()" method is not checked if it is called or not through my Jest unit testing.

export class AppError extends Error {
  public name: string;
  public message: string;
  public status?: number;
  public data?: any;
  constructor(message: string, status?: number, data?: any) {
    super(); <-- this guy!!
    this.name = 'AppError';
    this.status = status || 500;
    this.message = message;
    this.data = data;
  }
}

Is there any way to test it? Thanks.


回答1:


There's no reason to check if super() is called neither in native ES6 classes nor in classes transpiled with Babel.

Not calling super in child class constructor will result in error on class instantiation:

ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor

Babel provides a safeguard for that as well:

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

It may be possible to check that parent constructor is called (could be useful to assert super() arguments) by mocking child class prototype, something like:

let ParentOriginal;
let ParentMock;

beforeEach(() => {
  ParentOriginal = Object.getPrototypeOf(AppError);
  ParentMock = jest.fn();
  Object.setPrototypeOf(AppError, ParentMock);
});

it('..', () => {
  new AppError(...);
  expect(ParentMock.mock.calls.length).toBe(1);
})

afterEach(() => {
  Object.setPrototypeOf(AppError, ParentOriginal);
});

It's expected to mock super in both native classes and classes transpiled with Babel.

But this test is redundant, because missing super() will result in error any way. Testing that AppError inherits from Error is everything that needs be tested here:

expect(new AppError(...)).toBeInstanceOf(Error)


来源:https://stackoverflow.com/questions/51216056/jest-unit-testing-if-super-is-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!