Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Jest is a delightful JavaScript Testing Framework with a focus on simplicity. It works with projects using: Babel, TypeScript, Node, React, Angular, Vue, and more. It's designed to ensure correctness of any JavaScript codebase. It allows you to write tests with an approachable, familiar, and feature-rich API that gives you results quickly.
Unit Testing
Jest allows you to write unit tests for your JavaScript functions. In this example, a simple test is written to check if the addition of 1 and 2 equals 3.
test('adds 1 + 2 to equal 3', () => {
expect(1 + 2).toBe(3);
});
Mocking
Jest provides a powerful mocking library that lets you mock objects, functions, and modules. In this example, an HTTP request is mocked to test the behavior of a function that fetches user data.
jest.mock('../api');
test('should fetch users', () => {
const users = [{name: 'Bob'}];
const resp = {data: users};
axios.get.mockResolvedValue(resp);
return Users.all().then(data => expect(data).toEqual(users));
});
Snapshot Testing
Snapshot testing enables you to test your UI or any serializable value to ensure it does not change unexpectedly. This example demonstrates testing an object against a snapshot, with some fields expected to match specific types rather than values.
test('will check the matchers and pass', () => {
const user = {
createdAt: new Date(),
id: Math.floor(Math.random() * 20),
name: 'LeBron James'
};
expect(user).toMatchSnapshot({
createdAt: expect.any(Date),
id: expect.any(Number)
});
});
Asynchronous Testing
Jest supports testing of asynchronous code. In this example, an asynchronous function `fetchData` is tested to ensure it resolves with the expected value 'peanut butter'.
test('the data is peanut butter', async () => {
await expect(fetchData()).resolves.toBe('peanut butter');
});
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple. Mocha is often compared to Jest because of its flexible and configurable nature, but it requires more setup and configuration for features like mocking and snapshot testing, which Jest provides out of the box.
Jasmine is a behavior-driven development framework for testing JavaScript code. It does not rely on browsers, DOM, or any JavaScript framework. Similar to Jest, it comes with an easy syntax that makes writing tests simple. However, Jest offers more modern features like snapshot testing and built-in support for test coverage.
AVA is a test runner for Node.js with a concise API, detailed error output, and process isolation that lets you develop with confidence. AVA is designed to handle asynchronous testing more efficiently than Jest but lacks some of Jest's features like snapshot testing and built-in mocking.
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any JavaScript testing framework. It's often used alongside other testing frameworks like Mocha or Jest to provide more expressive assertions. Unlike Jest, Chai is purely an assertion library and does not provide test runners or mocking utilities.
🃏 Delightful JavaScript Testing
👩🏻💻 Developer Ready: Complete and ready to set-up JavaScript testing solution. Works out of the box for any React project.
🏃🏽 Instant Feedback: Failed tests run first. Fast interactive mode can switch between running all tests or only test files related to changed files.
📸 Snapshot Testing: Jest can capture snapshots of React trees or other serializable values to simplify UI testing.
Read More: https://jestjs.io/
23.5.0
[jest-cli]
Add package name to NotifyReporter
notification (#5898)[jest-runner]
print stack trace when process.exit
is called from user code (#6714)[jest-each]
introduces %#
option to add index of the test to its title (#6414)[pretty-format]
Support serializing DocumentFragment
(#6705)[jest-validate]
Add recursive
and recursiveBlacklist
options for deep config checks (#6802)[jest-cli]
Check watch plugins for key conflicts (#6697)[jest-snapshot]
Mark snapshots as obsolete when moved to an inline snapshot (#6773)[jest-config]
Fix --coverage
with --findRelatedTests
overwriting collectCoverageFrom
options (#6736)[jest-config]
Update default config for testURL from 'about:blank' to 'http://localhost' to address latest JSDOM security warning. (#6792)[jest-cli]
Fix testMatch
not working with negations (#6648)[jest-cli]
Don't report promises as open handles (#6716)[jest-each]
Add timeout support to parameterised tests (#6660)[jest-cli]
Improve the message when running coverage while there are no files matching global threshold (#6334)[jest-snapshot]
Correctly merge property matchers with the rest of the snapshot in toMatchSnapshot
. (#6528)[jest-snapshot]
Add error messages for invalid property matchers. (#6528)[jest-cli]
Show open handles from inside test files as well (#6263)[jest-haste-map]
Fix a problem where creating folders ending with .js
could cause a crash (#6818)FAQs
Delightful JavaScript Testing.
The npm package jest receives a total of 18,978,395 weekly downloads. As such, jest popularity was classified as popular.
We found that jest demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.