mattphillips / jest-chain

Chain Jest matchers together to create one powerful assertion 🃏⛓

Home Page:https://www.npmjs.com/package/jest-chain

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

One-line expects generate "Jest: `it` and `test` must return either a Promise or undefined"

amercier opened this issue · comments

Summary

jest-chain throws an error when used with one-line expect (arrow functions).

Description

Please consider the following code:

function sum(a, b) { return a + b; }

describe('sum', () => {
  it('returns 4 for [1, 3]', () => expect(sum(1, 3)).toBe(4));
  it('returns -2 for [1, -3]', () => expect(sum(1, -3)).toBe(-2));
  it('returns 3 for [0, 3]', () => expect(sum(0, 3)).toBe(3));
});
  1. Running this without jest-chain works as expected.
  2. Running this with jest-chain fails with the following error:
Jest: `it` and `test` must return either a Promise or undefined.

      at Object.asyncFn (node_modules/jest-jasmine2/build/jasmine_async.js:126:11)

Versions

  • package version: 0.7.2
  • node version: v10.4.1
  • npm version: 6.1.0

Notes

  1. FWIW, I think one-line arrow functions should be supported, as they add a lot of clarity in some cases.
  2. I use the following as a (hopefully temporary) workaround:
// Workaround for https://github.com/mattphillips/jest-chain/issues/1
const it2 = (message, fn) => it(message, async (...args) => fn(...args));

describe('sum', () => {
  it2('returns 4 for [1, 3]', () => expect(sum(1, 3)).toBe(4));
  it2('returns -2 for [1, -3]', () => expect(sum(1, -3)).toBe(-2));
  it2('returns 3 for [0, 3]', () => expect(sum(0, 3)).toBe(3));
});

Hey @amercier thanks for raising this, I wasn't aware of this error inside of Jest 😄

This one will be super hard to solve in userland and feels like potentially it needs some more thought in Jest's core itself. I've raised an issue (above) in Jest to remove this error so I'll keep this open for now and hopefully we can get this closed off by stopping Jest from throwing when returning something other than a Promise/undefined