Tuesday, 21 May 2019

How to add global commands to Jest like describe and it?

I'm writing a little testing tool for Jest (just to learn). It is called assertTruthy(msg, fn, args), expects a message, a function and arguments and should pass if the thing that is returned when the function is invoked with the arguments is truthy and fail if its not.

I would like to add it to Jest, so that you could call it without importing it in every environment.

describe('someFn()', () => {
  // like 'it', 'describe' and 'expect' it should just be available here
  it('is a function', () => {
    expect(typeop someFN).toEqual('Function');
  });

  assertTruthy('it should return true', someFn, 3, 4);
});

I know Jest has setupFiles and setupFilesAfterEnv but I can't figure out how to use them to achieve this.

How do you add commands to Jest?

PS: On a single project basis (in CRA) I managed to do this like this:

// in src/setupFiles.js
const assertTruthy = // function definition ...
global.assertTruthy = assertTruthy



from How to add global commands to Jest like describe and it?

No comments:

Post a Comment