[英]How to drop into a debugger in a Jasmine test?
我通過在Exercism.io上進行練習來改善我的JavaScript; 我目前正在http://exercism.io/exercises/javascript/leap/readme上工作。
到目前為止,我已經這樣編寫了一個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;
茉莉花測試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
奇怪的是,如果我復制的內容是return
版的節點REPL與this.year
改為2016
,我得到true
預期:
Kurts-MacBook-Pro:leap kurtpeek$ node
> (2016 % 4 === 0 && 2016 % 100 !== 0) || 2016 % 400 === 0
true
我懷疑正在發生的事情是this.year
不是數字2016
,而是先前實例化的Year
實例year
,因此模表達式沒有意義。
為了確認這一點,我想檢查isLeap
函數范圍內的變量。 經過一番谷歌搜索之后,我嘗試安裝jasmine-debug
和jasmine-node-debug
,但是當我嘗試運行其中一個(在插入debugger;
在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)
根據我在https://github.com/angular/protractor/issues/4307上閱讀的內容,此錯誤與Node.js團隊將用戶遷移到新的inspect
API有關-基本上,這些軟件包已過時。
還有其他方法可以通過Jasmine測試進入調試器嗎?
您沒有在任何地方設置this.year 。
在函數isLeap()中 ,this.year 未定義,因此通過了falsy的規范。
您可以在規范內使用console.log()並隨時查看其值。 它將被打印到終端以及瀏覽器實例。 您也可以嘗試dump() 。
嘗試這個:
var Year = function (year) {
this.year = year;
};
您還可以使用以下方法確認this.year持有的數據類型
expect(this.year).toEqual(jasmine.any(Number));
我設法通過使用Jasmine的CLI調用的jasmine.js
文件運行node --inspect-brk
來啟動調試器:
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瀏覽器中導航至chrome://inspect
並this.year
“繼續”后,我能夠確定this.year
確實undefined
:
然后,我通過定義this.year
修復測試:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.