Skip to content

Commit 251ea70

Browse files
juhanakristiannobraynermerodirotigerabrodiJacobMGEvans
authored
feat: Convert to TypeScript (#520)
* Added tsconfig.json. Modified eslint config * Convert src/cleanup.js to TypeScript * Change src/cleanup.ts to use inline types * revert eslintrc * disable eslint for cleanup import this can be reverted when `pure.js` is converted to typescript * convert index to typescript just a rename 😅 * BREAKING CHANGE: Worked on typing asyncUtils. Removed deprecated wait. Disabled import/no-unresolved for now * Fix incorrect cleanup callback type * asyncUtils has been updated. Two lines are disabled for eslint. * remove unnecessary eslint disable * Prettier was stripping try catch types - see: https://prettier.io/blog/2020/08/24/2.1.0.html\#type-annotations-on-catch-clauses-8805httpsgithubcomprettierprettierpull8805-by-fiskerhttpsgithubcomfisker - Updated prettier in local devDeps to resolve directly to newest supported version * Type for callback handle generic input & undefined handled by nullish operator TS expecting explicit return of undefined from arrow function with type Expected to return a value at the end of arrow function.eslintconsistent-return resolved by passing return in catch with undefined, same behavior more explicit * comments for review * VoidFunction in place for void until decided behavior for waitFor() * Utilizing OR operator with generic Types allows for desired behavior and previous tests pass * mistakenly left out linter ignores in last commit * Minimum types started for Pure file - File needs better, improved typing and Refactoring for Linter Disables to be removed IF POSSIBLE * removed comments -- answered in PR - Types and OR check should handle expected behavior * Generic HTML types for initialProps * Generic HTML types for initialProps * Generic type for callback and initialProps * Generics added and CB toplevel same generic pattern added * force rebuild with unbound method eslint ignore * parser issue -- attempt to fix by removing specific TS eslint rule * Newer versions of ESLint might resolve the parser issue * Added configs to get started on test conversion * Generic type for resultContainer this should allow typescript to infer the type of result.current further improvments needed to add type guards * convert tests that require no changes renamed tests that didn't require code changes to the tests * add types to cleanup test * Test TS Overhaul - Amr, Tiger and myself worked on these commits - Other Raid members in chat assisted - All tests are strongly typed with minimal types to allow for working and made sure tests types were easily usable with types in Pure and Utils file, allow for good UX & DX * Amr updated types useEffect * Jens suggestion for more generic number key type * Remove wait reference from docs * Add nobrayner to contributors * Add JacobMGEvans to contributors * Update src/pure.tsx * Add tigerabrodi to contributors * Add Amr, Juhana, and Jens to contributors * update suspenseHook, cache type and the type of the error in catch. * cleanup.ts, update the way addCleanup adds another callback to cleanupCallbacks. * Made generics more descriptive, made TestHook generic * Remove some eslint disables that didn't do anything * Remove DefinitelyTyped reference in CONTRIBUTING.md * chore: disable declaration for tests * Removes createTimeoutError. Adds constructor to TimeoutError. Adds typeing to waitForValueToChange options * Remove conditional in TestHook for hookProps * Replace old types defs with referenced types in dependencies * Remove destructuring of `testRenderer` * Disabled floating promise lint rule globally * Refactor TestHook catch to not disable lint rules * Disabled eslint error for while(true) * Cleaned up some line warnings from tests * Added "typecheck" kcd-script to improve "validate" script * Clean up ThrowError type in errorHook tests * Replace VoidFunction with () => void * Replace CallableFunction with a more explicit function type Co-authored-by: Braydon Hall <[email protected]> Co-authored-by: Amr Gad <[email protected]> Co-authored-by: tigerabrodi <[email protected]> Co-authored-by: Jacob Evans <[email protected]> Co-authored-by: Jacob M-G Evans <[email protected]> Co-authored-by: marcosvega91 <[email protected]> Co-authored-by: Michael Peyper <[email protected]>
1 parent c53b56b commit 251ea70

30 files changed

+373
-383
lines changed

Diff for: .all-contributorsrc

+58
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,64 @@
212212
"contributions": [
213213
"code"
214214
]
215+
},
216+
{
217+
"login": "nobrayner",
218+
"name": "Braydon Hall",
219+
"avatar_url": "https://avatars2.githubusercontent.com/u/40751395?v=4",
220+
"profile": "https://github.com/nobrayner",
221+
"contributions": [
222+
"code"
223+
]
224+
},
225+
{
226+
"login": "JacobMGEvans",
227+
"name": "Jacob M-G Evans",
228+
"avatar_url": "https://avatars1.githubusercontent.com/u/27247160?v=4",
229+
"profile": "https://dev.to/jacobmgevans",
230+
"contributions": [
231+
"code",
232+
"test"
233+
]
234+
},
235+
{
236+
"login": "tigerabrodi",
237+
"name": "Tiger Abrodi",
238+
"avatar_url": "https://avatars1.githubusercontent.com/u/49603590?v=4",
239+
"profile": "https://tigerabrodi.dev/",
240+
"contributions": [
241+
"code",
242+
"test"
243+
]
244+
},
245+
{
246+
"login": "merodiro",
247+
"name": "Amr A.Mohammed",
248+
"avatar_url": "https://avatars1.githubusercontent.com/u/17033502?v=4",
249+
"profile": "https://github.com/merodiro",
250+
"contributions": [
251+
"code",
252+
"test"
253+
]
254+
},
255+
{
256+
"login": "juhanakristian",
257+
"name": "Juhana Jauhiainen",
258+
"avatar_url": "https://avatars1.githubusercontent.com/u/544386?v=4",
259+
"profile": "https://github.com/juhanakristian",
260+
"contributions": [
261+
"code"
262+
]
263+
},
264+
{
265+
"login": "jensmeindertsma",
266+
"name": "Jens Meindertsma",
267+
"avatar_url": "https://avatars3.githubusercontent.com/u/64677517?v=4",
268+
"profile": "https://github.com/jensmeindertsma",
269+
"contributions": [
270+
"code",
271+
"test"
272+
]
215273
}
216274
],
217275
"commitConvention": "none"

Diff for: .eslintrc

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
{
2-
"extends": "./node_modules/kcd-scripts/eslint.js",
2+
"extends": ["./node_modules/kcd-scripts/eslint.js"],
33
"rules": {
44
"max-lines-per-function": "off",
55
"no-constant-condition": "off",
66
"no-await-in-loop": "off",
7+
"no-console": "off",
8+
"import/no-unresolved": "off",
79
"react-hooks/rules-of-hooks": "off",
8-
"no-console": "off"
10+
"@typescript-eslint/no-floating-promises": "off",
11+
"@typescript-eslint/no-unnecessary-condition": "off"
12+
},
13+
"parserOptions": {
14+
"project": ["./tsconfig.json", "./test/tsconfig.json"]
915
}
1016
}

Diff for: CONTRIBUTING.md

-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ select the added contribution type.
2727
Please make sure to run the tests before you commit your changes. You can do so by running
2828
`npm test`.
2929

30-
### Update Typings
31-
32-
The TypeScript type definitions can be found in the
33-
[DefinitelyTyped repo](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/testing-library__react-hooks).
34-
3530
## Help needed
3631

3732
Please check out the

Diff for: README.md

+8
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
192192
<td align="center"><a href="https://github.com/joshuaellis"><img src="https://avatars0.githubusercontent.com/u/37798644?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Josh</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=joshuaellis" title="Documentation">📖</a></td>
193193
<td align="center"><a href="https://github.com/Goldziher"><img src="https://avatars1.githubusercontent.com/u/30733348?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Na'aman Hirschfeld</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=Goldziher" title="Code">💻</a></td>
194194
</tr>
195+
<tr>
196+
<td align="center"><a href="https://github.com/nobrayner"><img src="https://avatars2.githubusercontent.com/u/40751395?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Braydon Hall</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=nobrayner" title="Code">💻</a></td>
197+
<td align="center"><a href="https://dev.to/jacobmgevans"><img src="https://avatars1.githubusercontent.com/u/27247160?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jacob M-G Evans</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=JacobMGEvans" title="Code">💻</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=JacobMGEvans" title="Tests">⚠️</a></td>
198+
<td align="center"><a href="https://tigerabrodi.dev/"><img src="https://avatars1.githubusercontent.com/u/49603590?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tiger Abrodi</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=tigerabrodi" title="Code">💻</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=tigerabrodi" title="Tests">⚠️</a></td>
199+
<td align="center"><a href="https://github.com/merodiro"><img src="https://avatars1.githubusercontent.com/u/17033502?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Amr A.Mohammed</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=merodiro" title="Code">💻</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=merodiro" title="Tests">⚠️</a></td>
200+
<td align="center"><a href="https://github.com/juhanakristian"><img src="https://avatars1.githubusercontent.com/u/544386?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Juhana Jauhiainen</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=juhanakristian" title="Code">💻</a></td>
201+
<td align="center"><a href="https://github.com/jensmeindertsma"><img src="https://avatars3.githubusercontent.com/u/64677517?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jens Meindertsma</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=jensmeindertsma" title="Code">💻</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=jensmeindertsma" title="Tests">⚠️</a></td>
202+
</tr>
195203
</table>
196204

197205
<!-- markdownlint-restore -->

Diff for: docs/api-reference.md

-25
Original file line numberDiff line numberDiff line change
@@ -259,28 +259,3 @@ The maximum amount of time in milliseconds (ms) to wait. By default, no timeout
259259
If this option is set to `true`, any errors that occur while waiting are treated as a failed check.
260260
If this option is set to `false`, any errors that occur while waiting cause the promise to be
261261
rejected. By default, errors are not suppressed for this utility.
262-
263-
### `wait`
264-
265-
_(DEPRECATED, use [`waitFor`](/reference/api#waitfor) instead)_
266-
267-
```js
268-
function wait(callback: function(): boolean|void, options?: {
269-
timeout?: number,
270-
suppressErrors?: boolean
271-
}): Promise<void>
272-
```
273-
274-
Returns a `Promise` that resolves if the provided callback executes without exception and returns a
275-
truthy or `undefined` value. It is safe to use the [`result` of `renderHook`](/reference/api#result)
276-
in the callback to perform assertion or to test values.
277-
278-
#### `timeout`
279-
280-
The maximum amount of time in milliseconds (ms) to wait. By default, no timeout is applied.
281-
282-
#### `suppressErrors`
283-
284-
If this option is set to `true`, any errors that occur while waiting are treated as a failed check.
285-
If this option is set to `false`, any errors that occur while waiting cause the promise to be
286-
rejected. By default, errors are suppressed for this utility.

Diff for: jest.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
// eslint-disable-next-line
12
const { jest: jestConfig } = require('kcd-scripts/config')
23

34
module.exports = Object.assign(jestConfig, {
45
roots: ['<rootDir>/src', '<rootDir>/test'],
5-
testMatch: ['<rootDir>/test/*.js']
6+
testMatch: ['<rootDir>/test/*.(ts|tsx|js)']
67
})

Diff for: package.json

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.0.0-semantically-released",
44
"description": "Simple and complete React hooks testing utilities that encourage good testing practices.",
55
"main": "lib/index.js",
6+
"types": "lib/index.d.ts",
67
"keywords": [
78
"testing",
89
"react",
@@ -27,29 +28,34 @@
2728
"validate": "kcd-scripts validate",
2829
"prepare": "npm run build",
2930
"build": "kcd-scripts build --out-dir lib",
31+
"test": "kcd-scripts test",
32+
"typecheck": "kcd-scripts typecheck",
3033
"lint": "kcd-scripts lint",
3134
"format": "kcd-scripts format",
3235
"coverage": "codecov",
33-
"test": "kcd-scripts test",
3436
"docs:dev": "docz dev",
3537
"docs:build": "docz build",
3638
"contributors:add": "all-contributors add"
3739
},
3840
"dependencies": {
3941
"@babel/runtime": "^7.12.5",
40-
"@types/testing-library__react-hooks": "^3.4.0"
42+
"@types/react": ">=16.9.0",
43+
"@types/react-test-renderer": ">=16.9.0"
4144
},
4245
"devDependencies": {
46+
"@typescript-eslint/eslint-plugin": "^4.9.1",
47+
"@typescript-eslint/parser": "^4.9.1",
4348
"all-contributors-cli": "6.19.0",
4449
"codecov": "3.8.1",
4550
"docz": "2.3.1",
4651
"docz-theme-default": "1.2.0",
4752
"docz-utils": "2.3.0",
53+
"eslint": "7.15.0",
4854
"kcd-scripts": "7.5.2",
55+
"prettier": "^2.2.1",
4956
"react": "17.0.1",
5057
"react-test-renderer": "17.0.1",
51-
"typescript": "4.1.2",
52-
"eslint": "7.15.0"
58+
"typescript": "4.1.2"
5359
},
5460
"peerDependencies": {
5561
"react": ">=16.9.0",

Diff for: src/asyncUtils.js

-123
This file was deleted.

0 commit comments

Comments
 (0)