🌜
ğŸŒž
@jest/environment

@jest/environment

v28.1.3

Delightful JavaScript Testing.

npm install @jest/environment

README

npm version Jest is released under the MIT license. Follow on Twitter

GitHub CI Status Coverage Status

Gitpod ready-to-code

 

🃏 Delightful JavaScript Testing

👩🏻‍💻 Developer Ready: A comprehensive JavaScript testing solution. Works out of the box for most JavaScript projects.

🏃🏽 Instant Feedback: Fast, interactive watch mode only runs test files related to changed files.

📸 Snapshot Testing: Capture snapshots of large objects to simplify testing and to analyze how they change over time.

See more on jestjs.io

Table of Contents

Getting Started

Install Jest using yarn:

yarn add --dev jest

Or npm:

npm install --save-dev jest

Note: Jest documentation uses yarn commands, but npm will also work. You can compare yarn and npm commands in the yarn docs, here.

Let's get started by writing a test for a hypothetical function that adds two numbers. First, create a sum.js file:

function sum(a, b) {
  return a + b;
}
module.exports = sum;

Then, create a file named sum.test.js. This will contain our actual test:

const sum = require('./sum');

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

Add the following section to your package.json:

{
  "scripts": {
    "test": "jest"
  }
}

Finally, run yarn test or npm test and Jest will print this message:

PASS  ./sum.test.js
✓ adds 1 + 2 to equal 3 (5ms)

You just successfully wrote your first test using Jest!

This test used expect and toBe to test that two values were exactly identical. To learn about the other things that Jest can test, see Using Matchers.

Running from command line

You can run Jest directly from the CLI (if it's globally available in your PATH, e.g. by yarn global add jest or npm install jest --global) with a variety of useful options.

Here's how to run Jest on files matching my-test, using config.json as a configuration file and display a native OS notification after the run:

jest my-test --notify --config=config.json

If you'd like to learn more about running jest through the command line, take a look at the Jest CLI Options page.

Additional Configuration

Generate a basic configuration file

Based on your project, Jest will ask you a few questions and will create a basic configuration file with a short description for each option:

jest --init

Using Babel

To use Babel, install required dependencies via yarn:

yarn add --dev babel-jest @babel/core @babel/preset-env

Configure Babel to target your current version of Node by creating a babel.config.js file in the root of your project:

// babel.config.js
module.exports = {
  presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
};

The ideal configuration for Babel will depend on your project. See Babel's docs for more details.

Making your Babel config jest-aware

Jest will set process.env.NODE_ENV to 'test' if it's not set to something else. You can use that in your configuration to conditionally setup only the compilation needed for Jest, e.g.

// babel.config.js
module.exports = api => {
  const isTest = api.env('test');
  // You can use isTest to determine what presets and plugins to use.

  return {
    // ...
  };
};

Note: babel-jest is automatically installed when installing Jest and will automatically transform files if a babel configuration exists in your project. To avoid this behavior, you can explicitly reset the transform configuration option:

// jest.config.js
module.exports = {
  transform: {},
};

Using webpack

Jest can be used in projects that use webpack to manage assets, styles, and compilation. webpack does offer some unique challenges over other tools. Refer to the webpack guide to get started.

Using Vite

Jest can be used in projects that use vite to serves source code over native ESM to provide some frontend tooling, vite is an opinionated tool and does offer some out-of-the box workflows. Jest is not fully supported by vite due to how the plugin system from vite works, but there is some working examples for first-class jest integration using the vite-jest, since this is not fully supported, you might as well read the limitation of the vite-jest. Refer to the vite guide to get started.

Using Parcel

Jest can be used in projects that use parcel-bundler to manage assets, styles, and compilation similar to webpack. Parcel requires zero configuration. Refer to the official docs to get started.

Using TypeScript

Jest supports TypeScript, via Babel. First, make sure you followed the instructions on using Babel above. Next, install the @babel/preset-typescript via yarn:

yarn add --dev @babel/preset-typescript

Then add @babel/preset-typescript to the list of presets in your babel.config.js.

// babel.config.js
module.exports = {
  presets: [
    ['@babel/preset-env', {targets: {node: 'current'}}],
+    '@babel/preset-typescript',
  ],
};

However, there are some caveats to using TypeScript with Babel. Because TypeScript support in Babel is purely transpilation, Jest will not type-check your tests as they are run. If you want that, you can use ts-jest instead, or just run the TypeScript compiler tsc separately (or as part of your build process).

Documentation

Learn more about using Jest on the official site!

Badge

Show the world you're using Jest → tested with jest jest tested jest

[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg?logo=jest)](https://github.com/facebook/jest)
[![jest tested](https://img.shields.io/badge/Jest-tested-eee.svg?logo=jest&labelColor=99424f)](https://github.com/facebook/jest)
[![jest](https://jestjs.io/img/jest-badge.svg)](https://github.com/facebook/jest)

Contributing

Development of Jest happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving Jest.

Code of Conduct

Facebook has adopted a Code of Conduct that we expect project participants to adhere to. Please read the full text so that you can understand what actions will and will not be tolerated.

Contributing Guide

Read our contributing guide to learn about our development process, how to propose bugfixes and improvements, and how to build and test your changes to Jest.

Good First Issues

To help you get your feet wet and get you familiar with our contribution process, we have a list of good first issues that contain bugs which have a relatively limited scope. This is a great place to get started.

Credits

This project exists thanks to all the people who contribute.

credit

Backers

Thank you to all our backers! 🙏

backer

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website.

sponsor sponsor sponsor sponsor sponsor sponsor sponsor sponsor sponsor sponsor

License

Jest is MIT licensed.

Release Notes

29.0.0-alpha.3
By Simen Bekkhus • Published on August 7, 2022

Features

  • [jest-worker] Adds workerIdleMemoryLimit option which is used as a check for worker memory leaks >= Node 16.11.0 and recycles child workers as required. (#13056)

Fixes

  • [jest-haste-map] Increase the maximum possible file size that jest-haste-map can handle (#13094)

New Contributors

Full Changelog: https://github.com/facebook/jest/compare/v29.0.0-alpha.1...v29.0.0-alpha.3

29.0.0-alpha.1
By Simen Bekkhus • Published on August 4, 2022

Features

  • [pretty-format] [BREAKING] Remove ConvertAnsi plugin in favour of jest-serializer-ansi-escapes (#13040)

Fixes

  • [jest-worker] When a process runs out of memory worker exits correctly and doesn't spin indefinitely (#13054)
  • [@jest/expect-utils] Fix deep equality of ImmutableJS Record (#13055)

New Contributors

Full Changelog: https://github.com/facebook/jest/compare/v29.0.0-alpha.0...v29.0.0-alpha.1

29.0.0-alpha.0
By Simen Bekkhus • Published on July 17, 2022

Features

  • [jest-config] [BREAKING] Make snapshotFormat default to escapeString: false and printBasicPrototype: false (#13036)
  • [jest-environment-jsdom] [BREAKING] Upgrade to [email protected] (#13037)

Chore & Maintenance

  • [*] [BREAKING] Drop support for Node v12 and v17 (#13033)
  • [jest-leak-detector] Remove support for weak-napi (#13035)

Full Changelog: https://github.com/facebook/jest/compare/v28.1.3...v29.0.0-alpha.0

28.1.3
By Simen Bekkhus • Published on July 13, 2022

Features

  • [jest-leak-detector] Use native FinalizationRegistry when it exists to get rid of external C dependency (#12973)

Fixes

  • [jest-changed-files] Fix a lock-up after repeated invocations (#12757)
  • [@jest/expect-utils] Fix deep equality of ImmutableJS OrderedSets (#12977)
  • [jest-mock] Add index signature support for spyOn types (#13013, #13020)
  • [jest-snapshot] Fix indentation of awaited inline snapshots (#12986)

Chore & Maintenance

  • [*] Replace internal usage of pretty-format/ConvertAnsi with jest-serializer-ansi-escapes (#12935, #13004)
  • [docs] Update spyOn docs (#13000)

New Contributors

Full Changelog: https://github.com/facebook/jest/compare/v28.1.2...v28.1.3

28.1.2
By Simen Bekkhus • Published on June 29, 2022

Fixes

  • [jest-runtime] Avoid star type import from @jest/globals (#12949)

Chore & Maintenance

  • [docs] Mention that jest-codemods now supports Sinon (#12898)

New Contributors

Full Changelog: https://github.com/facebook/jest/compare/v28.1.1...v28.1.2

28.1.1
By Simen Bekkhus • Published on June 7, 2022

Features

  • [jest] Expose Config type (#12848)
  • [@jest/reporters] Improve GitHubActionsReporters annotation format (#12826)
  • [@jest/types] Infer argument types passed to test and describe callback functions from each tables (#12885, #12905)

Fixes

  • [@jest/expect-utils] Fix deep equality of ImmutableJS OrderedMaps (#12763)
  • [jest-docblock] Handle multiline comments in parseWithComments (#12845)
  • [jest-mock] Improve spyOn error messages (#12901)
  • [jest-runtime] Correctly report V8 coverage with resetModules: true (#12912)
  • [jest-worker] Make JestWorkerFarm helper type to include methods of worker module that take more than one argument (#12839)

Chore & Maintenance

  • [docs] Updated docs to indicate that jest-environment-jsdom is a separate package #12828

  • [docs] Document the comments used by coverage providers #12835

  • [docs] Use docusaurus-remark-plugin-tab-blocks to format tabs with code examples (#12859)

  • [jest-haste-map] Bump walker version (#12324)

  • [expect] Adjust typings of lastCalledWith, nthCalledWith, toBeCalledWith matchers to allow a case there a mock was called with no arguments (#12807)

  • [@jest/expect-utils] Fix deep equality of ImmutableJS Lists (#12763)

  • [jest-core] Do not collect SIGNREQUEST as open handles (#12789)

New Contributors

Full Changelog: https://github.com/facebook/jest/compare/v28.1.0...v28.1.1

28.1.0
By Simen Bekkhus • Published on May 6, 2022

Features

  • [jest-circus] Add failing test modifier that inverts the behaviour of tests (#12610)
  • [jest-environment-node, jest-environment-jsdom] Allow specifying customExportConditions (#12774)

Fixes

  • [expect] Adjust typings of lastCalledWith, nthCalledWith, toBeCalledWith matchers to allow a case there a mock was called with no arguments (#12807)
  • [@jest/expect-utils] Fix deep equality of ImmutableJS Lists (#12763)
  • [jest-core] Do not collect SIGNREQUEST as open handles (#12789)

Chore & Maintenance

  • [docs] Specified documentation about --filter CLI docs (#12799)
  • [@jest-reporters] Move helper functions from utils.ts into separate files (#12782)
  • [jest-resolve] Replace process.versions.pnp type declaration with @types/pnpapi devDependency (#12783)

New Contributors

Full Changelog: https://github.com/facebook/jest/compare/v28.0.3...v28.1.0

28.0.2
By Simen Bekkhus • Published on April 27, 2022

Features

  • [jest-worker] Add JestWorkerFarm helper type (#12753)

Fixes

  • [*] Lower Node 16 requirement to 16.10 from 16.13 due to a Node bug that causes memory and performance issues (#12754)

Full Changelog: https://github.com/facebook/jest/compare/v28.0.1...v28.0.2

General

License
MIT
Typescript Types
Built-in
Tree-shakeable
No

Popularity

GitHub Stargazers
40.2K
Community Interest
42.9K
Number of Forks
6,059

Maintenance

Commits
10/219/220250
Last Commit
Open Issues
895
Closed Issues
6,149
Open Pull Requests
169
Closed Pull Requests
1,208

Versions

Versions Released
10/219/2208
Latest Version Released
Jul 13, 2022
Current Tags
latest28.1.3
next29.0.0-alpha.3

Dependencies

Dependencies (4)
Dev Dependencies (0)

Contributors

SimenB
SimenB
Commits: 1,024
cpojer
cpojer
Commits: 660
aaronabramov
aaronabramov
Commits: 213
thymikee
thymikee
Commits: 174
pedrottimark
pedrottimark
Commits: 161
mjesun
mjesun
Commits: 106
jeysal
jeysal
Commits: 63
rogeliog
rogeliog
Commits: 56
rickhanlonii
rickhanlonii
Commits: 55
rubennorte
rubennorte
Commits: 55
wtgtybhertgeghgtwtg
wtgtybhertgeghgtwtg
Commits: 44
Connormiha
Connormiha
Commits: 40
mattphillips
mattphillips
Commits: 40
kentaromiura
kentaromiura
Commits: 38
orta
orta
Commits: 36