Skip to content

Commit 1c3e183

Browse files
authored
test(wtr): fix all broken tests! @W-19098252 (#5516)
* test(wtr): use async/await instead of promise chains * Apply suggestion from @wjhsf * test(wtr): fix test assertion an error object is logged, not just a plain string * test(wtr): remove unnecessary custom matcher OOTB matcher with nearly identical name exists * test(wtr): implement missing jasmine matcher
1 parent 3d119dd commit 1c3e183

File tree

9 files changed

+35
-29
lines changed

9 files changed

+35
-29
lines changed

packages/@lwc/integration-not-karma/configs/base.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default {
2929
// time out before they receive focus. But it also makes the full suite take 3x longer to run...
3030
// Potential workaround: https://github.com/modernweb-dev/web/issues/2588
3131
concurrency: 1,
32+
browserLogs: false,
3233
nodeResolve: true,
3334
rootDir: join(import.meta.dirname, '..'),
3435
plugins: [

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,7 @@ import testPlugin from './plugins/serve-integration.js';
55
/** @type {import("@web/test-runner").TestRunnerConfig} */
66
export default {
77
...baseConfig,
8-
files: [
9-
// FIXME: These tests are just symlinks to integration-karma for now so the git diff smaller
10-
'test/**/*.spec.js',
11-
12-
// Logging mismatches
13-
'!test/component/LightningElement.addEventListener/index.spec.js',
14-
15-
// Implement objectContaining / arrayWithExactContents
16-
'!test/profiler/mutation-logging/index.spec.js',
17-
],
8+
files: ['test/**/*.spec.js'],
189
plugins: [
1910
...baseConfig.plugins,
2011
importMapsPlugin({ inject: { importMap: { imports: { lwc: './mocks/lwc.js' } } } }),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const transform = async (ctx) => {
8181
plugins: [customLwcRollupPlugin],
8282

8383
external: [
84+
'@vitest/expect',
8485
'@vitest/spy',
8586
'lwc',
8687
'wire-service',

packages/@lwc/integration-not-karma/helpers/matchers/jasmine.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ export const registerJasmineMatchers = (chai, utils) => {
1919
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
2020
chai.expect(value).to.be.true;
2121
},
22-
toHaveBeenCalledOnceWith(...args) {
23-
const spy = utils.flag(this, 'object');
24-
chai.expect(spy.calls).to.have.length(1);
25-
chai.expect(spy.calls[0]).to.deep.equal(args);
26-
},
2722
};
2823

2924
for (const [name, impl] of Object.entries(matchers)) {

packages/@lwc/integration-not-karma/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@lwc/synthetic-shadow": "8.22.3",
1717
"@types/chai": "^5.2.2",
1818
"@types/jasmine": "^5.1.9",
19+
"@vitest/expect": "^3.2.4",
1920
"@vitest/spy": "^3.2.4",
2021
"@web/dev-server-import-maps": "^0.2.1",
2122
"@web/dev-server-rollup": "^0.6.4",

packages/@lwc/integration-not-karma/test/component/LightningElement.addEventListener/index.spec.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ describe('event handler is not a function', () => {
5555
if (process.env.NODE_ENV === 'production') {
5656
expect(consoleSpy).not.toHaveBeenCalled();
5757
} else {
58+
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(expect.any(Error));
5859
expect(consoleSpy).toHaveBeenCalledExactlyOnceWith(
59-
expect.stringContaining('Invalid second argument for this.addEventListener()')
60+
expect.objectContaining({
61+
message: expect.stringContaining(
62+
'Invalid second argument for this.addEventListener()'
63+
),
64+
})
6065
);
6166
}
6267
});

packages/@lwc/integration-not-karma/test/profiler/mutation-logging/index.spec.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,40 @@ import { createElement } from 'lwc';
22
import Parent from 'x/parent';
33
import Child from 'x/child';
44
import GetterThrows from 'x/getterThrows';
5-
import { jasmine, jasmineSpyOn as spyOn } from '../../../helpers/jasmine.js';
5+
import { ArrayContaining } from '@vitest/expect';
6+
import { spyOn } from '@vitest/spy';
7+
8+
class ArrayWithExactContents extends ArrayContaining {
9+
asymmetricMatch(other) {
10+
return (
11+
super.asymmetricMatch(other) &&
12+
(this.inverse
13+
? this.sample.length !== other.length
14+
: this.sample.length === other.length)
15+
);
16+
}
17+
}
618

7-
const arr = jasmine.arrayWithExactContents;
8-
// `jasmine.objectContaining` is long, but the method can't be detached/aliased, ergo wrapper
9-
const obj = (val) => jasmine.objectContaining(val);
19+
const arr = (val) => new ArrayWithExactContents(val);
20+
const obj = (val) => expect.objectContaining(val);
1021

1122
let entries = [];
1223

13-
beforeEach(() => {
14-
const perfMeasure = performance.measure.bind(performance);
15-
16-
// We could use PerformanceObserver for this, but it's awkward and unreliable for unit testing
17-
// See: https://github.com/salesforce/lwc/issues/4600
18-
spyOn(performance, 'measure').and.callFake((...args) => {
19-
const entry = perfMeasure(...args);
24+
beforeAll(() => {
25+
const realMeasure = performance.measure.bind(performance);
26+
spyOn(performance, 'measure').mockImplementation((...args) => {
27+
const entry = realMeasure(...args);
2028
if (['lwc-render', 'lwc-rerender'].includes(entry.name)) {
2129
entries.push(entry);
2230
}
2331
return entry;
2432
});
2533
});
2634

35+
afterAll(() => {
36+
performance.measure.mockRestore();
37+
});
38+
2739
afterEach(() => {
2840
entries = [];
2941
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ it('renders iframes correctly - W-17015807', async () => {
1414

1515
await Promise.resolve();
1616

17-
expect(spy).toHaveBeenCalledOnceWith('src', 'about:blank');
17+
expect(spy).toHaveBeenCalledExactlyOnceWith('src', 'about:blank');
1818
});

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3569,7 +3569,7 @@
35693569
"@typescript-eslint/scope-manager" "^8.41.0"
35703570
"@typescript-eslint/utils" "^8.24.1"
35713571

3572-
"@vitest/expect@3.2.4":
3572+
"@vitest/expect@3.2.4", "@vitest/expect@^3.2.4":
35733573
version "3.2.4"
35743574
resolved "https://registry.yarnpkg.com/@vitest/expect/-/expect-3.2.4.tgz#8362124cd811a5ee11c5768207b9df53d34f2433"
35753575
integrity sha512-Io0yyORnB6sikFlt8QW5K7slY4OjqNX9jmJQ02QDda8lyM6B5oNgVWoSoKPac8/kgnCUzuHQKrSLtu/uOqqrig==

0 commit comments

Comments
 (0)