-
-
Notifications
You must be signed in to change notification settings - Fork 185
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
[TASK-1046] Introduce unit tests for UI components #5120
Conversation
@@ -159,6 +168,7 @@ | |||
"swc-loader": "^0.2.6", | |||
"terser-webpack-plugin": "^5.3.10", | |||
"ts-loader": "^9.5.1", | |||
"ts-node": "^10.9.2", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📓 So ts-node lets us:
- write jest.config.ts in TypeScript instead of JavaScript
- launch it from
npx jest --config ./jsapp/jest/jest.config.ts
without an extra step
It's not strictly necessary but it's in the spirit of leveraging TypeScript. In the future we could apply this to Webpack. I looked into it:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add "ts-node": { "swc": true }
to tsconfig.json
.
Since ts-node has to transpile the config, it does affect test startup time a little bit.
time npm run jest |
||
---|---|---|
As-is | jest.config.ts |
2021 ms ± 23 ms |
With swc: true |
jest.config.ts |
1156 ms ± 79 ms |
Plain JS config | jest.config.mjs |
989 ms ± 40 ms |
benchmark
hyperfine --warmup 3 'npm run jest'
TypeScript, ts-node
Time (mean ± σ): 2.021 s ± 0.023 s [User: 3.671 s, System: 0.255 s]
Range (min … max): 1.987 s … 2.066 s 10 runs
TypeScript, ts-node, `"ts-node": { "swc": true },`
Time (mean ± σ): 1.156 s ± 0.079 s [User: 1.454 s, System: 0.168 s]
Range (min … max): 1.091 s … 1.285 s 10 runs
Plain JS (jest.config.mjs)
Time (mean ± σ): 989.7 ms ± 39.7 ms [User: 1254.6 ms, System: 129.9 ms]
Range (min … max): 959.0 ms … 1092.9 ms 10 runs
@@ -201,7 +211,9 @@ | |||
"copy-fonts": "python3 ./scripts/copy_fonts.py && npm run generate-icons", | |||
"hint": "node ./scripts/hints.js", | |||
"storybook": "storybook dev -p 6006 --no-open", | |||
"build-storybook": "storybook build" | |||
"build-storybook": "storybook build", | |||
"jest": "jest --config ./jsapp/jest/jest.config.ts", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could add this to our GitHub CI (.github/workflows/npm-test.yml)
# Run Jest component tests
- name: Run Jest component tests
- run: npx jest --config ./jsapp/jest/jest.config.ts --ci
Here, or in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, I think that's in the scope of adding jest to project, because normally all tests are always run on CI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about doing this in a different PR, after some more tests were added. But I believe there's no problem doing so here anyways. 👍🏻
{value: '3', label: 'Avocado'}, | ||
]; | ||
|
||
// A wrapper is needed for the component to retain value changes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📓 Good point — many of our Kobo components are like this — 'controlled'-only. It'd be possible to make them more flexible.
'Course, then there would be more to test 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a common practice I found for single component testing, since most of the components will depend on external context anyways. So I really don't think it's the case of making them 'uncontrolled' just for the sake of tests. We only need to be careful to not create biased tests that could influence the component behavior. (but yeah, cool article! 😄)
Cool. Checks added the new tests already! |
Checklist
Description
Create the base configuration and samples for adding unit tests with Jest.
Create simple tests for the
Button
andKoboSelect3
components as samples.Notes
jest
to work/jsapp/jest
package.json
:npm run jest
npm run jest-watch
(Very useful when writing tests)npm run jest-watch -- Button
.tests.*
file, we're using the.spec.*
extension for tests.Button
component was added checking for its interaction when enabled and disabledKoboSelect3
component was added checking for its mouse and keyboard interaction