簡體   English   中英

在瀏覽器中即時定義摩卡規格

[英]Defining mocha specs on the fly in the browser

我需要能夠在瀏覽器中執行摩卡測試。 不使用cli工具通過終端在瀏覽器中運行測試。 從字面上看,瀏覽器中的所有內容。 目的是在用戶單擊按鈕后運行規格。

我已經在瀏覽器中正確設置了摩卡,但是有些緩存行為使我感到困惑。

https://plnkr.co/edit/nuZZyD5EsMtpgP66HtW5?p=preview

HTML:

<div id="mocha"></div>
<button id="test">run test</button>

JS:

(function(init, $, runner) {
    init();
    $('#test').addEventListener('click', runner)
})(
    function init() {
        mocha.setup('bdd');
    },
    function $(sel) {
        return document.querySelector(sel);
    },
    function specRunner(e) {
        describe('test at ' + new Date().getTime(), function() {
            it('should add 1 + 1', function() {
                chai.assert(1 + 1 === 2, 'added 1 + 1');
            });
        });
        mocha.run();
    }
)

我使用過的其他測試工具(qunit,茉莉花,業力,量角器)都鼓勵(強迫?)用戶在啟動轉輪之前定義所有規格。 這與我要實現的目標相反:以編程方式而不是在運行時定義規范。

在plnkr中,mocha似乎在緩存以前的規范,然后在每次點擊時運行所有規范。 我該如何避免這種行為?

我認為這令人擔憂,如您所料:

(function(init, $, runner) {
    init();
    $('#test').addEventListener('click', runner)
})(
    function init() {
        mocha.setup('bdd');
        describe('test at ' + new Date().getTime(), function() {
            it('should add 1 + 1', function() {
                chai.assert(1 + 1 === 2, 'added 1 + 1');
            });
        });
    },
    function $(sel) {
        return document.querySelector(sel);
    },
    function specRunner(e) {
        mocha.run();
    }
)

問題是您每次單擊按鈕時都定義測試,但是只需要定義一次,並且只能在運行程序中運行。

希望這對你有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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