npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sbnc/jest-skip

v29.0.0

Published

Jest environment and test utilities to conditionally skip tests.

Downloads

5

Readme

Jest Skip

This package extends Jest's functionality by allowing dynamic skipping of tests during test execution.

Overview

There are instances when we may need to skip a test based on certain conditions. For example, if the environment isn't properly set up or if other tests have failed. While Jest doesn't support this feature natively, this utility provides the functionality.

This utility addresses the issue by:

  • Including a custom test environment that marks tests as skipped when necessary.
  • Providing skip() and skipIf() functions that can be used to skip tests.

Disclaimer

The jest-circus documentation explicitly states that mutating the state data by the environment is unsupported. This utility does exactly that, so it may not work in some cases or could break in the future.

Use at your own risk!

In practice, Jest seems to handle such mutations well, especially those performed by this utility. Therefore, it should work for you too. However, if something doesn't work, feel free to raise an issue!

Usage

Installation

Install using npm or your preferred package manager, e.g.:

npm install --save-dev @sbnc/jest-skip

Configuration

Prerequisite

Ensure you understand what a Jest Environment is and how to use it.

As this utility replaces the Jest Environment, you need to configure it in your Jest configuration file or in your test files.

Since we're replacing the Jest Environment, it limits your ability to freely choose your Environment. This utility supports the most common environments:

  • node: @sbnc/jest-skip/node
  • jsdom: @sbnc/jest-skip/jsdom

If you want to use it with a different environment, such as @happy-dom/jest-environment, please raise an issue or a pull request.

CLI

Specify the environment as a CLI argument for the Jest command:

jest --env=@sbnc/jest-skip/node

In package.json

To configure for all tests, add the following to your package.json file:

{
    "jest": {
        "testEnvironment": "@sbnc/jest-skip/node"
    }
}

Jest configuration file

Add the following to your jest.config.js file to configure for all tests:

module.exports = {
  // ...
  testEnvironment: '@sbnc/jest-skip/node',
  // ...
}

Or if you are using json:

{
  "testEnvironment": "@happy-dom/jest-environment"
}

Test file

To use it in certain test files only, add the following to your test file:

/**
 * @jest-environment @sbnc/jest-skip/node
 */

Skipping tests

skip()

This method unconditionally skips a test. It can be used on its own or within custom logic.

import { skip } from '@sbnc/jest-skip';

test('skipped test', () => {
  skip();
  expect(true).toBe(false); // This will not be executed, the test will not raise an error.
});

skipIf()

This method skips a test if the condition is met. It can be used on its own or within custom logic.

import { skipIf } from '@sbnc/jest-skip';

test('test only when someCondition is not true', () => {
  skipIf(someCondition);
  expect(true).toBe(false); // This will be executed and the test will not raise an error if someCondition is falsey.
});

CJS vs ESM support

The environments are packaged in only CJS format, as Jest is still a CJS application. Currently, there are no use-cases where the environment should be in ESM format.

The utilities are shipped in both CJS and ESM formats. So, besides the examples above,

const { skip, skipIf } = require('@sbnc/jest-skip');

is also valid.


Bence Szalai - https://sbnc.eu/