javascript - mocha 单元测试 - 如何在每次测试后清除缓存的 javascript
问题描述
我正在尝试使用 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);
});
});
解决方案
推荐阅读
- c# - MSIX Web 安装程序不工作 - 解析应用程序包时出错
- python - 基于其他两列创建一列
- android - 如何将自定义 SharedPreferences 名称与 AndroidX 首选项库一起使用?
- google-maps-api-3 - 大屏幕上的 Google Maps API 故障
- python - 如何将列添加到数据框使其成为第一列?
- python - 浏览文件并将路径存储在函数中
- python - 我可以在虚拟环境中增加 Python 使用的内存量吗
- jquery-select2 - 带有 select2 的浮动标签
- mvvm - 未调用 Kendo Observable 远程数据源更新
- java - 用公共构造函数Java声明一个私有静态嵌套类?