Skip to content

Commit

Permalink
feat: Convert to TypeScript (#520)
Browse files Browse the repository at this point in the history
* 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 <40751395+nobrayner@users.noreply.github.com>
Co-authored-by: Amr Gad <merodiro@gmail.com>
Co-authored-by: tigerabrodi <tigerabrodi@gmail.com>
Co-authored-by: Jacob Evans <cloud887@gmail.com>
Co-authored-by: Jacob M-G Evans <27247160+JacobMGEvans@users.noreply.github.com>
Co-authored-by: marcosvega91 <marcomoretti0103@gmail.com>
Co-authored-by: Michael Peyper <mpeyper7@gmail.com>
  • Loading branch information
8 people committed Dec 15, 2020
1 parent c53b56b commit 251ea70
Show file tree
Hide file tree
Showing 30 changed files with 373 additions and 383 deletions.
58 changes: 58 additions & 0 deletions .all-contributorsrc
Expand Up @@ -212,6 +212,64 @@
"contributions": [
"code"
]
},
{
"login": "nobrayner",
"name": "Braydon Hall",
"avatar_url": "https://avatars2.githubusercontent.com/u/40751395?v=4",
"profile": "https://github.com/nobrayner",
"contributions": [
"code"
]
},
{
"login": "JacobMGEvans",
"name": "Jacob M-G Evans",
"avatar_url": "https://avatars1.githubusercontent.com/u/27247160?v=4",
"profile": "https://dev.to/jacobmgevans",
"contributions": [
"code",
"test"
]
},
{
"login": "tigerabrodi",
"name": "Tiger Abrodi",
"avatar_url": "https://avatars1.githubusercontent.com/u/49603590?v=4",
"profile": "https://tigerabrodi.dev/",
"contributions": [
"code",
"test"
]
},
{
"login": "merodiro",
"name": "Amr A.Mohammed",
"avatar_url": "https://avatars1.githubusercontent.com/u/17033502?v=4",
"profile": "https://github.com/merodiro",
"contributions": [
"code",
"test"
]
},
{
"login": "juhanakristian",
"name": "Juhana Jauhiainen",
"avatar_url": "https://avatars1.githubusercontent.com/u/544386?v=4",
"profile": "https://github.com/juhanakristian",
"contributions": [
"code"
]
},
{
"login": "jensmeindertsma",
"name": "Jens Meindertsma",
"avatar_url": "https://avatars3.githubusercontent.com/u/64677517?v=4",
"profile": "https://github.com/jensmeindertsma",
"contributions": [
"code",
"test"
]
}
],
"commitConvention": "none"
Expand Down
10 changes: 8 additions & 2 deletions .eslintrc
@@ -1,10 +1,16 @@
{
"extends": "./node_modules/kcd-scripts/eslint.js",
"extends": ["./node_modules/kcd-scripts/eslint.js"],
"rules": {
"max-lines-per-function": "off",
"no-constant-condition": "off",
"no-await-in-loop": "off",
"no-console": "off",
"import/no-unresolved": "off",
"react-hooks/rules-of-hooks": "off",
"no-console": "off"
"@typescript-eslint/no-floating-promises": "off",
"@typescript-eslint/no-unnecessary-condition": "off"
},
"parserOptions": {
"project": ["./tsconfig.json", "./test/tsconfig.json"]
}
}
5 changes: 0 additions & 5 deletions CONTRIBUTING.md
Expand Up @@ -27,11 +27,6 @@ select the added contribution type.
Please make sure to run the tests before you commit your changes. You can do so by running
`npm test`.

### Update Typings

The TypeScript type definitions can be found in the
[DefinitelyTyped repo](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/testing-library__react-hooks).

## Help needed

Please check out the
Expand Down
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -192,6 +192,14 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<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>
<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>
</tr>
<tr>
<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>
<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>
<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>
<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>
<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>
<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>
</tr>
</table>

<!-- markdownlint-restore -->
Expand Down
25 changes: 0 additions & 25 deletions docs/api-reference.md
Expand Up @@ -259,28 +259,3 @@ The maximum amount of time in milliseconds (ms) to wait. By default, no timeout
If this option is set to `true`, any errors that occur while waiting are treated as a failed check.
If this option is set to `false`, any errors that occur while waiting cause the promise to be
rejected. By default, errors are not suppressed for this utility.

### `wait`

_(DEPRECATED, use [`waitFor`](/reference/api#waitfor) instead)_

```js
function wait(callback: function(): boolean|void, options?: {
timeout?: number,
suppressErrors?: boolean
}): Promise<void>
```

Returns a `Promise` that resolves if the provided callback executes without exception and returns a
truthy or `undefined` value. It is safe to use the [`result` of `renderHook`](/reference/api#result)
in the callback to perform assertion or to test values.

#### `timeout`

The maximum amount of time in milliseconds (ms) to wait. By default, no timeout is applied.

#### `suppressErrors`

If this option is set to `true`, any errors that occur while waiting are treated as a failed check.
If this option is set to `false`, any errors that occur while waiting cause the promise to be
rejected. By default, errors are suppressed for this utility.
3 changes: 2 additions & 1 deletion jest.config.js
@@ -1,6 +1,7 @@
// eslint-disable-next-line
const { jest: jestConfig } = require('kcd-scripts/config')

module.exports = Object.assign(jestConfig, {
roots: ['<rootDir>/src', '<rootDir>/test'],
testMatch: ['<rootDir>/test/*.js']
testMatch: ['<rootDir>/test/*.(ts|tsx|js)']
})
14 changes: 10 additions & 4 deletions package.json
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.0-semantically-released",
"description": "Simple and complete React hooks testing utilities that encourage good testing practices.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"keywords": [
"testing",
"react",
Expand All @@ -27,29 +28,34 @@
"validate": "kcd-scripts validate",
"prepare": "npm run build",
"build": "kcd-scripts build --out-dir lib",
"test": "kcd-scripts test",
"typecheck": "kcd-scripts typecheck",
"lint": "kcd-scripts lint",
"format": "kcd-scripts format",
"coverage": "codecov",
"test": "kcd-scripts test",
"docs:dev": "docz dev",
"docs:build": "docz build",
"contributors:add": "all-contributors add"
},
"dependencies": {
"@babel/runtime": "^7.12.5",
"@types/testing-library__react-hooks": "^3.4.0"
"@types/react": ">=16.9.0",
"@types/react-test-renderer": ">=16.9.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.9.1",
"@typescript-eslint/parser": "^4.9.1",
"all-contributors-cli": "6.19.0",
"codecov": "3.8.1",
"docz": "2.3.1",
"docz-theme-default": "1.2.0",
"docz-utils": "2.3.0",
"eslint": "7.15.0",
"kcd-scripts": "7.5.2",
"prettier": "^2.2.1",
"react": "17.0.1",
"react-test-renderer": "17.0.1",
"typescript": "4.1.2",
"eslint": "7.15.0"
"typescript": "4.1.2"
},
"peerDependencies": {
"react": ">=16.9.0",
Expand Down
123 changes: 0 additions & 123 deletions src/asyncUtils.js

This file was deleted.

0 comments on commit 251ea70

Please sign in to comment.