Skip to content

Commit fd17503

Browse files
committed
chore(test): migrate from Jest to node:test
1 parent f568ce4 commit fd17503

File tree

71 files changed

+8786
-12991
lines changed

Some content is hidden

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

71 files changed

+8786
-12991
lines changed

Diff for: .github/dependabot.yml

-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ updates:
3939
patterns:
4040
- '@testing-library/*'
4141
- '@types/testing-library*'
42-
- '@types/jest'
43-
- 'jest'
44-
- 'jest-*'
4542
next-js:
4643
patterns:
4744
- 'next'

Diff for: .github/workflows/lint-and-tests.yml

+7-16
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ jobs:
189189
# that changes to dependencies or translations don't break the Unit Tests
190190
# We want to enforce that the actual `turbo@latest` package is used instead of a possible hijack from the user
191191
# the `${{ needs.base.outputs.turbo_args }}` is a string substitution happening from the base job
192-
run: npx --package=turbo@latest -- turbo test:unit ${{ needs.base.outputs.turbo_args }} -- --ci --coverage
192+
run: npx --package=turbo@latest -- turbo test:ci ${{ needs.base.outputs.turbo_args }}
193193

194194
- name: Start Visual Regression Tests (Chromatic)
195195
# This assigns the Environment Deployment for Storybook
@@ -210,19 +210,10 @@ jobs:
210210
exitOnceUploaded: true
211211
onlyChanged: true
212212

213-
- name: Jest Coverage Comment
214-
# We don't need to post the Jest Coverage comment on Crowdin PRs and Dependabot PRs
215-
# as in general they introduce no changes to the Unit Tests and the Codebase
216-
# We reuse the checks from the Chromatic Deploy step as they're the same conditions
217-
if: steps.chromatic-deploy.outcome == 'success'
218-
# This comments the current Jest Coverage Report containing JUnit XML reports
219-
# and a Code Coverage Summary
220-
uses: MishaKav/jest-coverage-comment@d74238813c33e6ea20530ff91b5ea37953d11c91 # v1.0.27
213+
- name: Report code coverage
214+
uses: zgosalvez/github-actions-report-lcov@df68834145a4a567247d6a3ea8565c4c39d1fd17 # v4.1.23
221215
with:
222-
title: 'Unit Test Coverage Report'
223-
multiple-junitxml-files: |
224-
@node-core/ui-components, ./packages/ui-components/junit.xml
225-
@nodejs/website, ./apps/site/junit.xml
226-
multiple-files: |
227-
@node-core/ui-components, ./packages/ui-components/coverage/coverage-summary.json
228-
@nodejs/website, ./apps/site/coverage/coverage-summary.json
216+
coverage-files: '{apps/site,packages/ui-components}/lcov.info'
217+
artifact-name: code-coverage-report
218+
github-token: ${{ secrets.GITHUB_TOKEN }}
219+
update-comment: true

Diff for: .gitignore

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ npm-debug.log
88
.next
99
build
1010

11-
# Jest
12-
coverage
13-
.swc
11+
# Test Runner
1412
junit.xml
13+
lcov.info
1514

1615
# Storybook
1716
storybook-static

Diff for: .prettierignore

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ build
1010
# Next.js Generated Files
1111
public/static/documents
1212

13-
# Jest
14-
coverage
15-
.swc
13+
# Test Runner
1614
junit.xml
15+
lcov.info
1716

1817
# Storybook
1918
storybook-static

Diff for: COLLABORATOR_GUIDE.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ You can add a PACKAGE_MANAGER the same way as adding an INSTALLATION_METHOD (fro
419419
## Unit Tests and Storybooks
420420

421421
Each new feature or bug fix should be accompanied by a unit test (when deemed valuable).
422-
We use [Jest][] as our test runner and [React Testing Library][] for our React unit tests.
422+
We use [`node:test`][] as our test runner and [React Testing Library][] for our React unit tests.
423423

424424
We also use [Storybook][] to document our components.
425425
Components within `packages/ui-components` should have a storybook story that documents the component's usage.
@@ -435,8 +435,6 @@ Unit Tests are fundamental to ensure that code changes do not disrupt the functi
435435
- Unit Tests should ensure that a given change's functionality is working as expected.
436436
- When creating unit tests for React components, we recommend that the tests cover all the possible states of the component.
437437
- We also recommend mocking external dependencies, if unsure about how to mock a particular dependency, raise the question on your Pull Request.
438-
- We recommend using [Jest's Mock Functions](https://jestjs.io/docs/en/mock-functions) for mocking dependencies.
439-
- We recommend using [Jest's Mock Modules](https://jestjs.io/docs/en/manual-mocks) for mocking dependencies unavailable on the Node.js runtime.
440438
- Common Providers and Contexts from the lifecycle of our App, such as [`next-intl`][] should not be mocked but given an empty or fake context whenever possible.
441439
- We recommend reading previous unit tests from the codebase for inspiration and code guidelines.
442440

@@ -644,7 +642,7 @@ A lot of the current structure is due to retro-compatibility, keeping a simple a
644642

645643
If you're unfamiliar or curious about something, we recommend opening a Discussion on this GitHub Repository.
646644

647-
[Jest]: https://jestjs.io/
645+
[`node:test`]: https://nodejs.org/api/test.html
648646
[React Testing Library]: https://testing-library.com/docs/react-testing-library/intro/
649647
[Storybook]: https://storybook.js.org/
650648
[`next-intl`]: https://next-intl-docs.vercel.app

Diff for: CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ This repository contains several scripts and commands for performing numerous ta
160160
- `npx turbo storybook` starts Storybook's local server
161161
- `npx turbo storybook:build` builds Storybook as a static web application for publishing
162162
- `npm run test` runs all tests locally
163-
- `npx turbo test:unit` runs jest (unit-tests) locally
163+
- `npx turbo test:unit` runs unit-tests locally
164164

165165
</details>
166166

Diff for: DEPENDENCY_PINNING.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ The following recommendations are in order when adding a new dependency:
1212
- Some non-code dependencies are required for the bootstrap of the repository. They are either used on basic build scripts or Git Hooks. Examples include `husky`, `lint-staged` and others.
1313
- A dependency should be a `devDependencies` if it is not invoked anywhere within the codebase.
1414
- This applies to runtimes, tooling, utility/dev-only commands, type packages and others
15-
- Sometimes, a dependency should be a `devDependency` even if invoked within the codebase, but only used within a development environment or during a test runner. Examples include `jest`, `storybook` and others.
15+
- Sometimes, a dependency should be a `devDependency` even if invoked within the codebase, but only used within a development environment or during a test runner. Examples include `storybook` and others.
1616
- A dependency should be a `peerDependencies` if it is a runtime dependency of the Website, but the Website itself does not install it. Examples include `react`, `react-dom` and others.
1717

1818
### When pinning dependencies
1919

2020
When adding dependencies, you should consider if that dependency should be saved as an exact dependency (`--save-exact`) or use either a `^` or `~` version range operator. The following guidelines are in order:
2121

22-
- A dependency, in general, should be pinned to its exact dependency if it's either a tooling or a CLI dependency. Examples include `husky`, `prettier`, `jest` and others.
22+
- A dependency, in general, should be pinned to its exact dependency if it's either a tooling or a CLI dependency. Examples include `husky`, `prettier`, and others.
2323
- A dependency should generally use `~` if we're interested in patch updates (such as hot-fixes and bug-fixes) and the package is part of the Development or Testing Environment. (Such as `storybook`, for example)
2424
- A dependency should generally use `^` if they're part of the Website Application itself, such as `react`, `next-intl` etc. This is done because we intentionally want to get these dependencies' latest features and bug-fixes.
2525
- If we're not interested in getting the latest features and bug fixes, we should consider using `~` instead.

Diff for: apps/site/.stylelintignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ build
77
# Public Folder
88
public
99

10-
# Jest
11-
coverage
10+
# Test Runner
11+
junit.xml
12+
lcov.info
1213

1314
# Old Styles
1415
styles/old
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert';
3+
14
import { render, screen } from '@testing-library/react';
25

36
import BlogHeader from '@/components/Blog/BlogHeader';
@@ -6,24 +9,24 @@ describe('BlogHeader', () => {
69
it('should have correct href when category is all', () => {
710
render(<BlogHeader category="all" />);
811
const link = screen.getByRole('link');
9-
expect(link).toHaveAttribute('href', '/feed/blog.xml');
12+
assert.strictEqual(link.getAttribute('href'), '/feed/blog.xml');
1013
});
1114

1215
it('should have correct href when category is release', () => {
1316
render(<BlogHeader category="release" />);
1417
const link = screen.getByRole('link');
15-
expect(link).toHaveAttribute('href', '/feed/releases.xml');
18+
assert.strictEqual(link.getAttribute('href'), '/feed/releases.xml');
1619
});
1720

1821
it('should have correct href when category is vulnerability', () => {
1922
render(<BlogHeader category="vulnerability" />);
2023
const link = screen.getByRole('link');
21-
expect(link).toHaveAttribute('href', '/feed/vulnerability.xml');
24+
assert.strictEqual(link.getAttribute('href'), '/feed/vulnerability.xml');
2225
});
2326

2427
it('should have correct href when category is random', () => {
2528
render(<BlogHeader category="random" />);
2629
const link = screen.getByRole('link');
27-
expect(link).toHaveAttribute('href', '/feed/blog.xml');
30+
assert.strictEqual(link.getAttribute('href'), '/feed/blog.xml');
2831
});
2932
});

Diff for: apps/site/components/Blog/BlogPostCard/__tests__/index.test.mjs renamed to apps/site/components/Blog/BlogPostCard/__tests__/index.test.jsx

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { render, screen } from '@testing-library/react';
2+
import { describe, it } from 'node:test';
3+
import assert from 'node:assert';
4+
import { isVisible } from '../../../../../../tests/utilities.mjs';
25

36
import BlogPostCard from '@/components/Blog/BlogPostCard';
47

@@ -29,40 +32,39 @@ describe('BlogPostCard', () => {
2932
it('Wraps the entire card within an article', () => {
3033
renderBlogPostCard({});
3134

32-
expect(screen.getByRole('article')).toBeVisible();
35+
assert.ok(isVisible(screen.getByRole('article')));
3336
});
3437

3538
it('Renders the title prop correctly', () => {
3639
const { title } = renderBlogPostCard({});
3740

38-
expect(screen.getAllByText(title).length).toBe(2);
41+
assert.strictEqual(screen.getAllByText(title).length, 2);
3942

4043
// title from preview should be ignored as the one from Links
4144
// and blog card/post are what matter
42-
expect(screen.getAllByText(title)[0]).toHaveAttribute(
43-
'aria-hidden',
45+
assert.strictEqual(
46+
screen.getAllByText(title)[0].getAttribute('aria-hidden'),
4447
'true'
4548
);
4649
});
4750

4851
it('Renders the description prop correctly', () => {
4952
const { description } = renderBlogPostCard({});
5053

51-
expect(screen.getByText(description)).toBeVisible();
54+
assert.ok(isVisible(screen.getByText(description)));
5255
});
5356

54-
it.each([
57+
[
5558
{ label: 'layouts.blog.categories.vulnerability', type: 'vulnerability' },
5659
{ label: 'layouts.blog.categories.announcements', type: 'announcements' },
5760
{ label: 'layouts.blog.categories.release', type: 'release' },
58-
])(
59-
'Renders "%label" text when passing it the type "%type"',
60-
({ label, type }) => {
61+
].forEach(({ label, type }) => {
62+
it(`Renders "${label} text when passing it the type "${type}`, () => {
6163
renderBlogPostCard({ type });
6264

63-
expect(screen.getByText(label)).toBeVisible();
64-
}
65-
);
65+
assert.ok(isVisible(screen.getByText(label)));
66+
});
67+
});
6668

6769
it('Renders all passed authors fullName(s), comma-separated', () => {
6870
const authors = ['Jane Doe', 'John Doe'];
@@ -77,7 +79,7 @@ describe('BlogPostCard', () => {
7779
return `${prev}, ${curr}`;
7880
}, '');
7981

80-
expect(screen.getByText(fullNames)).toBeVisible();
82+
assert.ok(isVisible(screen.getByText(fullNames)));
8183
});
8284

8385
it('Renders all passed authors fullName(s), comma-separated', () => {
@@ -93,7 +95,7 @@ describe('BlogPostCard', () => {
9395
return `${prev}, ${curr}`;
9496
}, '');
9597

96-
expect(screen.getByText(fullNames)).toBeVisible();
98+
assert.ok(isVisible(screen.getByText(fullNames)));
9799
});
98100

99101
it('Renders date prop in short format', () => {
@@ -107,7 +109,7 @@ describe('BlogPostCard', () => {
107109
year: 'numeric',
108110
});
109111

110-
expect(screen.getByText(dateTimeFormat.format(date))).toBeVisible();
112+
assert.ok(isVisible(screen.getByText(dateTimeFormat.format(date))));
111113
});
112114
});
113115
});

Diff for: apps/site/hooks/react-client/__tests__/useClientContext.test.mjs renamed to apps/site/hooks/react-client/__tests__/useClientContext.test.jsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { renderHook } from '@testing-library/react';
33
import useClientContext from '@/hooks/react-client/useClientContext';
44
import { MatterContext } from '@/providers/matterProvider';
55

6+
import { describe, it } from 'node:test';
7+
import assert from 'node:assert';
8+
69
describe('useClientContext', () => {
710
it('should return client context values', () => {
811
const mockContextValue = {
@@ -21,6 +24,6 @@ describe('useClientContext', () => {
2124

2225
const { result } = renderHook(() => useClientContext(), { wrapper });
2326

24-
expect(result.current).toEqual(mockContextValue);
27+
assert.partialDeepStrictEqual(result.current, mockContextValue);
2528
});
2629
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { render, fireEvent, screen } from '@testing-library/react';
2+
import { describe, it } from 'node:test';
3+
import assert from 'node:assert';
4+
import { setTimeout } from 'node:timers/promises';
5+
6+
import useCopyToClipboard from '@/hooks/react-client/useCopyToClipboard';
7+
8+
navigator.clipboard = { writeText: () => {} };
9+
10+
await describe('useCopyToClipboard', async () => {
11+
await it('should call clipboard API with `test` once', async t => {
12+
t.mock.method(navigator.clipboard, 'writeText', () => Promise.resolve());
13+
14+
const TestComponent = ({ textToCopy }) => {
15+
const [copied, copyText] = useCopyToClipboard();
16+
17+
return (
18+
<button onClick={() => copyText(textToCopy)} type="button">
19+
{copied ? 'copied' : 'copy'}
20+
</button>
21+
);
22+
};
23+
24+
render(<TestComponent textToCopy="test" />);
25+
26+
const button = screen.getByRole('button');
27+
28+
fireEvent.click(button);
29+
30+
assert.ok((await screen.findByText(/copied/i)).ownerDocument);
31+
32+
// TODO(@avivkeller): Once https://github.com/testing-library/react-testing-library/issues/1393 is resolved, we should move to mocking
33+
await setTimeout(3000);
34+
35+
assert.ok((await screen.findByText(/copy/i)).ownerDocument);
36+
37+
assert(navigator.clipboard.writeText.mock.callCount(), 1);
38+
assert.deepStrictEqual(
39+
navigator.clipboard.writeText.mock.calls[0].arguments,
40+
['test']
41+
);
42+
});
43+
});

Diff for: apps/site/hooks/react-client/__tests__/useCopyToClipboard.test.mjs

-64
This file was deleted.

0 commit comments

Comments
 (0)