Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add jest + react-testing library with example and docs guide #1246

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/testing/converting-old-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: Converting old tests
category: Testing
order: 4
---

## Converting old tests

This guide explains how to convert current InstUI tests written with the `@instructure/ui-test-utils` testing library to
use Jest + React Testing Library.

### Running Jest

Jest can be run from the project root with the `yarn test:new` command. It's configured in our CI pipeline so pushing a
branch to remote runs these tests together with the legacy tests automatically.

### Adding new tests to a package

Current tests can be found next to the component source code in the `__tests__` subfolder. New tests should be added
there in the `__new-tests__` directory. New test files should have the same names as the ones in `__tests__`.

New tests should try to mirror old tests as close as possible, most of the time this will be trivial.

### Skipping old tests

Once a test is rewritten in the new framework the old counterpart should be skipped to indicate that it's converted and
also to be resourceful with our CI. This can be done by adding `.skip` to individual tests or the whole suite. E.g.:
`it('should...')` -> `it.skip('should...)` or `describe('<Component/>)` -> `describe.skip('<Component/>')`. If a whole
suite is skipped there's no need to skip individual tests inside it.

### Example

The `ui-avatar` tests were added as an example/reference. They can be found [here](https://github.com/instructure/instructure-ui/tree/master/packages/ui-avatar/src/Avatar/__new-tests__).
6 changes: 4 additions & 2 deletions docs/testing/writing-tests.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
---
title: Writing tests
title: Writing tests (legacy)
category: Testing
order: 3
---

> This section uses `@instructure/ui-test-utils` testing library, plsease see the installation and configuration [here](/#ui-test-utils)
> This section uses [`@instructure/ui-test-utils`](/#ui-test-utils) testing library which will be sunset in our next
> release in favor of Jest + React Testing Library. See [our guide](/#converting-old-tests) on how to convert legacy tests to use the new
> frameworks.

## The anatomy of a test

Expand Down
37 changes: 37 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
testMatch: ['**/__new-tests__/**/*.test.tsx'],
coverageThreshold: {
global: {
statements: 87,
branches: 70,
functions: 80,
lines: 87
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should calculate test coverage too (and later fail the build if its below 80%)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the coverage threshold to the config. Once we are ready, we can add the --coverage flag to the script and it should work.

11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
"start": "yarn workspace docs-app start",
"start:watch": "yarn workspace docs-app start:watch ",
"dev": "yarn start:watch",
"test:patchset": "npm-run-all lint:commit lint:changes test:jest test",
"test:patchset": "npm-run-all lint:commit lint:changes test:jest test test:new",
"pretest:merge": "yarn bootstrap && yarn check",
"test:merge": "npm-run-all lint:commit lint test",
"test:all": "npm-run-all test:jest test",
"test:watch": "ui-scripts test --watch --no-headless",
"test:changes": "ui-scripts test --changed --randomize",
"test": "ui-scripts test --randomize",
"test:jest": "lerna run test:jest --stream",
"test:new": "NODE_ENV=test jest",
"lint": "lerna run lint --stream",
"lint:changes": "yarn lint --since HEAD^",
"lint:fix": "lerna run lint:fix --stream",
Expand Down Expand Up @@ -68,6 +69,11 @@
"@babel/cli": "^7.22.9",
"@instructure/ui-scripts": "workspace:*",
"@instructure/ui-token-scripts": "workspace:*",
"@testing-library/dom": "^9.3.1",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.4.3",
"@types/jest": "^29.5.3",
"@types/react-dom": "^18.2.7",
"chalk": "^4.1.2",
"commitizen": "^4.3.0",
Expand All @@ -76,10 +82,13 @@
"esbuild": "^0.18.12",
"eslint": "^8.44.0",
"husky": "^8.0.3",
"jest": "^29.6.2",
"jest-environment-jsdom": "^29.6.2",
"lerna": "^7.1.3",
"lint-staged": "^13.2.3",
"netlify-cli": "13.2.2",
"npm-run-all": "^4.1.5",
"ts-jest": "^29.1.1",
"typescript": "5.1.6",
"webpack": "^5.88.1"
},
Expand Down
5 changes: 4 additions & 1 deletion packages/ui-avatar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@
"prop-types": "^15.8.1"
},
"devDependencies": {
"@instructure/ui-axe-check": "8.39.0",
"@instructure/ui-babel-preset": "8.39.0",
"@instructure/ui-color-utils": "8.39.0",
"@instructure/ui-test-locator": "8.39.0",
"@instructure/ui-test-utils": "8.39.0",
"@instructure/ui-themes": "8.39.0"
"@instructure/ui-themes": "8.39.0",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^14.0.0"
},
"peerDependencies": {
"react": ">=16.8 <=18"
Expand Down
Loading
Loading