我正在编写需要定义 window.location.href 的单元测试 第一个单元测试创建如下
describe('myMethod()', () => {
beforeEach(() => {
global.window = Object.create(window);
Object.defineProperty(window, 'location', {
configurable: true,
value: {
href: 'http://localhost:7777/mainPoint?param=val1',
},
});
});
it('should call with val1', () => {
myMethod();
expect(myService.detectURLCall).toHaveBeenCalledWith('http://localhost:7777/mainPoint?param=val1'); // passes
});
describe('mySecondMethod()', () => {
beforeEach(() => {
global.window = Object.create(window);
Object.defineProperty(window, 'location', {
configurable: true,
value: {
href: 'http://localhost:7777/mainPoint?param=val2',
},
});
});
it('should call with val2', () => {
myMethod();
expect(myService.detectURLCall).toHaveBeenCalledWith('http://localhost:7777/mainPoint?param=val2'); // fails, the received call was ending with =val1
})
我知道 jest 单元测试并行运行,我尝试在 myMethod() 测试套件内的 afterEach() 中执行 clearMocks() 但它没有帮助,我的第二个测试套件仍然检测到我们在第一个测试套件中定义的内容..
相关分类