jest-codemods icon indicating copy to clipboard operation
jest-codemods copied to clipboard

Ava's test.throws accepts Promise value

Open ahutchings opened this issue 8 years ago • 8 comments

When Ava's test.throws is used with a Promise value, it cannot be transformed directly to a Jest .toThrowError call, since the latter does not support Promise values.

Any ideas on what can be done here? facebook/jest#1377 looks like it might be related.

ahutchings avatar Dec 16 '16 23:12 ahutchings

Thanks for reporting. I overlooked that missing feature... We cannot reliably detect if an argument is a promise, so I'm not sure what the right solution is.

After the transformation, which error do you get from Jest?

skovhus avatar Dec 17 '16 08:12 skovhus

@cpojer any suggestions?

skovhus avatar Dec 17 '16 09:12 skovhus

nothing yet beyond the last few suggestions in the issue mentioned above.

cpojer avatar Dec 17 '16 21:12 cpojer

@ahutchings seems like this is fixed in https://github.com/facebook/jest/pull/3068 ... Does Jest>20 work for you?

skovhus avatar Jun 05 '17 11:06 skovhus

@ahutchings did you have time to test newest version of Jest? Thanks.

skovhus avatar Aug 14 '17 22:08 skovhus

I just tried this with jest@20, and there is at least one case that does not work.

Input:

import test from 'ava';

test('promise throws', async t => {
  const error = new Error('Some error');
  const promise = Promise.reject(error);

  const err = await t.throws(promise);
  t.is(err.message, 'Some error');
});

Output:

test('promise throws', async () => {
  const error = new Error('Some error');
  const promise = Promise.reject(error);

  const err = await expect(promise).toThrow();
  expect(err.message).toBe('Some error');
});

Console Output:

> [email protected] test /Users/ahutchin/Projects/jest-codemods-27
> jest

(node:70794) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Some error
(node:70794) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
 FAIL  ./test.js
  ● promise throws

    expect(function).toThrow(undefined)
    
    Received value must be a function, but instead "object" was found
      
      at Object.<anonymous>.test (test.js:5:37)
          at Promise (<anonymous>)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

  ✕ promise throws (4ms)

ahutchings avatar Aug 15 '17 14:08 ahutchings

Thanks!

skovhus avatar Aug 15 '17 16:08 skovhus

I had the same issue, except I was dependent on the return value of t.throws from AVA.

What I did was a manual find-replace t.throws -> throws, then added

import { throws } from 'smid'

to every file, followed by yarn add --dev smid.

smid basically returns the thrown error, or throws an error if no error is thrown... damn that was a lot of throw. 😄

jeffijoe avatar Oct 13 '17 13:10 jeffijoe