Skip to content

Commit f729461

Browse files
committed
test(wtr): use vitest spy instead of custom
1 parent 6764ad9 commit f729461

File tree

7 files changed

+153
-96
lines changed

7 files changed

+153
-96
lines changed

packages/@lwc/integration-not-karma/test/component/dynamic-imports/index.spec.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import LwcDynamicSlotted from 'x/lwcDynamicSlotted';
1010
import ContainerFoo from 'x/containerFoo';
1111
import ContainerBar from 'x/containerBar';
1212

13-
import { spyConsole } from '../../../helpers/console.js';
13+
import { spyOn } from '@vitest/spy';
1414
import { registerForLoad, clearRegister } from '../../../helpers/dynamic-loader.js';
1515

1616
beforeEach(() => {
@@ -116,12 +116,10 @@ it('should not cache DOM elements using lwc:dynamic', async () => {
116116

117117
describe('slotted content using lwc:dynamic', () => {
118118
let consoleSpy;
119-
beforeEach(() => {
120-
consoleSpy = spyConsole();
121-
});
122-
afterEach(() => {
123-
consoleSpy.reset();
119+
beforeAll(() => {
120+
consoleSpy = spyOn(console, 'error');
124121
});
122+
afterAll(() => consoleSpy.mockRestore());
125123

126124
it('reallocate slotted content after changing constructor', async () => {
127125
const elm = createElement('x-dynamic-slotted', { is: LwcDynamicSlotted });
@@ -136,7 +134,7 @@ describe('slotted content using lwc:dynamic', () => {
136134
// `slot-bar` is not rendered in synthetic shadow
137135
expect(elm.shadowRoot.querySelector('[data-id="slot-bar"]').assignedSlot).toBe(null);
138136
}
139-
expect(consoleSpy.calls.error.length).toEqual(0);
137+
expect(consoleSpy).not.toHaveBeenCalled();
140138

141139
// Swap construstor and check if nodes have been reallocated.
142140
elm.ctor = ContainerBar;
@@ -150,7 +148,7 @@ describe('slotted content using lwc:dynamic', () => {
150148
// `slot-foo` is not rendered in synthetic shadow
151149
expect(elm.shadowRoot.querySelector('[data-id="slot-foo"]').assignedSlot).toBe(null);
152150
}
153-
expect(consoleSpy.calls.error.length).toEqual(0);
151+
expect(consoleSpy).not.toHaveBeenCalled();
154152
});
155153
});
156154

@@ -242,12 +240,10 @@ it('should not cache DOM elements', async () => {
242240

243241
describe('slotted content', () => {
244242
let consoleSpy;
245-
beforeEach(() => {
246-
consoleSpy = spyConsole();
247-
});
248-
afterEach(() => {
249-
consoleSpy.reset();
243+
beforeAll(() => {
244+
consoleSpy = spyOn(console, 'error');
250245
});
246+
afterAll(() => consoleSpy.mockRestore());
251247

252248
it('reallocate slotted content after changing constructor', async () => {
253249
const elm = createElement('x-dynamic-slotted', { is: DynamicSlotted });
@@ -262,7 +258,7 @@ describe('slotted content', () => {
262258
// `slot-bar` is not rendered in synthetic shadow
263259
expect(elm.shadowRoot.querySelector('[data-id="slot-bar"]').assignedSlot).toBe(null);
264260
}
265-
expect(consoleSpy.calls.error.length).toEqual(0);
261+
expect(consoleSpy).not.toHaveBeenCalled();
266262

267263
// Swap constructor and check if nodes have been reallocated.
268264
elm.ctor = ContainerBar;
@@ -276,6 +272,6 @@ describe('slotted content', () => {
276272
// `slot-foo` is not rendered in synthetic shadow
277273
expect(elm.shadowRoot.querySelector('[data-id="slot-foo"]').assignedSlot).toBe(null);
278274
}
279-
expect(consoleSpy.calls.error.length).toEqual(0);
275+
expect(consoleSpy).not.toHaveBeenCalled();
280276
});
281277
});

packages/@lwc/integration-not-karma/test/lwc-on/index.spec.js

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import RerenderLoop from 'x/rerenderLoop';
1111
import PublicProp from 'x/publicProp';
1212
import ComputedKey from 'x/computedKey';
1313
import ValueEvaluationThrows from 'x/valueEvaluationThrows';
14+
import { spyOn } from '@vitest/spy';
1415
import { jasmine } from '../../helpers/jasmine.js';
15-
import { spyConsole } from '../../helpers/console.js';
1616
import { catchUnhandledRejectionsAndErrors } from '../../helpers/utils.js';
1717

1818
describe('lwc:on', () => {
@@ -257,23 +257,27 @@ describe('lwc:on', () => {
257257
describe('with same object modified', () => {
258258
let consoleSpy;
259259
beforeEach(() => {
260-
consoleSpy = spyConsole();
260+
consoleSpy = spyOn(console, 'error').mockImplementation(() => {});
261261
});
262262
afterEach(() => {
263-
consoleSpy.reset();
263+
consoleSpy.mockRestore();
264264
});
265265

266266
it('throws when a new property is added to object passed to lwc:on', async () => {
267267
element.addMouseoverHandler();
268268
await element.triggerReRender();
269269

270270
if (process.env.NODE_ENV !== 'production') {
271-
expect(consoleSpy.calls.error.length).toEqual(1);
272-
expect(consoleSpy.calls.error[0][0].message).toContain(
273-
"Detected mutation of property 'mouseover' in the object passed to lwc:on for <button>. Reusing the same object with modified properties is prohibited. Please pass a new object instead."
271+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(expect.any(Error));
272+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(
273+
expect.objectContaining({
274+
message: expect.stringContaining(
275+
"Detected mutation of property 'mouseover' in the object passed to lwc:on for <button>. Reusing the same object with modified properties is prohibited. Please pass a new object instead."
276+
),
277+
})
274278
);
275279
} else {
276-
expect(consoleSpy.calls.error.length).toEqual(0);
280+
expect(consoleSpy).not.toHaveBeenCalled();
277281
}
278282
});
279283

@@ -282,12 +286,16 @@ describe('lwc:on', () => {
282286
await element.triggerReRender();
283287

284288
if (process.env.NODE_ENV !== 'production') {
285-
expect(consoleSpy.calls.error.length).toEqual(1);
286-
expect(consoleSpy.calls.error[0][0].message).toContain(
287-
"Detected mutation of property 'click' in the object passed to lwc:on for <button>. Reusing the same object with modified properties is prohibited. Please pass a new object instead."
289+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(expect.any(Error));
290+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(
291+
expect.objectContaining({
292+
message: expect.stringContaining(
293+
"Detected mutation of property 'click' in the object passed to lwc:on for <button>. Reusing the same object with modified properties is prohibited. Please pass a new object instead."
294+
),
295+
})
288296
);
289297
} else {
290-
expect(consoleSpy.calls.error.length).toEqual(0);
298+
expect(consoleSpy).not.toHaveBeenCalled();
291299
}
292300
});
293301

@@ -296,12 +304,16 @@ describe('lwc:on', () => {
296304
await element.triggerReRender();
297305

298306
if (process.env.NODE_ENV !== 'production') {
299-
expect(consoleSpy.calls.error.length).toEqual(1);
300-
expect(consoleSpy.calls.error[0][0].message).toContain(
301-
"Detected mutation of property 'click' in the object passed to lwc:on for <button>. Reusing the same object with modified properties is prohibited. Please pass a new object instead."
307+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(expect.any(Error));
308+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(
309+
expect.objectContaining({
310+
message: expect.stringContaining(
311+
"Detected mutation of property 'click' in the object passed to lwc:on for <button>. Reusing the same object with modified properties is prohibited. Please pass a new object instead."
312+
),
313+
})
302314
);
303315
} else {
304-
expect(consoleSpy.calls.error.length).toEqual(0);
316+
expect(consoleSpy).not.toHaveBeenCalled();
305317
}
306318
});
307319
});
@@ -348,23 +360,27 @@ describe('lwc:on', () => {
348360
describe('with same object modified', () => {
349361
let consoleSpy;
350362
beforeEach(() => {
351-
consoleSpy = spyConsole();
363+
consoleSpy = spyOn(console, 'error').mockImplementation(() => {});
352364
});
353365
afterEach(() => {
354-
consoleSpy.reset();
366+
consoleSpy.mockRestore();
355367
});
356368

357369
it('throws when a new property is added to object passed to lwc:on', async () => {
358370
element.addMouseoverHandler();
359371
await element.triggerReRender();
360372

361373
if (process.env.NODE_ENV !== 'production') {
362-
expect(consoleSpy.calls.error.length).toEqual(1);
363-
expect(consoleSpy.calls.error[0][0].message).toContain(
364-
"Detected mutation of property 'mouseover' in the object passed to lwc:on for <button>. Reusing the same object with modified properties is prohibited. Please pass a new object instead."
374+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(expect.any(Error));
375+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(
376+
expect.objectContaining({
377+
message: expect.stringContaining(
378+
"Detected mutation of property 'mouseover' in the object passed to lwc:on for <button>. Reusing the same object with modified properties is prohibited. Please pass a new object instead."
379+
),
380+
})
365381
);
366382
} else {
367-
expect(consoleSpy.calls.error.length).toEqual(0);
383+
expect(consoleSpy).not.toHaveBeenCalled();
368384
}
369385
});
370386

@@ -373,12 +389,16 @@ describe('lwc:on', () => {
373389
await element.triggerReRender();
374390

375391
if (process.env.NODE_ENV !== 'production') {
376-
expect(consoleSpy.calls.error.length).toEqual(1);
377-
expect(consoleSpy.calls.error[0][0].message).toContain(
378-
"Detected mutation of property 'click' in the object passed to lwc:on for <button>. Reusing the same object with modified properties is prohibited. Please pass a new object instead."
392+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(expect.any(Error));
393+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(
394+
expect.objectContaining({
395+
message: expect.stringContaining(
396+
"Detected mutation of property 'click' in the object passed to lwc:on for <button>. Reusing the same object with modified properties is prohibited. Please pass a new object instead."
397+
),
398+
})
379399
);
380400
} else {
381-
expect(consoleSpy.calls.error.length).toEqual(0);
401+
expect(consoleSpy).not.toHaveBeenCalled();
382402
}
383403
});
384404

@@ -387,12 +407,16 @@ describe('lwc:on', () => {
387407
await element.triggerReRender();
388408

389409
if (process.env.NODE_ENV !== 'production') {
390-
expect(consoleSpy.calls.error.length).toEqual(1);
391-
expect(consoleSpy.calls.error[0][0].message).toContain(
392-
"Detected mutation of property 'click' in the object passed to lwc:on for <button>. Reusing the same object with modified properties is prohibited. Please pass a new object instead."
410+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(expect.any(Error));
411+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(
412+
expect.objectContaining({
413+
message: expect.stringContaining(
414+
"Detected mutation of property 'click' in the object passed to lwc:on for <button>. Reusing the same object with modified properties is prohibited. Please pass a new object instead."
415+
),
416+
})
393417
);
394418
} else {
395-
expect(consoleSpy.calls.error.length).toEqual(0);
419+
expect(consoleSpy).not.toHaveBeenCalled();
396420
}
397421
});
398422
});

packages/@lwc/integration-not-karma/test/regression/invalid-key/index.spec.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { createElement } from 'lwc';
22
import ConditionalList from 'x/conditionalList';
3+
import { spyOn } from '@vitest/spy';
34
import { extractDataIds } from '../../../helpers/utils.js';
4-
import { spyConsole } from '../../../helpers/console.js';
5+
6+
let consoleSpy;
7+
beforeEach(() => {
8+
consoleSpy = spyOn(console, 'error');
9+
});
10+
afterEach(() => {
11+
consoleSpy.mockRestore();
12+
});
513

614
it('W-15885661 - renders list when key is invalid (preserve backwards compat)', async () => {
715
const elm = createElement('x-conditional-list', { is: ConditionalList });
@@ -12,18 +20,21 @@ it('W-15885661 - renders list when key is invalid (preserve backwards compat)',
1220
// Empty fragment
1321
expect(ul.children.length).toBe(0);
1422

15-
const spy = spyConsole();
1623
elm.items = [{ value: 1 }];
1724
await Promise.resolve();
1825

19-
const errorCalls = spy.calls.error;
20-
expect(errorCalls.length).toBe(process.env.NODE_ENV === 'production' ? 0 : 2);
21-
errorCalls.forEach(([error]) => {
22-
expect(error).toBeInstanceOf(Error);
23-
expect(error.message).toMatch(/(Invalid key value.*|Invalid "key" attribute.*)/);
24-
});
25-
26-
spy.reset();
26+
if (process.env.NODE_ENV === 'production') {
27+
expect(consoleSpy).not.toHaveBeenCalled();
28+
} else {
29+
expect(consoleSpy).toHaveBeenCalledTimes(2);
30+
const [firstCall, secondCall] = consoleSpy.mock.calls;
31+
expect(firstCall).toHaveSize(1);
32+
expect(firstCall[0]).toBeInstanceOf(Error);
33+
expect(firstCall[0].message).toContain('Invalid key value');
34+
expect(secondCall).toHaveSize(1);
35+
expect(secondCall[0]).toBeInstanceOf(Error);
36+
expect(secondCall[0].message).toContain('Invalid "key" attribute');
37+
}
2738

2839
// Still renders list with invalid keys
2940
expect(ul.children.length).toBe(1);

0 commit comments

Comments
 (0)