Javascript 如何在Jasmine测试中进入调试器?
我通过在Exercism.io上做练习来改进我的JavaScript;我目前正在研究 到目前为止,我已经编写了一个类似这样的Javascript 如何在Jasmine测试中进入调试器?,javascript,node.js,jasmine,node-inspector,Javascript,Node.js,Jasmine,Node Inspector,我通过在Exercism.io上做练习来改进我的JavaScript;我目前正在研究 到目前为止,我已经编写了一个类似这样的leap.js: var Year = function (year) {}; Year.prototype.isLeap = function () { return (this.year % 4 === 0 && this.year % 100 !== 0) || this.year % 400 === 0 }; module.exports
leap.js
:
var Year = function (year) {};
Year.prototype.isLeap = function () {
return (this.year % 4 === 0 && this.year % 100 !== 0) || this.year % 400 === 0
};
module.exports = Year;
Jasmine测试,leap.spec.js
,是
var Year = require('./leap');
describe('Leap year', function () {
it('is not very common', function () {
var year = new Year(2015);
expect(year.isLeap()).toBe(false);
});
it('is introduced every 4 years to adjust about a day', function () {
var year = new Year(2016);
expect(year.isLeap()).toBe(true);
});
it('is skipped every 100 years to remove an extra day', function () {
var year = new Year(1900);
expect(year.isLeap()).toBe(false);
});
it('is reintroduced every 400 years to adjust another day', function () {
var year = new Year(2000);
expect(year.isLeap()).toBe(true);
});
然而,一些测试仍然失败:
Kurts-MacBook-Pro:leap kurtpeek$ jasmine leap.spec.js
Started
.F.F
Failures:
1) Leap year is introduced every 4 years to adjust about a day
Message:
Expected false to be true.
Stack:
Error: Expected false to be true.
at UserContext.<anonymous> (/Users/kurtpeek/exercism/javascript/leap/leap.spec.js:11:27)
2) Leap year is reintroduced every 400 years to adjust another day
Message:
Expected false to be true.
Stack:
Error: Expected false to be true.
at UserContext.<anonymous> (/Users/kurtpeek/exercism/javascript/leap/leap.spec.js:21:27)
Ran 4 of 8 specs
4 specs, 2 failures
Finished in 0.009 seconds
我怀疑发生的是,今年不是2016年的数字,而是之前实例化的年的实例,因此模表达式没有意义
然而,为了确认这一点,我想检查isLeap
函数范围内的变量。在一些谷歌搜索之后,我尝试安装和,但是当我尝试运行其中任何一个(在调试器;
语句插入return
语句之前)时,我得到以下错误:
Kurts-MacBook-Pro:leap kurtpeek$ jasmine-node-debug
internal/modules/cjs/loader.js:550
throw err;
^
Error: Cannot find module '_debugger'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:548:15)
at Function.Module._load (internal/modules/cjs/loader.js:475:25)
at Module.require (internal/modules/cjs/loader.js:598:17)
at require (internal/modules/cjs/helpers.js:11:18)
at Object.<anonymous> (/usr/local/lib/node_modules/jasmine-node-debug/node_modules/node-inspector/lib/debugger.js:2:16)
at Module._compile (internal/modules/cjs/loader.js:654:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
at Module.load (internal/modules/cjs/loader.js:566:32)
at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
at Function.Module._load (internal/modules/cjs/loader.js:498:3)
Kurts MacBook Pro:leap kurtpeek$jasmine节点调试
内部/modules/cjs/loader.js:550
犯错误;
^
错误:找不到模块“\u调试器”
位于Function.Module.\u resolveFilename(internal/modules/cjs/loader.js:548:15)
at Function.Module._load(内部/modules/cjs/loader.js:475:25)
at Module.require(内部/modules/cjs/loader.js:598:17)
根据需要(内部/modules/cjs/helpers.js:11:18)
在Object.,此错误与Node.js团队将用户迁移到新的inspect
API有关-基本上,这些包已经过时
有没有其他方法可以通过Jasmine测试进入调试器?您今年没有在任何地方设置 在函数isLeap()中,this.year未定义,因此通过了falsy的规范 您可以在规范中控制台.log(),并随时查看其值。它将打印到终端以及浏览器实例。 您也可以尝试dump() 试试这个:
var Year = function (year) {
this.year = year;
};
您还可以使用以下命令确认本年度持有的数据类型
expect(this.year).toEqual(jasmine.any(Number));
我通过运行
node--inspect brk
启动了调试器,其中jasmine的CLI调用了jasmine.js
文件:
Kurts-MacBook-Pro:bin kurtpeek$ node --inspect-brk /usr/local/lib/node_modules/jasmine/bin/jasmine.js ~/exercism/javascript/leap/leap.spec.js
Debugger listening on ws://127.0.0.1:9229/03d10b73-1d60-4db3-be79-850f7ee4d0d6
For help see https://nodejs.org/en/docs/inspector
Debugger attached.
Started
然后,在导航到chrome://inspect
在Chrome浏览器中点击“继续”,我可以确定今年的确实是未定义的:
然后,我通过定义今年的,修复了测试:
var Year = function (year) {
this.year = year;
};
Year.prototype.isLeap = function () {
return (this.year % 4 === 0 && this.year % 100 !== 0) || this.year % 400 === 0
};
module.exports = Year;
现在测试通过了:
Kurts-MacBook-Pro:bin kurtpeek$ jasmine ~/exercism/javascript/leap/leap.spec.js
Started
....
Ran 4 of 8 specs
4 specs, 0 failures
Finished in 0.009 seconds
我的问题不在于如何解决这个问题,而在于如何进入调试器,这样我就可以掌握所有变量,并在将来更快地解决这些问题?
Kurts-MacBook-Pro:bin kurtpeek$ jasmine ~/exercism/javascript/leap/leap.spec.js
Started
....
Ran 4 of 8 specs
4 specs, 0 failures
Finished in 0.009 seconds