如何使用mocha和Babel在SyntaxErrors上获得更短的堆栈跟踪?

时间:2017-05-26 22:09:26

标签: javascript mocha babeljs

当我遇到语法错误时,我得到了这个巨大的堆栈跟踪。我想知道在babel或mocha中是否有一个选项,我可以用它来缩短它们。我尝试过使用mocha-clean,但没有什么区别。

stacktrace:

watch-run --initial --pattern 'lib/**/*,test/**/*' npm run test

/myProject/node_modules/babel-core/lib/transformation/file/index.js:600
      throw err;
      ^

SyntaxError: /myProject/lib/sentence-sequence/states.js: Invalid left-hand side in assignment expression (4:2)
  2 |   States.states = states
  3 |
> 4 |   States.states.isPending(i) = States.states[i] === States.PENDING
    |   ^
  7 |   States.states.isMisplaced(i) = States.states[i] === States.MISPLACED
    at Parser.pp$5.raise (/myProject/node_modules/babylon/lib/index.js:4373:13)
    at Parser.pp$2.toAssignable (/myProject/node_modules/babylon/lib/index.js:2941:16)
    at Parser.pp$3.parseMaybeAssign (/myProject/node_modules/babylon/lib/index.js:3282:45)
    at Parser.pp$3.parseExpression (/myProject/node_modules/babylon/lib/index.js:3239:19)
    at Parser.pp$1.parseStatement (/myProject/node_modules/babylon/lib/index.js:1861:19)
    at Parser.pp$1.parseBlockBody (/myProject/node_modules/babylon/lib/index.js:2223:21)
    at Parser.pp$1.parseBlock (/myProject/node_modules/babylon/lib/index.js:2202:8)
    at Parser.pp$3.parseFunctionBody (/myProject/node_modules/babylon/lib/index.js:4154:22)
    at Parser.pp$1.parseFunction (/myProject/node_modules/babylon/lib/index.js:2341:8)
    at Parser.pp$1.parseFunctionStatement (/myProject/node_modules/babylon/lib/index.js:2008:15)
    at Parser.pp$1.parseStatement (/myProject/node_modules/babylon/lib/index.js:1794:19)
    at Parser.pp$1.parseBlockBody (/myProject/node_modules/babylon/lib/index.js:2223:21)
    at Parser.pp$1.parseTopLevel (/myProject/node_modules/babylon/lib/index.js:1733:8)
    at Parser.parse (/myProject/node_modules/babylon/lib/index.js:1628:17)
    at parse (/myProject/node_modules/babylon/lib/index.js:7083:37)
    at File.parse (/myProject/node_modules/babel-core/lib/transformation/file/index.js:527:15)
    at File.parseCode (/myProject/node_modules/babel-core/lib/transformation/file/index.js:612:20)
    at /myProject/node_modules/babel-core/lib/transformation/pipeline.js:49:12
    at File.wrap (/myProject/node_modules/babel-core/lib/transformation/file/index.js:574:16)
    at Pipeline.transform (/myProject/node_modules/babel-core/lib/transformation/pipeline.js:47:17)
    at Object.transformFileSync (/myProject/node_modules/babel-core/lib/api/node.js:152:10)
    at compile (/myProject/node_modules/babel-register/lib/node.js:118:20)
    at loader (/myProject/node_modules/babel-register/lib/node.js:144:14)
    at Object.require.extensions.(anonymous function) [as .js] (/myProject/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/myProject/lib/sentence-sequence/index.js:4:16)
    at Module._compile (module.js:571:32)
    at loader (/myProject/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/myProject/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/myProject/lib/whatever.js:9:26)
    at Module._compile (module.js:571:32)
    at loader (/myProject/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/myProject/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/myProject/test/test-whatever.js:5:21)
    at Module._compile (module.js:571:32)
    at loader (/myProject/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/myProject/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at /myProject/node_modules/mocha/lib/mocha.js:222:27
    at Array.forEach (native)
    at Mocha.loadFiles (/myProject/node_modules/mocha/lib/mocha.js:219:14)
    at Mocha.run (/myProject/node_modules/mocha/lib/mocha.js:487:10)
    at Object.<anonymous> (/myProject/node_modules/mocha/bin/_mocha:459:18)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:425:7)
    at startup (bootstrap_node.js:146:9)
    at bootstrap_node.js:540:3

npm ERR! Darwin 16.5.0
npm ERR! argv "/usr/local/Cellar/node/7.7.2/bin/node" "/usr/local/bin/npm" "run" "test"
npm ERR! node v7.7.2
npm ERR! npm  v4.1.2
npm ERR! code ELIFECYCLE
npm ERR! myProject@1.0.1 test: `mocha --recursive --require babel-register --reporter spec --colors test/`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the myProject@1.0.1 test script 'mocha --recursive --require babel-register --reporter spec --colors test/'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the myProject package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     mocha --recursive --require babel-register --reporter spec --colors test/
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs myProject
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls myProject
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /myProject/npm-debug.log

1 个答案:

答案 0 :(得分:0)

我最终切换到mocha-webpack,现在语法错误堆栈跟踪要短得多:

ERROR in ./lib/sentence-sequence/states.js
Module parse failed: /myProject/lib/sentence-sequence/states.js Unexpected token (4:30)
You may need an appropriate loader to handle this file type.
|   States.states = states
|
|   States.states.isPending(i) => States.states[i] === States.PENDING
 @ ./test/test-sentence-sequence.js 6:15-57
 @ ./test nonrecursive object Object
 @ ./.tmp/mocha-webpack/3bd9f7b3e26640b8ee82e62a985a1e73-entry.js