Tag: expect.js

Node.js中定义的AssertionError在哪里?

我希望我的unit testing断言特定的函数调用会在需要时抛出一个AssertionError,而不是抛出一个exception。 assertion库(expect)通过传入一个exception构造函数来支持这样的事情,但是我似乎无法findAssertionError构造函数被导出的地方。 它只是为了成为一个内部阶级,而不是暴露给我们? 文档包含大量的参考,但没有链接。 我有一个超级哈克的方式: let AssertionError; try { const assert = require("assert"); assert.fail(); } catch (ex) { AssertionError = ex.constructor; } 但我希望有更好的方法。

Webpack在Node.js App中打破摩卡

我有一个使用Mocha和Expect进行testing的node.js应用程序。 所有的testing工作很好,直到我安装了webpack的反应。 现在当我运行“npmtesting”时,出现以下错误: Error: Cannot find module 'should' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at Object.<anonymous> (C:\Users\Brian\version-control\tysons-tech-map-redone\node_modules\watchpack\test\DirectoryWatcher.test.js:2:1) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at C:\Users\Brian\version-control\tysons-tech-map-redone\node_modules\mocha\lib\mocha.js:230:27 at Array.forEach (native) at Mocha.loadFiles (C:\Users\Brian\version-control\tysons-tech-map-redone\node_modules\mocha\lib\mocha.js:227:14) at Mocha.run (C:\Users\Brian\version-control\tysons-tech-map-redone\node_modules\mocha\lib\mocha.js:495:10) […]

在NodeJS中重新抛出exception,不会丢失堆栈跟踪

如何在nodejs / javascript中重新引发错误或exception,并包含自定义消息。 我有以下代码 var json = JSON.parse(result); 而且我想在发生任何分析错误时将result内容包含在exception消息中。 像这样的东西。 1. try { 2. var json = JSON.parse(result); 3. expect(json.messages.length).to.be(1); 4. } catch(ex) { 5. throw new Error(ex.message + ". " + "JSON response: " + result); 6. } 这里的问题是我失去了我的堆栈跟踪。 有没有办法做到这一点类似于java ? throw new Error("JSON response: " + result, ex);

摩卡testing:未捕获TypeError:无法读取null的属性“状态”

学习TDD和我的“Hello World”服务器响应的第一个简单testing在Mocha中是失败的。 我正在使用Mocha.js,Superagent和Expect.js。 当我curl -i localhost:8080 ,我得到正确的响应和状态代码。 HTTP/1.1 200 OK Content-Type: text/plain Date: Mon, 27 Apr 2015 17:55:36 GMT Connection: keep-alive Transfer-Encoding: chunked Hello World testing代码: var request = require('superagent'); var expect = require('expect.js'); // Test structure describe('Suite one', function(){ it("should get a response that contains World",function(done){ request.get('localhost:8080').end(function(res){ // TODO check that response is okay […]