skovhus / jest-codemods

Codemods for migrating to Jest https://github.com/facebook/jest 👾
MIT License
881 stars 82 forks source link

Ava's test.throws accepts Promise value #27

Open ahutchings opened 7 years ago

ahutchings commented 7 years ago

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.

skovhus commented 7 years ago

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 commented 7 years ago

@cpojer any suggestions?

cpojer commented 7 years ago

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

skovhus commented 7 years ago

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

skovhus commented 7 years ago

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

ahutchings commented 7 years ago

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:

> jest-codemods-27@1.0.0 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)
skovhus commented 7 years ago

Thanks!

jeffijoe commented 7 years ago

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. 😄