Upon inspection, it appears that the objects failApiClient
and explicitFailApiClient
should be of the same type. When logging them, they seem to have identical outputs:
console.log(failApiClient) // { getObjects: [Function: getObjects] }
console.log(explicitFailApiClient) // { getObjects: [Function: getObjects] }
After reading this question, I learned how to handle this situation correctly. However, the reason why the generated failApiClient
triggers a warning while explicitFailApiClient
does not is still unclear.
I have simplified this scenario to its core for replication purposes and to showcase functional alternatives:
import * as sinon from 'sinon';
import 'source-map-support/register';
class LocalObject {
}
const fakeObject = new LocalObject();
const getFakeApi = (result: Promise<LocalObject[]>) = ({getObjects: () => result});
const successObjectClient = getFakeApi(Promise.resolve([fakeObject]));
// Both lines below should behave similarly, yet one causes a test error
const failApiClient = getFakeApi(Promise.reject(new Error()));
const explicitFailApiClient = {
getObjects(): Promise<LocalObject[]> {
return Promise.reject(new Error());
}
};
describe('successApiClient', () => {
before(() => {
sinon.spy(successObjectClient, 'getObjects');
});
it('does not generate a warning', async () => {
// do nothing
});
});
describe('failApiClient', () => {
before(() => {
sinon.spy(failApiClient, 'getObjects');
});
it('should not produce a warning', async () => {
// do nothing
});
});
describe('explicitFailApiClient', () => {
before(() => {
sinon.spy(explicitFailApiClient, 'getObjects');
});
it('does not trigger a warning', async () => {
// do nothing
});
});
The outcome of running
~/...> tsc && npm test
:
> <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e67607a6b7c606f62236f7e674e3f203e203e">[email protected]</a> test /Users/./Projects/./node/internal-api
> grunt test
Running "test" task
Running "env:dev" (env) task
Running "simplemocha:unit" (simplemocha) task
(node:72101) UnhandledPromiseRejectionWarning: Error
at Object.<anonymous> (/Users/./Projects/./node/internal-api/src/test/unit/models/mvp.test.ts:21:57)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (/Users/./Projects/./node/internal-api/node_modules/coffee-script/lib/coffee-script/register.js:45:36)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at /Users/./Projects/./node/internal-api/node_modules/mocha/lib/mocha.js:222:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/Users/./Projects/./node/internal-api/node_modules/mocha/lib/mocha.js:219:14)
at Mocha.run (/Users/./Projects/./node/internal-api/node_modules/mocha/lib/mocha.js:487:10)
[…]
(node:72101) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise […]
successApiClient
✓ does not have a warning
failApiClient
✓ should not have a warning
(node:72101) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously […]
explicitFailApiClient
✓ does not have a warning
3 passing (14ms)
Done.