0

UPDATE: This question was closed with a link to this question, but note that the linked page has no accepted answers.


I have the following mocha test:

import 'mocha';
import { expect } from 'chai';

describe('simple test', () => {
  const functionWithError = async () => {
    throw new Error('abc');
  };

  it('should work in a regular function', () => {
    const execute = () => functionWithError();
    expect(execute).to.throw(Error, 'abc');
  });

  it('might work in an async function?', () => {
    const execute = async () => await functionWithError();
    expect(execute).to.throw(Error, 'abc');
  });
});

When I run this, I expect everything to pass, but I get instead:

% npm run test

> functions@ test /home/me/repos/exception-tests
> mocha -r ts-node/register test/**/*.spec.ts



  simple test
    1) should work in a regular function
(node:2997) UnhandledPromiseRejectionWarning: Error: abc
    at functionWithError (/home/me/repos/exception-tests/test/confused.spec.ts:6:11)
    at execute (/home/me/repos/exception-tests/test/confused.spec.ts:10:27)
    at Proxy.assertThrows (/home/me/repos/exception-tests/node_modules/chai/lib/chai/core/assertions.js:2630:7)
    at Proxy.methodWrapper (/home/me/repos/exception-tests/node_modules/chai/lib/chai/utils/addMethod.js:57:25)
    at Context.it (/home/me/repos/exception-tests/test/confused.spec.ts:11:29)
    at callFn (/home/me/repos/exception-tests/node_modules/mocha/lib/runnable.js:358:21)
    at Test.Runnable.run (/home/me/repos/exception-tests/node_modules/mocha/lib/runnable.js:346:5)
    at Runner.runTest (/home/me/repos/exception-tests/node_modules/mocha/lib/runner.js:621:10)
    at /home/me/repos/exception-tests/node_modules/mocha/lib/runner.js:745:12
    at next (/home/me/repos/exception-tests/node_modules/mocha/lib/runner.js:538:14)
    at /home/me/repos/exception-tests/node_modules/mocha/lib/runner.js:548:7
    at next (/home/me/repos/exception-tests/node_modules/mocha/lib/runner.js:430:14)
    at Immediate.<anonymous> (/home/me/repos/exception-tests/node_modules/mocha/lib/runner.js:516:5)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
(node:2997) 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(). (rejection id: 1)
(node:2997) [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.
    2) might work in an async function?
(node:2997) UnhandledPromiseRejectionWarning: Error: abc
    at functionWithError (/home/me/repos/exception-tests/test/confused.spec.ts:6:11)
    at execute (/home/me/repos/exception-tests/test/confused.spec.ts:15:39)
    at Proxy.assertThrows (/home/me/repos/exception-tests/node_modules/chai/lib/chai/core/assertions.js:2630:7)
    at Proxy.methodWrapper (/home/me/repos/exception-tests/node_modules/chai/lib/chai/utils/addMethod.js:57:25)
    at Context.it (/home/me/repos/exception-tests/test/confused.spec.ts:16:29)
    at callFn (/home/me/repos/exception-tests/node_modules/mocha/lib/runnable.js:358:21)
    at Test.Runnable.run (/home/me/repos/exception-tests/node_modules/mocha/lib/runnable.js:346:5)
    at Runner.runTest (/home/me/repos/exception-tests/node_modules/mocha/lib/runner.js:621:10)
    at /home/me/repos/exception-tests/node_modules/mocha/lib/runner.js:745:12
    at next (/home/me/repos/exception-tests/node_modules/mocha/lib/runner.js:538:14)
    at /home/me/repos/exception-tests/node_modules/mocha/lib/runner.js:548:7
    at next (/home/me/repos/exception-tests/node_modules/mocha/lib/runner.js:430:14)
    at Immediate.<anonymous> (/home/me/repos/exception-tests/node_modules/mocha/lib/runner.js:516:5)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
(node:2997) 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(). (rejection id: 3)


  0 passing (15ms)
  2 failing

  1) simple test
       should work in a regular function:
     AssertionError: expected [Function: execute] to throw Error
      at Context.it (test/confused.spec.ts:11:29)

  2) simple test
       might work in an async function?:
     AssertionError: expected [Function: execute] to throw Error
      at Context.it (test/confused.spec.ts:16:29)



npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ test: `mocha -r ts-node/register test/**/*.spec.ts`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the functions@ test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/me/.npm/_logs/2020-11-19T02_45_43_745Z-debug.log

The differences between async and regular functions in JavaScript confuses me somewhat... What do I change in this file to get a passing test when the called async function (functionWithError()) throws.

seawolf
  • 2,147
  • 3
  • 20
  • 37
  • You have to put your `functionWithError` into a `try catch` to see if it throws – Dominik Nov 19 '20 at 02:35
  • @Dominik Don't you think the unit testing framework does that to implement `expect().to.throw()`? – Barmar Nov 19 '20 at 02:37
  • Oh I thought they were executing `functionWithError` on the line above but they just put it into another function. That would be the issue then. You have to make that wrapping function `async` as well. – Dominik Nov 19 '20 at 02:39
  • Try `const execute = async () => await functionWithError();`... I'm guessing here... Can't test this _(I assume you need to wrap it for other reasons as you don't in this example)_ – Dominik Nov 19 '20 at 02:40
  • I've updated the code to demonstrate that the change above does not change the output. – seawolf Nov 19 '20 at 02:47

0 Answers0