Back to Resources

Blog

Posted September 15, 2023

Three Ways To Run A Single Test In Jest

Running a single test can help you understand the root cause behind a test failure and uncover potential issues within your application. In this post, you'll learn how to how to run a single test using Jest, a popular JavaScript testing framework.

quote

Jest executes all tests in parallel by putting each test file in a different process by default. However, Jest simplifies the task of running single unit tests by providing several methods, ensuring developers can focus on a specific test's behavior without distractions from other tests. Let’s explore the different ways to run a single test with Jest.

Prerequisites For Running A Test

Before running a test in Jest, a few prerequisites must be in place. First, Jest should be installed in your project as a development dependency. Second, the test file should be set up using the '.test.js' or '.spec.js' naming convention. Lastly, any required dependencies and configuration should be configured appropriately to ensure the test environment is accurately set up for execution.

Install Jest by executing the following command:

This code block is meant to be executed on a shell

npm install --save-dev jest 

#or

yarn add --dev jest

3 Different Ways To Run a Single Test in Jest

1. Use 'only' as a suffix or 'f' as a prefix

First, select the file that contains the test you want to run, either by specifying the file name or a pattern to match the file name. Let’s assume the file name is ‘math-integration.test.js’. 

This code block is meant to be executed on a shell

jest math-integration #pattern

jest path/to/math-integration.test.js #file name

Now, inside the file, use ‘.only’ as a suffix or ‘f’ as a prefix to the test you want to run. Let’s say the file contents are:

This code block is written in JavaScript

test('adds 1 + 2 to equal 3', () => {

  expect(sum(1, 2)).toBe(3);

});

test.only('two plus two is four', () => {

  expect(2 + 2).toBe(4);

});

Only the “two plus two is four” test will be executed. The result in the console is:

✓ two plus two is four (1 ms)

○ skipped adds 1 + 2 to equal 3

This also works for 'it.only' and 'describe.only’. Using ‘f’ as a prefix only works for ‘fit’ and ‘fdescribe’.

2. Use 'skip' as a suffix or 'x' as a prefix

This is not a conventional approach, but it is worth mentioning. You can use ‘skip’ to mark all tests that will not be run and only run the one you are interested in. Continuing with the same test file, “adds 1 + 2 to equal 3” is skipped, and “two plus two is four” is the only test executed:

This code block is written in JavaScript

test.skip('adds 1 + 2 to equal 3', () => {

  expect(sum(1, 2)).toBe(3);

});

test('two plus two is four', () => {

  expect(2 + 2).toBe(4);

});

This also works for 'it.skip' and 'describe.skip'. Using 'x' as a prefix works for 'xit', 'xtest', and 'xdescribe'.

3. Using a test name pattern

Execute exclusively those tests whose names align with the defined regular expression (regex). This is a practical approach because it does not require to change the test code to achieve the objective. But there is a caveat: if there is more than one test with the same name, all of those will be executed. That is why it is vital to have unique test names. 

Using the same test file, if we want to execute only the "two plus two is four" test, we need to execute the following command:

This code block is meant to be executed on a shell

jest --testNamePattern="two plus two is four"

# or

jest --testNamePattern="two plus"

# or

jest -t="two plus two is four"

8 Jest Best Practices

Usually, it's not a standard practice to include code that employs the '.only' or '.skip' feature in your version control system. This feature is more suited for debugging, offering a way to focus on specific tests during troubleshooting exclusively. Once the identified issues are resolved, and the tests are functioning correctly, removing the ".only" or ‘.skip’ designation is recommended, ensuring your repository maintains a clean and comprehensive set of tests.

More generally, here is a list of good practices with Jest (most of them apply to testing in general):

  1. Use descriptive test names: Use meaningful and descriptive names for your test cases. This makes it easier to understand their purpose.

  2. Ensure test isolation: Ensure tests are isolated from each other to prevent interference. For example, use tools 'jest.mock()' to isolate dependencies.

  3. Arrange-Act-Assert Pattern: Structure your tests with a clear separation of the Arrange (setup), Act (execution), and Assert (verification) phases.

  4. Use matchers: Leverage Jest's built-in matcher tools like 'toBe' and 'toEqual' for precise assertions.

  5. Use before and after hooks: Utilize ‘beforeEach' and 'afterEach' hooks to set up and clean up test conditions, ensuring test consistency.

  6. Monitor coverage reports: Monitor code coverage using Jest's built-in coverage tools. Aim for comprehensive coverage to identify untested areas.

  7. Use watch mode: Use Jest's watch mode for quick feedback during development. It automatically re-runs relevant tests on code changes.

  8. Strive for continuous improvement: Regularly review and refactor your tests for clarity, efficiency, and coverage.

Conclusion

As a wrap to this Jest guide to leveraging debugging options and efficient testing, these three tricks show how to run a single test. Now, you can delve into specific parts of your codebase and understand why a test fails.

Diego Molina
Staff Software Engineer at Sauce Labs
Published:
Sep 15, 2023
Share this post
Copy Share Link

Need to test right now? Get started free.

Ship code that behaves exactly as it should, faster.

© 2023 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdictions.