[英]mocha unit test - how to clear cached javascript after each test
我正在嘗試使用 mocha 和 jsdom 測試一個 javascript 文件(我們稱之為 hello.js)。 我有幾個單元測試; 每個測試基本上都會設置一些窗口屬性,調用 hello.js,並檢查窗口屬性值。 測試似乎以某種方式運行; 但是,似乎 mocha 在第一次測試后使用了“緩存”的 hello.js(日志記錄僅在第一次測試中顯示)。
有人能告訴我如何確保在每次測試中重新加載 hello.js 嗎?
const expect = require('chai').expect;
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
var dom = (new JSDOM(`<!DOCTYPE html><html><head></head><body></body></html>`));
describe('hello', ()=>{
afterEach(()=>{
dom = (new JSDOM(`<!DOCTYPE html><html><head></head><body></body></html>`));
global.window = {};
document = {};
location = {};
});
it('should test A', ()=> {
global.window = dom.window;
global.window.document.cookie = "test=kiwi";
document = global.window.document;
const hello = require('../public/hello.js');
expect(document.getElementsByTagName('IFRAME').length).to.equal(1);
});
it('should test B', ()=> {
global.window = dom.window;
global.window.document.cookie = "test=apple";
document = global.window.document;
const hello = require('../public/hello.js');
expect(document.getElementsByTagName('IFRAME').length).to.equal(0);
});
});
您可以使用一個庫來為您執行此操作:
示例: const resetCache = require('resnap')(); // Capture "clean" cache state
const resetCache = require('resnap')(); // Capture "clean" cache state
參考: Resnap
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.