Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit012e9ca

Browse files
committed
fix: clearHistory() can deal with unmatched calls
Also adds some long overdue tests
1 parentdf91c9c commit012e9ca

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

‎packages/fetch-mock/src/CallHistory.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ class CallHistory {
5555
}
5656

5757
clear(){
58-
this.callLogs.forEach(({ route})=>route.reset());
58+
this.callLogs.forEach(({ route})=>{
59+
if(route){
60+
route.reset();
61+
}
62+
});
5963
this.callLogs=[];
6064
}
6165

‎packages/fetch-mock/src/__tests__/CallHistory.test.js‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,38 @@ describe('CallHistory', () => {
1111
constfetchTheseUrls=(...urls)=>
1212
Promise.all(urls.map((url)=>fm.fetchHandler(url)));
1313

14-
describe('helper methods',()=>{
14+
describe('clear',()=>{
15+
it('should clear all call logs',async()=>{
16+
fm.route('http://a.com',200).route('http://b.com',200);
17+
fm.fetchHandler('http://a.com');
18+
fm.fetchHandler('http://b.com');
19+
20+
expect(fm.callHistory.callLogs.length).toEqual(2);
21+
fm.callHistory.clear();
22+
expect(fm.callHistory.callLogs.length).toEqual(0);
23+
});
24+
it('should clear counter in routes that are set to only respond a set number of times',async()=>{
25+
fm.once('http://a.com',200);
26+
fm.fetchHandler('http://a.com');
27+
fm.callHistory.clear();
28+
awaitexpect(fm.fetchHandler('http://a.com')).resolves.not.toThrow();
29+
});
30+
it('should not error if there are unmatched calls',async()=>{
31+
try{
32+
awaitfm.fetchHandler('https://example.com');
33+
}catch{
34+
expect(()=>fm.callHistory.clear()).not.toThrow();// Error
35+
}
36+
});
37+
38+
it('is aliased to fetchMock.clearHistory()',async()=>{
39+
vi.spyOn(fm.callHistory,'clear');
40+
fm.clearHistory();
41+
expect(fm.callHistory.clear).toHaveBeenCalled();
42+
fm.callHistory.clear.mockRestore();
43+
});
44+
});
45+
describe('inspecting call history methods',()=>{
1546
describe('called()',()=>{
1647
it('returns a suitable boolean',()=>{
1748
fm.catch();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp