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 5.0.0-beta.7 to 5.0.0-beta.8

src/__tests__/defaultRenderer.test.ts

5

lib/core/cleanup.d.ts

@@ -0,5 +1,6 @@

import { CleanupCallback } from '../types';
declare function cleanup(): Promise<void>;
declare function addCleanup(callback: () => Promise<void> | void): () => void;
declare function removeCleanup(callback: () => Promise<void> | void): void;
declare function addCleanup(callback: CleanupCallback): () => void;
declare function removeCleanup(callback: CleanupCallback): void;
declare function autoRegisterCleanup(): void;
export { cleanup, addCleanup, removeCleanup, autoRegisterCleanup };

11

lib/core/index.d.ts

@@ -1,4 +0,11 @@

import { CreateRenderer, Renderer, RenderHook } from '../types';
import { CreateRenderer, Renderer, RenderResult, RenderHookOptions } from '../types';
import { cleanup, addCleanup, removeCleanup } from './cleanup';
declare function createRenderHook<TProps, TResult, TOptions extends object, TRenderer extends Renderer<TProps>>(createRenderer: CreateRenderer<TProps, TResult, TOptions, TRenderer>): RenderHook<TProps, TResult, TOptions, TRenderer>;
declare function createRenderHook<TProps, TResult, TRendererOptions extends object, TRenderer extends Renderer<TProps>>(createRenderer: CreateRenderer<TProps, TResult, TRendererOptions, TRenderer>): (callback: (props: TProps) => TResult, options?: RenderHookOptions<TProps> & TRendererOptions) => {
waitFor: (callback: () => boolean | void, opts?: import("../types").WaitOptions | undefined) => Promise<void>;
waitForNextUpdate: (opts?: Pick<import("../types").WaitOptions, "timeout"> | undefined) => Promise<void>;
waitForValueToChange: (selector: () => unknown, options?: import("../types").WaitOptions | undefined) => Promise<void>;
result: RenderResult<TResult>;
rerender: (newProps?: TProps | undefined) => void;
unmount: () => void;
} & Pick<TRenderer, Exclude<keyof TRenderer, "render" | "act" | "rerender" | "unmount">>;
export { createRenderHook, cleanup, addCleanup, removeCleanup };

@@ -42,6 +42,8 @@ "use strict";

get current() {
var _results;
const {
value,
error
} = results[results.length - 1];
} = (_results = results[results.length - 1]) != null ? _results : {};

@@ -90,2 +92,7 @@ if (error) {

} = resultContainer();
const renderProps = {
callback,
setValue,
setError
};
let hookProps = options.initialProps;

@@ -98,7 +105,3 @@ const {

...renderUtils
} = createRenderer({
callback,
setValue,
setError
}, options);
} = createRenderer(renderProps, options);
render(hookProps);

@@ -124,10 +127,5 @@

};
}; // If the function name does not get used before it is returned,
// it's name is removed by babel-plugin-minify-dead-code-elimination.
// This dummy usage works around that.
};
renderHook.name; // eslint-disable-line @typescript-eslint/no-unused-expressions
return renderHook;
}
import { act } from 'react-dom/test-utils';
import { RendererOptions } from '../types/react';
import { cleanup, addCleanup, removeCleanup } from '../core';
declare const renderHook: <TProps, TResult>(callback: (props: TProps) => TResult, options?: import("../types").RenderHookOptions<TProps, RendererOptions<TProps>> | undefined) => import("../types").RenderHookResult<TProps, TResult, {
declare const renderHook: <TProps, TResult>(callback: (props: TProps) => TResult, options?: import("../types").RenderHookOptions<TProps> & RendererOptions<TProps>) => {
waitFor: (callback: () => boolean | void, opts?: import("../types").WaitOptions | undefined) => Promise<void>;
waitForNextUpdate: (opts?: Pick<import("../types").WaitOptions, "timeout"> | undefined) => Promise<void>;
waitForValueToChange: (selector: () => unknown, options?: import("../types").WaitOptions | undefined) => Promise<void>;
result: import("../types").RenderResult<TResult>;
rerender: (newProps?: TProps | undefined) => void;
unmount: () => void;
} & Pick<{
render(props?: TProps | undefined): void;

@@ -9,5 +15,5 @@ rerender(props?: TProps | undefined): void;

act: typeof act;
}>;
export { renderHook, act, cleanup, addCleanup, removeCleanup };
export * from '../types';
}, never>;
export { renderHook, act };
export { cleanup, addCleanup, removeCleanup } from '../core';
export * from '../types/react';

@@ -49,16 +49,2 @@ "use strict";

var _types = require("../types");
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];
}
});
});
var _react = require("../types/react");

@@ -65,0 +51,0 @@

/// <reference types="react" />
import { RendererProps } from '../types';
import { WrapperComponent } from '../types/react';
import { RendererProps, WrapperComponent } from '../types/react';
declare function createTestHarness<TProps, TResult>(rendererProps: RendererProps<TProps, TResult>, Wrapper?: WrapperComponent<TProps>, suspense?: boolean): (props?: TProps | undefined) => JSX.Element;
export { createTestHarness };

@@ -55,10 +55,5 @@ "use strict";

return component;
}; // If the function name does not get used before it is returned,
// it's name is removed by babel-plugin-minify-dead-code-elimination.
// This dummy usage works around that.
};
testHarness.name; // eslint-disable-line @typescript-eslint/no-unused-expressions
return testHarness;
}
import { act } from 'react-test-renderer';
import { RendererOptions } from '../types/react';
import { cleanup, addCleanup, removeCleanup } from '../core';
declare const renderHook: <TProps, TResult>(callback: (props: TProps) => TResult, options?: import("../types").RenderHookOptions<TProps, RendererOptions<TProps>> | undefined) => import("../types").RenderHookResult<TProps, TResult, {
declare const renderHook: <TProps, TResult>(callback: (props: TProps) => TResult, options?: import("../types").RenderHookOptions<TProps> & RendererOptions<TProps>) => {
waitFor: (callback: () => boolean | void, opts?: import("../types").WaitOptions | undefined) => Promise<void>;
waitForNextUpdate: (opts?: Pick<import("../types").WaitOptions, "timeout"> | undefined) => Promise<void>;
waitForValueToChange: (selector: () => unknown, options?: import("../types").WaitOptions | undefined) => Promise<void>;
result: import("../types").RenderResult<TResult>;
rerender: (newProps?: TProps | undefined) => void;
unmount: () => void;
} & Pick<{
render(props?: TProps | undefined): void;

@@ -9,5 +15,5 @@ rerender(props?: TProps | undefined): void;

act: typeof act;
}>;
export { renderHook, act, cleanup, addCleanup, removeCleanup };
export * from '../types';
}, never>;
export { renderHook, act };
export { cleanup, addCleanup, removeCleanup } from '../core';
export * from '../types/react';

@@ -45,16 +45,2 @@ "use strict";

var _types = require("../types");
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];
}
});
});
var _react = require("../types/react");

@@ -61,0 +47,0 @@

@@ -1,4 +0,3 @@

declare const renderHook: <TProps, TResult>(callback: (props: TProps) => TResult, options?: import("./types").RenderHookOptions<TProps, import("./types/react").RendererOptions<TProps>> | undefined) => import("./types").RenderHookResult<TProps, TResult, import("./types").Renderer<TProps>>, act: import("./types").Act, cleanup: () => void, addCleanup: (callback: () => void | Promise<void>) => () => void, removeCleanup: (callback: () => void | Promise<void>) => void;
declare const renderHook: <TProps, TResult>(callback: (props: TProps) => TResult, options?: import("./types/react").RenderHookOptions<TProps> | undefined) => import("./types").RenderHookResult<TProps, TResult, import("./types").Renderer<TProps>>, act: import("./types").Act, cleanup: () => void, addCleanup: (callback: import("./types").CleanupCallback) => () => void, removeCleanup: (callback: import("./types").CleanupCallback) => void;
export { renderHook, act, cleanup, addCleanup, removeCleanup };
export * from './types';
export * from './types/react';

@@ -15,16 +15,2 @@ "use strict";

var _types = require("./types");
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];
}
});
});
var _react = require("./types/react");

@@ -31,0 +17,0 @@

import { act } from 'react-dom/test-utils';
import { RendererOptions } from '../types/react';
import { cleanup, addCleanup, removeCleanup } from '../core';
declare const renderHook: <TProps, TResult>(callback: (props: TProps) => TResult, options?: import("../types").RenderHookOptions<TProps, RendererOptions<TProps>> | undefined) => import("../types").RenderHookResult<TProps, TResult, {
declare const renderHook: <TProps, TResult>(callback: (props: TProps) => TResult, options?: import("../types").RenderHookOptions<TProps> & RendererOptions<TProps>) => {
waitFor: (callback: () => boolean | void, opts?: import("../types").WaitOptions | undefined) => Promise<void>;
waitForNextUpdate: (opts?: Pick<import("../types").WaitOptions, "timeout"> | undefined) => Promise<void>;
waitForValueToChange: (selector: () => unknown, options?: import("../types").WaitOptions | undefined) => Promise<void>;
result: import("../types").RenderResult<TResult>;
rerender: (newProps?: TProps | undefined) => void;
unmount: () => void;
} & Pick<{
render(props?: TProps | undefined): void;

@@ -10,5 +16,5 @@ hydrate(): void;

act: typeof act;
}>;
export { renderHook, act, cleanup, addCleanup, removeCleanup };
export * from '../types';
}, "hydrate">;
export { renderHook, act };
export { cleanup, addCleanup, removeCleanup } from '../core';
export * from '../types/react';

@@ -51,16 +51,2 @@ "use strict";

var _types = require("../types");
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];
}
});
});
var _react = require("../types/react");

@@ -67,0 +53,0 @@

@@ -12,5 +12,5 @@ export declare type Renderer<TProps> = {

};
export declare type CreateRenderer<TProps, TResult, TOptions, TRenderer extends Renderer<TProps>> = (props: RendererProps<TProps, TResult>, options: TOptions) => TRenderer;
export declare type CreateRenderer<TProps, TResult, TRendererOptions extends object, TRenderer extends Renderer<TProps>> = (props: RendererProps<TProps, TResult>, options: TRendererOptions) => TRenderer;
export declare type RenderResult<TValue> = {
readonly all: (TValue | Error | undefined)[];
readonly all: Array<TValue | Error>;
readonly current: TValue;

@@ -33,11 +33,9 @@ readonly error?: Error;

export declare type RenderHookResult<TProps, TValue, TRenderer extends Renderer<TProps> = Renderer<TProps>> = ResultContainer<TValue> & Omit<Renderer<TProps>, 'render' | 'act'> & Omit<TRenderer, keyof Renderer<TProps>> & AsyncUtils;
export declare type RenderHookOptions<TProps, TOptions extends object> = TOptions & {
export declare type RenderHookOptions<TProps> = {
initialProps?: TProps;
};
export interface RenderHook<TProps, TResult, TOptions extends object, TRenderer extends Renderer<TProps> = Renderer<TProps>> {
(callback: (props: TProps) => TResult, options?: RenderHookOptions<TProps, TOptions>): RenderHookResult<TProps, TResult, TRenderer>;
}
export interface Act {
export declare type Act = {
(callback: () => void | undefined): void;
(callback: () => Promise<void | undefined>): Promise<undefined>;
}
};
export declare type CleanupCallback = () => Promise<void> | void;
import { ComponentType } from 'react';
import { RenderHookOptions, RenderHookResult, Act } from '.';
import { RenderHookOptions as BaseRenderHookOptions, RenderHookResult, Act, CleanupCallback } from '.';
export declare type WrapperComponent<TProps> = ComponentType<TProps>;

@@ -7,8 +7,12 @@ export declare type RendererOptions<TProps> = {

};
export declare type RenderHookOptions<TProps> = BaseRenderHookOptions<TProps> & {
wrapper?: WrapperComponent<TProps>;
};
export declare type ReactHooksRenderer = {
renderHook: <TProps, TResult>(callback: (props: TProps) => TResult, options?: RenderHookOptions<TProps, RendererOptions<TProps>>) => RenderHookResult<TProps, TResult>;
renderHook: <TProps, TResult>(callback: (props: TProps) => TResult, options?: RenderHookOptions<TProps>) => RenderHookResult<TProps, TResult>;
act: Act;
cleanup: () => void;
addCleanup: (callback: () => Promise<void> | void) => () => void;
removeCleanup: (callback: () => Promise<void> | void) => void;
addCleanup: (callback: CleanupCallback) => () => void;
removeCleanup: (callback: CleanupCallback) => void;
};
export * from '.';

@@ -1,1 +0,18 @@

"use strict";
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _ = require(".");
Object.keys(_).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (key in exports && exports[key] === _[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function () {
return _[key];
}
});
});
{
"name": "@testing-library/react-hooks",
"version": "5.0.0-beta.7",
"version": "5.0.0-beta.8",
"description": "Simple and complete React hooks testing utilities that encourage good testing practices.",

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

"eslint": "7.17.0",
"kcd-scripts": "7.5.3",
"kcd-scripts": "7.5.4",
"prettier": "^2.2.1",

@@ -62,0 +62,0 @@ "react": "17.0.1",

@@ -1,3 +0,5 @@

let cleanupCallbacks: (() => Promise<void> | void)[] = []
import { CleanupCallback } from '../types'
let cleanupCallbacks: Array<CleanupCallback> = []
async function cleanup() {

@@ -10,3 +12,3 @@ for (const callback of cleanupCallbacks) {

function addCleanup(callback: () => Promise<void> | void) {
function addCleanup(callback: CleanupCallback) {
cleanupCallbacks = [callback, ...cleanupCallbacks]

@@ -16,3 +18,3 @@ return () => removeCleanup(callback)

function removeCleanup(callback: () => Promise<void> | void) {
function removeCleanup(callback: CleanupCallback) {
cleanupCallbacks = cleanupCallbacks.filter((cb) => cb !== callback)

@@ -19,0 +21,0 @@ }

@@ -1,3 +0,2 @@

import { CreateRenderer, Renderer, RenderResult, RenderHook, RenderHookOptions } from '../types'
import { ResultContainer } from '../types/internal'
import { CreateRenderer, Renderer, RenderResult, RenderHookOptions } from '../types'

@@ -7,3 +6,3 @@ import { asyncUtils } from './asyncUtils'

function resultContainer<TValue>(): ResultContainer<TValue> {
function resultContainer<TValue>() {
const results: Array<{ value?: TValue; error?: Error }> = []

@@ -14,6 +13,6 @@ const resolvers: Array<() => void> = []

get all() {
return results.map(({ value, error }) => error ?? value)
return results.map(({ value, error }) => error ?? (value as TValue))
},
get current() {
const { value, error } = results[results.length - 1]
const { value, error } = results[results.length - 1] ?? {}
if (error) {

@@ -48,8 +47,8 @@ throw error

TResult,
TOptions extends object,
TRendererOptions extends object,
TRenderer extends Renderer<TProps>
>(createRenderer: CreateRenderer<TProps, TResult, TOptions, TRenderer>) {
const renderHook: RenderHook<TProps, TResult, TOptions, TRenderer> = (
callback,
options = {} as RenderHookOptions<TProps, TOptions>
>(createRenderer: CreateRenderer<TProps, TResult, TRendererOptions, TRenderer>) {
const renderHook = (
callback: (props: TProps) => TResult,
options = {} as RenderHookOptions<TProps> & TRendererOptions
) => {

@@ -85,7 +84,2 @@ const { result, setValue, setError, addResolver } = resultContainer<TResult>()

// If the function name does not get used before it is returned,
// it's name is removed by babel-plugin-minify-dead-code-elimination.
// This dummy usage works around that.
renderHook.name // eslint-disable-line @typescript-eslint/no-unused-expressions
return renderHook

@@ -92,0 +86,0 @@ }

import ReactDOM from 'react-dom'
import { act } from 'react-dom/test-utils'
import { RendererProps } from '../types'
import { RendererOptions } from '../types/react'
import { RendererProps, RendererOptions } from '../types/react'
import { createRenderHook, cleanup, addCleanup, removeCleanup } from '../core'
import { createRenderHook } from '../core'
import { createTestHarness } from '../helpers/createTestHarness'

@@ -42,5 +41,6 @@

export { renderHook, act, cleanup, addCleanup, removeCleanup }
export { renderHook, act }
export * from '../types'
export { cleanup, addCleanup, removeCleanup } from '../core'
export * from '../types/react'
import { act, create, ReactTestRenderer } from 'react-test-renderer'
import { RendererProps } from '../types'
import { RendererOptions } from '../types/react'
import { RendererProps, RendererOptions } from '../types/react'
import { createRenderHook, cleanup, addCleanup, removeCleanup } from '../core'
import { createRenderHook } from '../core'
import { createTestHarness } from '../helpers/createTestHarness'

@@ -39,5 +38,6 @@

export { renderHook, act, cleanup, addCleanup, removeCleanup }
export { renderHook, act }
export * from '../types'
export { cleanup, addCleanup, removeCleanup } from '../core'
export * from '../types/react'

@@ -39,3 +39,2 @@ import { ReactHooksRenderer } from './types/react'

export * from './types'
export * from './types/react'

@@ -5,6 +5,5 @@ import ReactDOMServer from 'react-dom/server'

import { RendererProps } from '../types'
import { RendererOptions } from '../types/react'
import { RendererProps, RendererOptions } from '../types/react'
import { createRenderHook, cleanup, addCleanup, removeCleanup } from '../core'
import { createRenderHook } from '../core'
import { createTestHarness } from '../helpers/createTestHarness'

@@ -64,5 +63,6 @@

export { renderHook, act, cleanup, addCleanup, removeCleanup }
export { renderHook, act }
export * from '../types'
export { cleanup, addCleanup, removeCleanup } from '../core'
export * from '../types/react'

@@ -14,9 +14,11 @@ export type Renderer<TProps> = {

export type CreateRenderer<TProps, TResult, TOptions, TRenderer extends Renderer<TProps>> = (
props: RendererProps<TProps, TResult>,
options: TOptions
) => TRenderer
export type CreateRenderer<
TProps,
TResult,
TRendererOptions extends object,
TRenderer extends Renderer<TProps>
> = (props: RendererProps<TProps, TResult>, options: TRendererOptions) => TRenderer
export type RenderResult<TValue> = {
readonly all: (TValue | Error | undefined)[]
readonly all: Array<TValue | Error>
readonly current: TValue

@@ -51,21 +53,11 @@ readonly error?: Error

export type RenderHookOptions<TProps, TOptions extends object> = TOptions & {
export type RenderHookOptions<TProps> = {
initialProps?: TProps
}
export interface RenderHook<
TProps,
TResult,
TOptions extends object,
TRenderer extends Renderer<TProps> = Renderer<TProps>
> {
(
callback: (props: TProps) => TResult,
options?: RenderHookOptions<TProps, TOptions>
): RenderHookResult<TProps, TResult, TRenderer>
}
export interface Act {
export type Act = {
(callback: () => void | undefined): void
(callback: () => Promise<void | undefined>): Promise<undefined>
}
export type CleanupCallback = () => Promise<void> | void
import { ComponentType } from 'react'
import { RenderHookOptions, RenderHookResult, Act } from '.'
import {
RenderHookOptions as BaseRenderHookOptions,
RenderHookResult,
Act,
CleanupCallback
} from '.'

@@ -11,11 +16,17 @@ export type WrapperComponent<TProps> = ComponentType<TProps>

export type RenderHookOptions<TProps> = BaseRenderHookOptions<TProps> & {
wrapper?: WrapperComponent<TProps>
}
export type ReactHooksRenderer = {
renderHook: <TProps, TResult>(
callback: (props: TProps) => TResult,
options?: RenderHookOptions<TProps, RendererOptions<TProps>>
options?: RenderHookOptions<TProps>
) => RenderHookResult<TProps, TResult>
act: Act
cleanup: () => void
addCleanup: (callback: () => Promise<void> | void) => () => void
removeCleanup: (callback: () => Promise<void> | void) => void
addCleanup: (callback: CleanupCallback) => () => void
removeCleanup: (callback: CleanupCallback) => void
}
export * from '.'

Sorry, the diff of this file is not supported yet

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