Unit testing 如何使用jsdom对页面重定向进行单元测试,localhost是';空白';
我有一个功能,它的目的是重定向到pageA,如果由用户触发,否则重定向到pageB 当我对函数进行单元测试时Unit testing 如何使用jsdom对页面重定向进行单元测试,localhost是';空白';,unit-testing,redirect,mocha.js,chai,jsdom,Unit Testing,Redirect,Mocha.js,Chai,Jsdom,我有一个功能,它的目的是重定向到pageA,如果由用户触发,否则重定向到pageB 当我对函数进行单元测试时 const expected = `${host}/pageA`; redirect(true); // calling the redirect function const actual = window.location; expect(actual).to.equal(expected); 失败,因为“blank
const expected = `${host}/pageA`;
redirect(true); // calling the redirect function
const actual = window.location;
expect(actual).to.equal(expected);
失败,因为“blank/pageA”不是“”
看起来window.location有一个“空白”主机,为什么会发生这种情况?我该如何解决这个问题?
我正在使用jsdom、摩卡、柴
我的设置如下所示:
let originalWindow = global.window,
host = 'https://localhost:5401';
beforeEach(() => {
global.window = {
location: {
href: "https://localhost:5401/pageA/2020"
}
};
});
afterEach(() => {
global.window = originalWindow;
});
请把这个做成一个盒子。问题可能在您显示的代码之外的其他地方。请将此问题转化为一个问题。问题可能出现在代码之外的其他地方。