Package detail

@xunnamius/jest-types

Xunnamius57MITdeprecated1.1.3

This package has been superseded by @-xun/jest

Various constants, utility types, and typed functions for Jest-based projects

type, types, typescript, ergodark

readme

![Black Lives Matter!][badge-blm] ![Maintenance status][badge-maintenance] ![Last commit timestamp][badge-last-commit] ![Open issues][badge-issues] ![Pull requests][badge-pulls] ![Codecov][badge-codecov] ![Source license][badge-license] [![Tree shaking support][badge-tree-shaking]][link-bundlephobia] [Compressed package size][link-bundlephobia] ![NPM version][badge-npm] ![Uses Semantic Release!][badge-semantic-release]

@xunnamius/jest-types


This package contains several TypeScript utility types and helper functions for use with Jest-based projects.

Install

npm install --save-dev @xunnamius/jest-types

Usage

You can use this library's exports in your TypeScript projects like so:

import type { X } from '@xunnamius/jest-types'

const x: X = 'y';

Type and Constant Glossary

This package exports the following: (none yet)

Function Glossary

The following functions are available:

asMockedFunction

See the docs for interface description.

This function returns fn or a function type T wrapped with Jest mock type definitions via jest.MockedFunction.

import execa from 'execa';
import debugFactory from 'debug';

import type { Debugger } from 'debug';
import type { ExecaChildProcess } from 'execa';

jest.mock('execa');
jest.mock('debug');

const mockedExeca = asMockedFunction(execa);
const mockedDebug = asMockedFunction<Debugger>();

// ...

beforeEach(() => {
  mockedDebug.extend =
    asMockedFunction<Debugger['extend']>().mockReturnValue(mockedDebug);

  asMockedFunction(debugFactory).mockReturnValue(mockedDebug);

  mockedExeca.mockImplementation(
    () =>
      Promise.resolve({
        /* ... */
      }) as ExecaChildProcess<Buffer>
  );
});

// ...

it('throws and outputs to CLI when doing a thing that fails', async () => {
  expect.hasAssertions();

  doThingThatFails(); // <== calls execa internally

  expect(mockedDebug).toBeCalledWith(expect.stringContaining('failed!'));
});

asMockedClass

See the docs for interface description.

This function returns returns classConstructor or a constructor type T wrapped with Jest mock type definitions via jest.MockedClass.

import { Db, MongoClient } from 'mongodb';

jest.mock('mongodb');

const mockMongoClient = asMockedClass(MongoClient);

beforeEach(() => {
  mockMongoClient.connect = jest.fn((url: string) =>
    Promise.resolve(
      new (class {
        // ...
      })() as unknown as MongoClient
    )
  );
});

it("creates client only if it doesn't already exist", async () => {
  expect.hasAssertions();

  getMemoizedClientConnection();
  getMemoizedClientConnection();
  getMemoizedClientConnection();

  expect(mockMongoClient.connect).toHaveBeenCalledTimes(1);
});

Documentation

Further documentation can be found under docs/.

License

[![FOSSA analysis][badge-fossa]][link-fossa]

Contributing and Support

[New issues][choose-new-issue] and pull requests are always welcome and greatly appreciated! 🤩 Just as well, you can star 🌟 this project to let me know you found it useful! ✊🏿 Thank you!

See CONTRIBUTING.md and SUPPORT.md for more information.

[badge-blm]: https://api.ergodark.com/badges/blm 'Join the movement!'

[badge-maintenance]: https://img.shields.io/maintenance/active/2022 'Is this package maintained?'

[badge-last-commit]: https://img.shields.io/github/last-commit/xunnamius/typescript-utils 'Latest commit timestamp' [badge-issues]: https://img.shields.io/github/issues/Xunnamius/typescript-utils 'Open issues'

[badge-pulls]: https://img.shields.io/github/issues-pr/xunnamius/typescript-utils 'Open pull requests'

[badge-codecov]: https://codecov.io/gh/Xunnamius/typescript-utils/branch/main/graph/badge.svg?token=HWRIOBAAPW 'Is this package well-tested?'

[badge-license]: https://img.shields.io/npm/l/@xunnamius/jest-types "This package's source license"

[badge-fossa]: https://app.fossa.com/api/projects/custom%2B27276%2Fgit%40github.com%3AXunnamius%2Ftypescript-utils.git.svg?type=large "Analysis of this package's license obligations" [link-fossa]: https://app.fossa.com/projects/custom+27276%2Fgit@github.com:Xunnamius%2Ftypescript-utils.git [badge-npm]: https://api.ergodark.com/badges/npm-pkg-version/@xunnamius/jest-types 'Install this package using npm or yarn!'

[badge-semantic-release]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg 'This repo practices continuous integration and deployment!'

[badge-tree-shaking]: https://badgen.net/bundlephobia/tree-shaking/@xunnamius/jest-types 'Is this package optimized for Webpack?' [link-bundlephobia]: https://bundlephobia.com/result?p=@xunnamius/jest-types 'Package size (minified and gzipped)'

[choose-new-issue]: https://github.com/xunnamius/typescript-utils/issues/new/choose