首页 > 解决方案 > JavaScript 异常混乱

问题描述

更新:此问题已通过指向此问题的链接关闭,但请注意链接页面没有接受的答案。


我有以下mocha测试:

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');
  });
});

当我运行它时,我希望一切都会通过,但我得到的是:

% 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

JavaScript 中的常规函数​​和常规函数之间的差异让我有些困惑……当被调用的函数 ( ) 抛出async时,我应该在这个文件中进行哪些更改才能通过测试。asyncfunctionWithError()

标签: javascriptmocha.jschai

解决方案


推荐阅读