Extend global test object

Viewed 47

Is there any way to extend global jest test with some custom property? For example, I want implement some logic before test in this way:

test.debug({ /* ... */ }, 'test', () => {
  // ...
});
1 Answers

This can be done either in per-test setup file or custom environment, in case a customization needs to interact with the environment on a deeper level:

it.whatever = (whateverArg, ...args) => {
    const cb = args[1];
    if (cb) {
         args[1] = cb.length ?
            (done) => { /* do whatever with whateverArg */ return cb(done); } :
            () => { /* do whatever with whateverArg */ return cb(); }
    }

    return it(...args);
};

All arguments need to be passed to it as is, test callback needs to be additionally taken care of because whether a test is considered asynchronous depends on callback function arity.

Related