-
Notifications
You must be signed in to change notification settings - Fork 407
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
Extend callbackToPromise tests #181
Extend callbackToPromise tests #181
Conversation
rmeritz
commented
Jun 9, 2020
- Add 100% coverage
- Test promise reject
- Test callbackArgIndex argument usage
d3127d0
to
9b84555
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testing Promise resolution values is unavoidably asynchronous. Since the tests don't return a Promise, Jest will consider them synchronous and move on before they're truly complete. If the Promises reject, Jest won't consider that a test failure.
For example, this change:
it('lets a function return a promise that can reject', function() {
var wrapped = callbackToPromise(rejectValue, 1);
- expect(wrapped(2)).rejects.toThrow(/reject this value/);
+ expect(wrapped(2)).rejects.toThrow(/reject this kangaroo/);
});
Here's the output from running the test suite
> [email protected] test-unit /home/mike/projects/bocoup/airtable/airtable.js
> jest --env node
(node:2036) UnhandledPromiseRejectionWarning: Error: expect(received).rejects.toThrow(expected)
Expected pattern: /reject this kangaroo/
Received message: "I reject this value"
9 |
10 | function rejectValue(value, callback) {
> 11 | callback(new Error('I reject this value'));
| ^
12 | }
13 |
14 | function sum() {
at Number.apply (test/callback_to_promise.test.js:11:18)
at lib/callback_to_promise.js:40:20
at wrapped (lib/callback_to_promise.js:32:20)
at Object.<anonymous> (test/callback_to_promise.test.js:30:16)
(node:2036) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:2036) [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.
PASS test/callback_to_promise.test.js
PASS test/update.test.js
PASS test/delete.test.js
PASS test/base.test.js
PASS test/create.test.js
PASS test/airtable.test.js
PASS test/record.test.js
PASS test/object_to_query_param_string.test.js
PASS test/has.test.js
PASS test/airtable_error.test.js
PASS test/node_version.test.js
PASS test/browser_build.test.js
Test Suites: 12 passed, 12 total
Tests: 106 passed, 106 total
Snapshots: 0 total
Time: 2.285s
Ran all test suites.
That looks like a test failure, but really, it's just Node.js reporting an unhandled rejection. When Jest finishes, it reports "12 passed, 12 total." More importantly, the process's exit code is still 0
, so it won't be detected in continuous integration.
This is a problem with the existing tests, so it's your call if you want to fix those in this pull request or in a follow-up.
I added |
test/callback_to_promise.test.js
Outdated
@@ -47,4 +56,9 @@ describe('callbackToPromise', function() { | |||
}); | |||
}); | |||
}); | |||
|
|||
it('can allow the user to explicately identify the index of the callback', function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/explicately/explicitly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
test/callback_to_promise.test.js
Outdated
@@ -47,4 +56,9 @@ describe('callbackToPromise', function() { | |||
}); | |||
}); | |||
}); | |||
|
|||
it('can allow the user to explicately identify the index of the callback', function() { | |||
var wrapped = callbackToPromise(returnThisPlusValue, 1, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be worth making this test the case where "if an explicit callbackArgIndex is set, but the function is called with too few arguments, we want to push undefined onto args so that our constructed callback ends up at the right index."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added more unit testing for all cases of callbackIndex being set
- Add 100% coverage - Test promise reject - Test callbackArgIndex argument usage
By returning the result https://jestjs.io/docs/en/expect.html#resolves
Include unit tests for all cases of the callback index being explicately set.
b8b2657
to
955616c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!