![](/img/trans.png)
[英]Run different modules in a test_file.js in javascript in mocha
[英]Mocha test returning different answer when compared to js bin
我正在編寫 function 以嘗試從 html 字符串中刪除<noscript>
標記。
我在下面寫了我的 function:
function removeNoScript(str){ var start = str.search("<noscript>"); var end = str.search("</noscript>") + "</noscript>".length; var result = str.replace(str.substring(start,end),""); return result; } let result = removeNoScript("<p>first word</p><noscript>This shows up</noscript><p>second word</p>"); console.log(result)
這工作得很好,但是當我使用 chai 和 mocha 對此進行單元測試時(如下):
it("removes <noscript> in-between markup", () => {
removeScripts(
"<p>first word</p><noscript>This shows up</noscript><p>second word</p>"
).should.equal("<p>first word</p><p>second word</p>");
});
同樣的 function 在 JSBin 中工作我注銷了響應 - 知道為什么它在 JSBin 上工作但 Mocha/Chai 返回錯誤嗎?
這不是測試。 在您發布的圖片中,您有:
result = str.replace(str.substr(start, end),"");
使用substr
而不是substring
。 substr
的第二個參數表示要提取的字符數,而substring
的第二個參數是要排除的第一個字符的索引。
子字符串: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
substring: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
由於substr
是遺留的,您可能希望堅持使用substring
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.