簡體   English   中英

與 js bin 相比,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 返回錯誤嗎?

PS:如果有幫助,這是我在編輯器中編寫的代碼的快照(忽略關於<script>的注釋,因為我打算接下來刪除它): 在此處輸入圖像描述

這不是測試。 在您發布的圖片中,您有:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM