Skip to content

Commit 5eaabcf

Browse files
robintowntoger5
andauthored
Clean up our tests in preparation for the testing sprint (#2466)
* Fix coverage reporting Codecov hasn't been working recently because Vitest doesn't report coverage by default. * Suppress some noisy log lines Closes #686 * Store test files alongside source files This way we benefit from not having to maintain the same directory structure twice, and our linters etc. will actually lint test files by default. * Stop using Vitest globals Vitest provides globals primarily to make the transition from Jest more smooth. But importing its functions explicitly is considered a better pattern, and we have so few tests right now that it's trivial to migrate them all. * Remove Storybook directory We no longer use Storybook. * Configure Codecov Add a coverage gate for all new changes and disable its comments. * upgrade vitest --------- Co-authored-by: Timo <[email protected]>
1 parent 3a75447 commit 5eaabcf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+470
-180
lines changed

.github/workflows/test.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ jobs:
1818
- name: Install dependencies
1919
run: "yarn install"
2020
- name: Vitest
21-
run: "yarn run test"
21+
run: "yarn run test:coverage"
2222
- name: Upload to codecov
2323
uses: codecov/codecov-action@e28ff129e5465c2c0dcc6f003fc735cb6ae0c673 # v4
2424
with:
2525
flags: unittests
26+
fail_ci_if_error: true

.storybook/main.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

.storybook/preview.jsx

Lines changed: 0 additions & 24 deletions
This file was deleted.

codecov.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Don't post comments on PRs; they're noisy and the same information can be
2+
# gotten through the checks section at the bottom of the PR anyways
3+
comment: false
4+
coverage:
5+
status:
6+
project:
7+
default:
8+
# Track the impact of changes on overall coverage without blocking PRs
9+
informational: true
10+
patch:
11+
default:
12+
# Expect 80% coverage on all lines that a PR touches
13+
target: 80%

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"i18n": "node_modules/i18next-parser/bin/cli.js",
1616
"i18n:check": "node_modules/i18next-parser/bin/cli.js --fail-on-warnings --fail-on-update",
1717
"test": "vitest",
18-
"test:coverage": "vitest run --coverage",
18+
"test:coverage": "vitest --coverage",
1919
"backend": "docker-compose -f backend-docker-compose.yml up"
2020
},
2121
"dependencies": {
@@ -95,7 +95,7 @@
9595
"@types/content-type": "^1.1.5",
9696
"@types/dom-screen-wake-lock": "^1.0.1",
9797
"@types/dompurify": "^3.0.2",
98-
"@types/grecaptcha": "^3.0.4",
98+
"@types/grecaptcha": "^3.0.9",
9999
"@types/node": "^20.0.0",
100100
"@types/react-dom": "^18.3.0",
101101
"@types/react-router-dom": "^5.3.3",
@@ -104,6 +104,7 @@
104104
"@types/uuid": "10",
105105
"@typescript-eslint/eslint-plugin": "^7.0.0",
106106
"@typescript-eslint/parser": "^7.0.0",
107+
"@vitest/coverage-v8": "^2.0.5",
107108
"babel-loader": "^9.0.0",
108109
"babel-plugin-transform-vite-meta-env": "^1.0.3",
109110
"eslint": "^8.14.0",
@@ -116,6 +117,7 @@
116117
"eslint-plugin-react": "^7.29.4",
117118
"eslint-plugin-react-hooks": "^4.5.0",
118119
"eslint-plugin-unicorn": "^55.0.0",
120+
"global-jsdom": "^24.0.0",
119121
"i18next-parser": "^9.0.0",
120122
"jsdom": "^25.0.0",
121123
"prettier": "^3.0.0",

src/Avatar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
import { useMemo, FC } from "react";
1818
import { Avatar as CompoundAvatar } from "@vector-im/compound-web";
1919

20-
import { getAvatarUrl } from "./matrix-utils";
20+
import { getAvatarUrl } from "./utils/matrix";
2121
import { useClient } from "./ClientContext";
2222

2323
export enum Size {

src/ClientContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {
3939
CryptoStoreIntegrityError,
4040
fallbackICEServerAllowed,
4141
initClient,
42-
} from "./matrix-utils";
42+
} from "./utils/matrix";
4343
import { widget } from "./widget";
4444
import {
4545
PosthogAnalytics,

src/Toast.test.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2023 New Vector Ltd
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { describe, expect, test, vi } from "vitest";
18+
import { render, configure } from "@testing-library/react";
19+
20+
import { Toast } from "../src/Toast";
21+
import { withFakeTimers } from "./utils/test";
22+
23+
configure({
24+
defaultHidden: true,
25+
});
26+
27+
describe("Toast", () => {
28+
test("renders", () => {
29+
const { queryByRole } = render(
30+
<Toast open={false} onDismiss={() => {}}>
31+
Hello world!
32+
</Toast>,
33+
);
34+
expect(queryByRole("dialog")).toBe(null);
35+
const { getByRole } = render(
36+
<Toast open={true} onDismiss={() => {}}>
37+
Hello world!
38+
</Toast>,
39+
);
40+
expect(getByRole("dialog")).toMatchSnapshot();
41+
});
42+
43+
test("dismisses itself after the specified timeout", () => {
44+
withFakeTimers(() => {
45+
const onDismiss = vi.fn();
46+
render(
47+
<Toast open={true} onDismiss={onDismiss} autoDismiss={2000}>
48+
Hello world!
49+
</Toast>,
50+
);
51+
vi.advanceTimersByTime(2000);
52+
expect(onDismiss).toHaveBeenCalled();
53+
});
54+
});
55+
});

src/Toast.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const Toast: FC<Props> = ({
8686
<DialogOverlay
8787
className={classNames(overlayStyles.bg, overlayStyles.animate)}
8888
/>
89-
<DialogContent asChild>
89+
<DialogContent aria-describedby={undefined} asChild>
9090
<DialogClose
9191
className={classNames(
9292
overlayStyles.overlay,

test/UrlParams-test.ts renamed to src/UrlParams.test.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,16 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { vi } from "vitest";
17+
import { describe, expect, it } from "vitest";
1818

1919
import { getRoomIdentifierFromUrl } from "../src/UrlParams";
20-
import { Config } from "../src/config/Config";
2120

2221
const ROOM_NAME = "roomNameHere";
2322
const ROOM_ID = "!d45f138fsd";
2423
const ORIGIN = "https://call.element.io";
25-
const HOMESERVER = "call.ems.host";
26-
27-
vi.mock("../src/config/Config");
24+
const HOMESERVER = "localhost";
2825

2926
describe("UrlParams", () => {
30-
beforeAll(() => {
31-
vi.mocked(Config.defaultServerName).mockReturnValue("call.ems.host");
32-
});
33-
3427
describe("handles URL with /room/", () => {
3528
it("and nothing else", () => {
3629
expect(

0 commit comments

Comments
 (0)