Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ava's test.throws accepts Promise value #27

Open
ahutchings opened this issue Dec 16, 2016 · 8 comments
Open

Ava's test.throws accepts Promise value #27

ahutchings opened this issue Dec 16, 2016 · 8 comments

Comments

@ahutchings
Copy link
Contributor

ahutchings commented Dec 16, 2016

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? jestjs/jest#1377 looks like it might be related.

@skovhus
Copy link
Owner

skovhus commented Dec 17, 2016

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
Copy link
Owner

skovhus commented Dec 17, 2016

@cpojer any suggestions?

@cpojer
Copy link

cpojer commented Dec 17, 2016

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

@skovhus
Copy link
Owner

skovhus commented Jun 5, 2017

@ahutchings seems like this is fixed in jestjs/jest#3068 ... Does Jest>20 work for you?

@skovhus
Copy link
Owner

skovhus commented Aug 14, 2017

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

@ahutchings
Copy link
Contributor Author

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)

@skovhus
Copy link
Owner

skovhus commented Aug 15, 2017

Thanks!

@jeffijoe
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants