Skip to content

Commit 1a7e77d

Browse files
authored
test: migrate debug tests to vitest spies (#4817)
Migrates the debug tests to use `vi.fn`.
1 parent e1b2e6f commit 1a7e77d

9 files changed

+123
-112
lines changed

debug/test/browser/component-stack-2.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createElement, render, Component } from 'preact';
22
import 'preact/debug';
33
import { setupScratch, teardown } from '../../../test/_util/helpers';
4+
import { vi } from 'vitest';
45

56
/** @jsx createElement */
67

@@ -19,13 +20,13 @@ describe('component stack', () => {
1920

2021
errors = [];
2122
warnings = [];
22-
sinon.stub(console, 'error').callsFake(e => errors.push(e));
23-
sinon.stub(console, 'warn').callsFake(w => warnings.push(w));
23+
vi.spyOn(console, 'error').mockImplementation(e => errors.push(e));
24+
vi.spyOn(console, 'warn').mockImplementation(w => warnings.push(w));
2425
});
2526

2627
afterEach(() => {
27-
console.error.restore();
28-
console.warn.restore();
28+
console.error.mockRestore();
29+
console.warn.mockRestore();
2930
teardown(scratch);
3031
});
3132

debug/test/browser/debug-compat.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as PropTypes from 'prop-types';
77
// eslint-disable-next-line no-duplicate-imports
88
import { resetPropWarnings } from 'preact/debug';
99
import { forwardRef, createPortal } from 'preact/compat';
10+
import { vi } from 'vitest';
1011

1112
const h = createElement;
1213
/** @jsx createElement */
@@ -21,17 +22,17 @@ describe('debug compat', () => {
2122
errors = [];
2223
warnings = [];
2324
scratch = setupScratch();
24-
sinon.stub(console, 'error').callsFake(e => errors.push(e));
25-
sinon.stub(console, 'warn').callsFake(w => warnings.push(w));
25+
vi.spyOn(console, 'error').mockImplementation(e => errors.push(e));
26+
vi.spyOn(console, 'warn').mockImplementation(w => warnings.push(w));
2627

2728
root = document.createElement('div');
2829
document.body.appendChild(root);
2930
});
3031

3132
afterEach(() => {
3233
/** @type {*} */
33-
console.error.restore();
34-
console.warn.restore();
34+
console.error.mockRestore();
35+
console.warn.mockRestore();
3536
teardown(scratch);
3637

3738
document.body.removeChild(root);
@@ -73,7 +74,7 @@ describe('debug compat', () => {
7374

7475
render(<Foo ref={ref} text="123" />, scratch);
7576

76-
expect(console.error).not.been.called;
77+
expect(console.error).not.toHaveBeenCalled();
7778

7879
expect(ref.current).to.not.be.undefined;
7980
});

debug/test/browser/debug-hooks.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useState, useEffect } from 'preact/hooks';
33
import 'preact/debug';
44
import { act } from 'preact/test-utils';
55
import { setupScratch, teardown } from '../../../test/_util/helpers';
6+
import { vi } from 'vitest';
67

78
/** @jsx createElement */
89

@@ -15,13 +16,13 @@ describe('debug with hooks', () => {
1516
errors = [];
1617
warnings = [];
1718
scratch = setupScratch();
18-
sinon.stub(console, 'error').callsFake(e => errors.push(e));
19-
sinon.stub(console, 'warn').callsFake(w => warnings.push(w));
19+
vi.spyOn(console, 'error').mockImplementation(e => errors.push(e));
20+
vi.spyOn(console, 'warn').mockImplementation(w => warnings.push(w));
2021
});
2122

2223
afterEach(() => {
23-
console.error.restore();
24-
console.warn.restore();
24+
console.error.mockRestore();
25+
console.warn.mockRestore();
2526
teardown(scratch);
2627
});
2728

debug/test/browser/debug-suspense.test.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
teardown,
77
serializeHtml
88
} from '../../../test/_util/helpers';
9+
import { vi } from 'vitest';
910

1011
/** @jsx createElement */
1112

@@ -21,13 +22,13 @@ describe('debug with suspense', () => {
2122
warnings = [];
2223
scratch = setupScratch();
2324
rerender = setupRerender();
24-
sinon.stub(console, 'error').callsFake(e => errors.push(e));
25-
sinon.stub(console, 'warn').callsFake(w => warnings.push(w));
25+
vi.spyOn(console, 'error').mockImplementation(e => errors.push(e));
26+
vi.spyOn(console, 'warn').mockImplementation(w => warnings.push(w));
2627
});
2728

2829
afterEach(() => {
29-
console.error.restore();
30-
console.warn.restore();
30+
console.error.mockRestore();
31+
console.warn.mockRestore();
3132
teardown(scratch);
3233
});
3334

@@ -74,7 +75,7 @@ describe('debug with suspense', () => {
7475
render(suspense, scratch);
7576
rerender(); // render fallback
7677

77-
expect(console.error).to.not.be.called;
78+
expect(console.error).not.toHaveBeenCalled();
7879
expect(serializeHtml(scratch)).to.equal('<div>fallback...</div>');
7980

8081
return loader.then(() => {
@@ -106,7 +107,7 @@ describe('debug with suspense', () => {
106107

107108
return loader.then(() => {
108109
rerender();
109-
expect(console.warn).to.be.calledTwice;
110+
expect(console.warn).toHaveBeenCalledTimes(2);
110111
expect(warnings[1].includes('MyLazyLoaded')).to.equal(true);
111112
expect(serializeHtml(scratch)).to.equal('<div>Hi there</div>');
112113
});
@@ -132,7 +133,7 @@ describe('debug with suspense', () => {
132133

133134
return loader.then(() => {
134135
rerender();
135-
expect(console.warn).to.be.calledTwice;
136+
expect(console.warn).toHaveBeenCalledTimes(2);
136137
expect(warnings[1].includes('HelloLazy')).to.equal(true);
137138
expect(serializeHtml(scratch)).to.equal('<div>Hi there</div>');
138139
});
@@ -160,7 +161,7 @@ describe('debug with suspense', () => {
160161
}
161162

162163
// Called once on initial render, and again when promise rejects
163-
expect(console.warn).to.be.calledTwice;
164+
expect(console.warn).toHaveBeenCalledTimes(2);
164165
});
165166
});
166167

@@ -181,7 +182,7 @@ describe('debug with suspense', () => {
181182
error = e;
182183
}
183184

184-
expect(console.warn).to.be.calledOnce;
185+
expect(console.warn).toHaveBeenCalledOnce();
185186
expect(error).not.to.be.undefined;
186187
expect(error.message).to.eql('Hello');
187188
});

debug/test/browser/debug.options.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { useState } from 'preact/hooks';
1212
import { setupRerender } from 'preact/test-utils';
1313
import 'preact/debug';
1414
import { setupScratch, teardown } from '../../../test/_util/helpers';
15+
import { vi } from 'vitest';
1516

1617
/** @jsx createElement */
1718

@@ -25,7 +26,7 @@ describe('debug options', () => {
2526
/** @type {(count: number) => void} */
2627
let setCount;
2728

28-
/** @type {import('sinon').SinonFakeTimers | undefined} */
29+
/** @type {import('vitest').VitestUtils | undefined} */
2930
let clock;
3031

3132
beforeEach(() => {
@@ -42,7 +43,7 @@ describe('debug options', () => {
4243

4344
afterEach(() => {
4445
teardown(scratch);
45-
if (clock) clock.restore();
46+
if (clock) vi.useRealTimers();
4647
});
4748

4849
class ClassApp extends Component {
@@ -123,7 +124,7 @@ describe('debug options', () => {
123124
}
124125
}
125126

126-
clock = sinon.useFakeTimers();
127+
clock = vi.useFakeTimers();
127128

128129
render(<ErrorApp />, scratch);
129130
rerender();
@@ -132,6 +133,6 @@ describe('debug options', () => {
132133

133134
// we expect to throw after setTimeout to trigger a window.onerror
134135
// this is to ensure react compat (i.e. with next.js' dev overlay)
135-
expect(() => clock.tick(0)).to.throw(e);
136+
expect(() => clock.advanceTimersByTime(0)).to.throw(e);
136137
});
137138
});

0 commit comments

Comments
 (0)