Skip to content

Commit 7112bc7

Browse files
committed
test(wtr): use vitest spy instead of custom
1 parent e967569 commit 7112bc7

File tree

3 files changed

+39
-53
lines changed

3 files changed

+39
-53
lines changed

packages/@lwc/integration-not-karma/configs/plugins/serve-integration.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@ const createRollupPlugin = (input, options) => {
3232
enableStaticContentOptimization: !DISABLE_STATIC_CONTENT_OPTIMIZATION,
3333
disableSyntheticShadowSupport: DISABLE_SYNTHETIC_SHADOW_SUPPORT_IN_COMPILER,
3434
apiVersion: API_VERSION,
35-
modules: [
36-
{
37-
// Assume `ctx.path` is a component file, e.g. modules/x/foo/foo.js
38-
dir: path.resolve(input, '../../..'),
39-
},
40-
],
35+
// Assume `ctx.path` is a component file, e.g. modules/x/foo/foo.js
36+
modules: [{ dir: path.resolve(input, '../../..') }],
4137
...options,
4238
});
4339
};
@@ -85,6 +81,7 @@ const transform = async (ctx) => {
8581
plugins: [customLwcRollupPlugin],
8682

8783
external: [
84+
'@vitest/spy',
8885
'lwc',
8986
'wire-service',
9087
// Some helper files export functions that mutate a global state. The setup file calls

packages/@lwc/integration-not-karma/test/api/CustomElementConstructor-getter/index.spec.js

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import AttrChanged from 'x/attrChanged';
99
import ReflectCamel from 'x/reflectCamel';
1010
import WithChildElmsHasSlot from 'x/withChildElmsHasSlot';
1111
import WithChildElmsHasSlotLight from 'x/withChildElmsHasSlotLight';
12-
import { spyConsole } from '../../../helpers/console.js';
12+
import { spyOn } from '@vitest/spy';
1313
import { USE_COMMENTS_FOR_FRAGMENT_BOOKENDS } from '../../../helpers/constants.js';
1414

1515
const vFragBookend = USE_COMMENTS_FOR_FRAGMENT_BOOKENDS ? '<!---->' : '';
@@ -69,19 +69,9 @@ it('should create custom element if it exists before customElements.define', ()
6969
describe('non-empty custom element', () => {
7070
let consoleSpy;
7171
beforeEach(() => {
72-
consoleSpy = spyConsole();
72+
consoleSpy = spyOn(console, 'warn').mockImplementation(() => {});
7373
});
74-
afterEach(() => {
75-
consoleSpy.reset();
76-
});
77-
78-
function expectWarnings(expectedWarnings) {
79-
const observedWarnings = consoleSpy.calls.warn
80-
.flat()
81-
.map((err) => (err instanceof Error ? err.message : err));
82-
83-
expect(observedWarnings).toEqual(expectedWarnings);
84-
}
74+
afterEach(() => consoleSpy.mockRestore());
8575

8676
it('should log error if non-native-shadow custom element has children', () => {
8777
const elm = document.createElement('test-custom-element-preexisting2');
@@ -93,11 +83,11 @@ describe('non-empty custom element', () => {
9383
class extends WithChildElms.CustomElementConstructor {}
9484
);
9585
if (process.env.NODE_ENV !== 'production' && !process.env.NATIVE_SHADOW) {
96-
expectWarnings([
97-
'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.',
98-
]);
86+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(
87+
'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.'
88+
);
9989
} else {
100-
expectWarnings([]);
90+
expect(consoleSpy).not.toHaveBeenCalled();
10191
}
10292

10393
expect(elm.shadowRoot.childNodes.length).toBe(1);
@@ -123,11 +113,11 @@ describe('non-empty custom element', () => {
123113
);
124114

125115
if (process.env.NODE_ENV !== 'production') {
126-
expectWarnings([
127-
'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.',
128-
]);
116+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(
117+
'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.'
118+
);
129119
} else {
130-
expectWarnings([]);
120+
expect(consoleSpy).not.toHaveBeenCalled();
131121
}
132122

133123
expect(elm.innerHTML).toBe(`${vFragBookend}${vFragBookend}`);
@@ -143,11 +133,11 @@ describe('non-empty custom element', () => {
143133
class extends WithChildElmsHasSlot.CustomElementConstructor {}
144134
);
145135
if (process.env.NODE_ENV !== 'production' && !process.env.NATIVE_SHADOW) {
146-
expectWarnings([
147-
'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.',
148-
]);
136+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(
137+
'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.'
138+
);
149139
} else {
150-
expectWarnings([]);
140+
expect(consoleSpy).not.toHaveBeenCalled();
151141
}
152142

153143
expect(elm.shadowRoot.childNodes.length).toBe(1);
@@ -174,11 +164,11 @@ describe('non-empty custom element', () => {
174164
);
175165

176166
if (process.env.NODE_ENV === 'production') {
177-
expectWarnings([]);
167+
expect(consoleSpy).not.toHaveBeenCalled();
178168
} else {
179-
expectWarnings([
180-
'Found an existing shadow root for the custom element "Child". Call `hydrateComponent` instead.',
181-
]);
169+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(
170+
'Found an existing shadow root for the custom element "Child". Call `hydrateComponent` instead.'
171+
);
182172
}
183173
expect(elm.shadowRoot.innerHTML).toBe('<div></div>');
184174
});
@@ -194,11 +184,11 @@ describe('non-empty custom element', () => {
194184
document.body.appendChild(elm);
195185

196186
if (process.env.NODE_ENV !== 'production' && !process.env.NATIVE_SHADOW) {
197-
expectWarnings([
198-
'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.',
199-
]);
187+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(
188+
'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.'
189+
);
200190
} else {
201-
expectWarnings([]);
191+
expect(consoleSpy).not.toHaveBeenCalled();
202192
}
203193

204194
expect(elm.childNodes.length).toBe(1);

packages/@lwc/integration-not-karma/test/template/directive-for-each/index.spec.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import XTest from 'x/test';
33
import XTestStatic from 'x/testStatic';
44
import XTestCustomElement from 'x/testCustomElement';
55
import ArrayNullPrototype from 'x/arrayNullPrototype';
6-
import { spyConsole } from '../../../helpers/console.js';
6+
import { spyOn } from '@vitest/spy';
77

88
function testForEach(type, obj) {
99
it(`should render ${type}`, () => {
@@ -73,14 +73,10 @@ it('should throw an error when the passing a non iterable', () => {
7373

7474
describe('null/undefined values', () => {
7575
let spy;
76-
77-
beforeEach(() => {
78-
spy = spyConsole();
79-
});
80-
81-
afterEach(() => {
82-
spy.reset();
76+
beforeAll(() => {
77+
spy = spyOn(console, 'error').mockImplementation(() => {});
8378
});
79+
afterEach(() => spy.mockRestore());
8480

8581
[undefined, null].forEach((value) => {
8682
it(`should log an error when passing in ${value}`, async () => {
@@ -93,13 +89,16 @@ describe('null/undefined values', () => {
9389
expect(elm.shadowRoot.querySelector('ul').children.length).toBe(0);
9490

9591
if (process.env.NODE_ENV === 'production') {
96-
expect(spy.calls.error.length).toBe(0);
92+
expect(spy).not.toHaveBeenCalled();
9793
} else {
98-
expect(spy.calls.error.length).toBe(1);
99-
const err = spy.calls.error[0][0]; // first arg of first call
100-
expect(err).toBeInstanceOf(Error);
101-
// TODO [#1283]: Improve this error message. The vm should not be exposed and the message is not helpful.
102-
expect(err.message).toMatch(/It must be an array-like object/);
94+
expect(spy).toHaveBeenCalledTimes(1);
95+
expect(spy).toHaveBeenCalledWith(expect.any(Error));
96+
expect(spy).toHaveBeenCalledWith(
97+
expect.objectContaining({
98+
// TODO [#1283]: Improve this error message. The vm should not be exposed and the message is not helpful.
99+
message: expect.stringMatching(/It must be an array-like object/),
100+
})
101+
);
103102
}
104103
});
105104
});

0 commit comments

Comments
 (0)