Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@testing-library/react-hooks

Package Overview
Dependencies
Maintainers
16
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@testing-library/react-hooks - npm Package Compare versions

Comparing version 4.0.0-beta.2 to 4.0.0-beta.3

dom/index.d.ts

10

lib/index.js

@@ -7,2 +7,4 @@ "use strict";

var _cleanup = require("./core/cleanup");
var _pure = require("./pure");

@@ -20,8 +22,2 @@

});
// Automatically registers cleanup in supported testing frameworks
if (typeof afterEach === 'function' && !process.env.RHTL_SKIP_AUTO_CLEANUP) {
afterEach(async () => {
await (0, _pure.cleanup)();
});
}
(0, _cleanup.autoRegisterCleanup)();

@@ -1,19 +0,4 @@

import React from 'react';
import { act } from 'react-test-renderer';
import { cleanup, addCleanup, removeCleanup } from './cleanup';
declare function renderHook<TProps, TResult>(callback: (props: TProps) => TResult, { initialProps, wrapper }?: {
initialProps?: TProps;
wrapper?: React.ComponentType<TProps>;
}): {
waitFor: (callback: () => boolean | void, { interval, timeout, suppressErrors }?: import("./asyncUtils").WaitOptions) => Promise<void>;
waitForNextUpdate: ({ timeout }?: Pick<import("./asyncUtils").WaitOptions, "timeout">) => Promise<void>;
waitForValueToChange: (selector: () => unknown, options?: import("./asyncUtils").WaitOptions) => Promise<void>;
result: {
readonly all: (Error | TResult | undefined)[];
readonly current: TResult;
readonly error: Error | undefined;
};
rerender: (newProps?: typeof initialProps) => void;
unmount: () => void;
};
export { renderHook, cleanup, addCleanup, removeCleanup, act };
declare const renderHook: <TProps, TResult>() => import("./types").RenderHook<TProps, TResult, import("./types").Renderer<TProps>>, act: import("./types").Act, cleanup: () => void, addCleanup: (callback: () => void | Promise<void>) => () => void, removeCleanup: (callback: () => void | Promise<void>) => void;
export { renderHook, act, cleanup, addCleanup, removeCleanup };
export * from './types';
export * from './types/react';
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.renderHook = renderHook;
Object.defineProperty(exports, "act", {
enumerable: true,
get: function () {
return _reactTestRenderer.act;
}
});
Object.defineProperty(exports, "cleanup", {
enumerable: true,
get: function () {
return _cleanup.cleanup;
}
});
Object.defineProperty(exports, "addCleanup", {
enumerable: true,
get: function () {
return _cleanup.addCleanup;
}
});
Object.defineProperty(exports, "removeCleanup", {
enumerable: true,
get: function () {
return _cleanup.removeCleanup;
}
});
var _exportNames = {
renderHook: true,
act: true,
cleanup: true,
addCleanup: true,
removeCleanup: true
};
exports.removeCleanup = exports.addCleanup = exports.cleanup = exports.act = exports.renderHook = void 0;
var _react = _interopRequireWildcard(require("react"));
var _types = require("./types");
var _reactTestRenderer = require("react-test-renderer");
var _asyncUtils = _interopRequireDefault(require("./asyncUtils"));
var _cleanup = require("./cleanup");
function isPromise(value) {
return typeof value.then === 'function';
}
function TestHook({
callback,
hookProps,
onError,
children
}) {
try {
// coerce undefined into TProps, so it maintains the previous behaviour
children(callback(hookProps));
} catch (err) {
if (isPromise(err)) {
throw err;
} else {
onError(err);
Object.keys(_types).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _types[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _types[key];
}
}
});
});
return null;
}
var _react = require("./types/react");
function Fallback() {
return null;
}
function resultContainer() {
const results = [];
const resolvers = [];
const result = {
get all() {
return results.map(({
value,
error
}) => error != null ? error : value);
},
get current() {
const {
value,
error
} = results[results.length - 1];
if (error) {
throw error;
}
return value;
},
get error() {
const {
error
} = results[results.length - 1];
return error;
Object.keys(_react).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _react[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _react[key];
}
});
});
const renderers = [{
required: 'react-test-renderer',
renderer: './native/pure'
}, {
required: 'react-dom',
renderer: './dom/pure'
}];
};
function hasDependency(name) {
try {
require(name);
const updateResult = (value, error) => {
results.push({
value,
error
});
resolvers.splice(0, resolvers.length).forEach(resolve => resolve());
};
return {
result,
addResolver: resolver => {
resolvers.push(resolver);
},
setValue: value => updateResult(value),
setError: error => updateResult(undefined, error)
};
return true;
} catch {
return false;
}
}
function renderHook(callback, {
initialProps,
wrapper
} = {}) {
const {
result,
setValue,
setError,
addResolver
} = resultContainer();
const hookProps = {
current: initialProps
};
function getRenderer() {
const validRenderer = renderers.find(({
required
}) => hasDependency(required));
const wrapUiIfNeeded = innerElement => wrapper ? /*#__PURE__*/_react.default.createElement(wrapper, hookProps.current, innerElement) : innerElement;
const toRender = () => wrapUiIfNeeded( /*#__PURE__*/_react.default.createElement(_react.Suspense, {
fallback: /*#__PURE__*/_react.default.createElement(Fallback, null)
}, /*#__PURE__*/_react.default.createElement(TestHook, {
callback: callback,
hookProps: hookProps.current,
onError: setError
}, setValue)));
let testRenderer;
(0, _reactTestRenderer.act)(() => {
testRenderer = (0, _reactTestRenderer.create)(toRender());
});
function unmountHook() {
(0, _reactTestRenderer.act)(() => {
(0, _cleanup.removeCleanup)(unmountHook);
testRenderer.unmount();
});
if (validRenderer) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require(validRenderer.renderer);
} else {
const options = renderers.map(({
required
}) => ` - ${required}`).sort((a, b) => a.localeCompare(b)).join('/n');
throw new Error(`Could not auto-detect a React renderer. Are you sure you've installed one of the following\n${options}`);
}
}
(0, _cleanup.addCleanup)(unmountHook);
return {
result,
rerender: function (newProps = hookProps.current) {
hookProps.current = newProps;
(0, _reactTestRenderer.act)(() => {
testRenderer.update(toRender());
});
},
unmount: unmountHook,
...(0, _asyncUtils.default)(addResolver)
};
}
const {
renderHook,
act,
cleanup,
addCleanup,
removeCleanup
} = getRenderer();
exports.removeCleanup = removeCleanup;
exports.addCleanup = addCleanup;
exports.cleanup = cleanup;
exports.act = act;
exports.renderHook = renderHook;
{
"name": "@testing-library/react-hooks",
"version": "4.0.0-beta.2",
"version": "4.0.0-beta.3",
"description": "Simple and complete React hooks testing utilities that encourage good testing practices.",

@@ -17,3 +17,6 @@ "main": "lib/index.js",

"src",
"pure.js",
"dom",
"native",
"server",
"pure",
"dont-cleanup-after-each.js"

@@ -31,3 +34,4 @@ ],

"prepare": "npm run build",
"build": "kcd-scripts build --out-dir lib",
"build": "kcd-scripts build --out-dir lib && npm run generate:submodules",
"generate:submodules": "ts-node scripts/generate-submodules.ts",
"test": "kcd-scripts test",

@@ -45,2 +49,3 @@ "typecheck": "kcd-scripts typecheck",

"@types/react": ">=16.9.0",
"@types/react-dom": ">=16.9.0",
"@types/react-test-renderer": ">=16.9.0"

@@ -60,3 +65,5 @@ },

"react": "17.0.1",
"react-dom": "^17.0.1",
"react-test-renderer": "17.0.1",
"ts-node": "^9.1.1",
"typescript": "4.1.3"

@@ -66,4 +73,16 @@ },

"react": ">=16.9.0",
"react-dom": ">=16.9.0",
"react-test-renderer": ">=16.9.0"
},
"peerDependenciesMeta": {
"react": {
"optional": true
},
"react-dom": {
"optional": true
},
"react-test-renderer": {
"optional": true
}
}
}

@@ -29,3 +29,3 @@ <div align="center">

[![All Contributors](https://img.shields.io/badge/all_contributors-13-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/github/all-contributors/testing-library/react-hooks-testing-library?color=orange&style=flat-square)](#contributors)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)

@@ -170,3 +170,3 @@ [![Code of Conduct](https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square)](https://github.com/testing-library/react-hooks-testing-library/blob/master/CODE_OF_CONDUCT.md)

<tr>
<td align="center"><a href="https://github.com/mpeyper"><img src="https://avatars0.githubusercontent.com/u/23029903?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Michael Peyper</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=mpeyper" title="Code">💻</a> <a href="#design-mpeyper" title="Design">🎨</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=mpeyper" title="Documentation">📖</a> <a href="#ideas-mpeyper" title="Ideas, Planning, & Feedback">🤔</a> <a href="#infra-mpeyper" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="#platform-mpeyper" title="Packaging/porting to new platform">📦</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=mpeyper" title="Tests">⚠️</a> <a href="#tool-mpeyper" title="Tools">🔧</a></td>
<td align="center"><a href="https://github.com/mpeyper"><img src="https://avatars0.githubusercontent.com/u/23029903?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Michael Peyper</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=mpeyper" title="Code">💻</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=mpeyper" title="Documentation">📖</a> <a href="#ideas-mpeyper" title="Ideas, Planning, & Feedback">🤔</a> <a href="#infra-mpeyper" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="#maintenance-mpeyper" title="Maintenance">🚧</a> <a href="#question-mpeyper" title="Answering Questions">💬</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=mpeyper" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/otofu-square"><img src="https://avatars0.githubusercontent.com/u/10118235?v=4?s=100" width="100px;" alt=""/><br /><sub><b>otofu-square</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=otofu-square" title="Code">💻</a></td>

@@ -194,3 +194,3 @@ <td align="center"><a href="https://github.com/ab18556"><img src="https://avatars2.githubusercontent.com/u/988696?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Patrick P. Henley</b></sub></a><br /><a href="#ideas-ab18556" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/testing-library/react-hooks-testing-library/pulls?q=is%3Apr+reviewed-by%3Aab18556" title="Reviewed Pull Requests">👀</a></td>

<td align="center"><a href="https://huchen.dev/"><img src="https://avatars3.githubusercontent.com/u/2078389?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Hu Chen</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=huchenme" title="Code">💻</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=huchenme" title="Documentation">📖</a> <a href="#example-huchenme" title="Examples">💡</a></td>
<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/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> <a href="#question-joshuaellis" title="Answering Questions">💬</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=joshuaellis" title="Code">💻</a> <a href="#ideas-joshuaellis" title="Ideas, Planning, & Feedback">🤔</a> <a href="#maintenance-joshuaellis" title="Maintenance">🚧</a> <a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=joshuaellis" title="Tests">⚠️</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>

@@ -205,3 +205,29 @@ </tr>

<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>
<td align="center"><a href="https://github.com/marcosvega91"><img src="https://avatars2.githubusercontent.com/u/5365582?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Marco Moretti</b></sub></a><br /><a href="#infra-marcosvega91" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td>
</tr>
<tr>
<td align="center"><a href="https://www.parkside.at/"><img src="https://avatars0.githubusercontent.com/u/27507295?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Martin V.</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=ndresx" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/erozak"><img src="https://avatars3.githubusercontent.com/u/22066282?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Erozak</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=erozak" title="Documentation">📖</a></td>
<td align="center"><a href="https://nickmccurdy.com/"><img src="https://avatars0.githubusercontent.com/u/927220?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nick McCurdy</b></sub></a><br /><a href="#maintenance-nickmccurdy" title="Maintenance">🚧</a></td>
<td align="center"><a href="https://codepen.io/aryyya/"><img src="https://avatars1.githubusercontent.com/u/29365565?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Arya</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=aryyya" title="Documentation">📖</a></td>
<td align="center"><a href="https://numb86.net/"><img src="https://avatars1.githubusercontent.com/u/16703337?v=4?s=100" width="100px;" alt=""/><br /><sub><b>numb86</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=numb86" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/foray1010"><img src="https://avatars3.githubusercontent.com/u/3212221?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Alex Young</b></sub></a><br /><a href="#maintenance-foray1010" title="Maintenance">🚧</a></td>
<td align="center"><a href="https://blam.sh/"><img src="https://avatars1.githubusercontent.com/u/3645856?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ben Lambert</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=benjdlambert" title="Documentation">📖</a></td>
</tr>
<tr>
<td align="center"><a href="https://github.com/ElRatonDeFuego"><img src="https://avatars1.githubusercontent.com/u/12750934?v=4?s=100" width="100px;" alt=""/><br /><sub><b>David Cho-Lerat</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=ElRatonDeFuego" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/evanharmon"><img src="https://avatars1.githubusercontent.com/u/8229989?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Evan Harmon</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=evanharmon" title="Documentation">📖</a></td>
<td align="center"><a href="http://codedaily.io/"><img src="https://avatars1.githubusercontent.com/u/1714673?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jason Brown</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=browniefed" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/kahwee"><img src="https://avatars1.githubusercontent.com/u/262105?v=4?s=100" width="100px;" alt=""/><br /><sub><b>KahWee Teng</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=kahwee" title="Documentation">📖</a></td>
<td align="center"><a href="http://shagabutdinov.com/"><img src="https://avatars2.githubusercontent.com/u/1635613?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Leonid Shagabutdinov</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=shagabutdinov" title="Documentation">📖</a></td>
<td align="center"><a href="https://levibutcher.dev/"><img src="https://avatars2.githubusercontent.com/u/31522433?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Levi Butcher</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=LeviButcher" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/7michele7"><img src="https://avatars2.githubusercontent.com/u/17926167?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Michele Settepani</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=7michele7" title="Documentation">📖</a></td>
</tr>
<tr>
<td align="center"><a href="https://github.com/samnoh"><img src="https://avatars1.githubusercontent.com/u/14857416?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Sam</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=samnoh" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/tanaypratap"><img src="https://avatars0.githubusercontent.com/u/10216863?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tanay Pratap</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=tanaypratap" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/techanvil"><img src="https://avatars0.githubusercontent.com/u/18395600?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tom Rees-Herdman</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=techanvil" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/iqbal125"><img src="https://avatars2.githubusercontent.com/u/24860061?v=4?s=100" width="100px;" alt=""/><br /><sub><b>iqbal125</b></sub></a><br /><a href="https://github.com/testing-library/react-hooks-testing-library/commits?author=iqbal125" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/cliffzhaobupt"><img src="https://avatars3.githubusercontent.com/u/7374506?v=4?s=100" width="100px;" alt=""/><br /><sub><b>cliffzhaobupt</b></sub></a><br /><a href="#maintenance-cliffzhaobupt" title="Maintenance">🚧</a></td>
</tr>
</table>

@@ -208,0 +234,0 @@

@@ -1,10 +0,5 @@

import { cleanup } from './pure'
import { autoRegisterCleanup } from './core/cleanup'
// Automatically registers cleanup in supported testing frameworks
if (typeof afterEach === 'function' && !process.env.RHTL_SKIP_AUTO_CLEANUP) {
afterEach(async () => {
await cleanup()
})
}
autoRegisterCleanup()
export * from './pure'
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc