Three Ways To Run A Single Test In Jest
Sauce AI for Test Authoring: Move from purpose to execution in second.|xBack to ResourcesBlogPosted
Sauce AI for Test Authoring: Move from purpose to execution in second.
|
x
Blog
Three Ways To Run A Single Test In Jest
Running a single test can help you understand the root grounds behind a tryout failure and uncover potential issue within your application. In this berth, you & # x27; ll learn how to how to run a individual test using Jest, a democratic JavaScript testing framework.
executes all tests in by putting each test file in a different process by default. However, Jest simplifies the task of running single unit tests by render various methods, ensuring developers can focalize on a specific exam & # x27; s behavior without distraction from other trial. Let ’ s explore the different ways to run a single test with Jest.
Prerequisites For Running A Test
Before lam a test in Jest, a few requirement must be in place. First, Jest should be installed in your undertaking as a development dependency. Second, the test file should be set up using the & # x27; .test.js & # x27; or & # x27; .spec.js & # x27; naming convention. Lastly, any mandatory dependencies and configuration should be configured fittingly 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
recital add -- dev jest
3 Different Ways To Run a Single Test in Jest
1. Use & # x27; solely & # x27; as a suffix or & # x27; f & # x27; 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 codification cube is imply to be fulfill 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 examination you want to run. Let ’ s say the file content are:
This code block is written in JavaScript
examination (& # x27; adds 1 + 2 to equal 3 & # x27;, () = & gt; {
ask (sum (1, 2)) .toBe (3);
});
test.only(& # x27; two plus two is four & # x27;, () = & gt; {
expect (2 + 2) .toBe (4);
});
Only the “ two plus two is four ” test will be executed. The event in the console is:
✓ two plus two is four (1 ms)
○ jump adds 1 + 2 to equal 3
This also works for & # x27; it.only & # x27; and & # x27; describe.only ’. Using ‘ f ’ as a prefix only work for ‘ fit ’ and ‘ fdescribe ’.
2. Use & # x27; skip & # x27; as a suffix or & # x27; x & # x27; as a prefix
This is not a conventional approach, but it is deserving mentioning. You can use ‘ omission ’ to mark all tryout that will not be run and only run the one you are interested in. Continuing with the same trial file, “ adds 1 + 2 to match 3 ” is hop, and “ two plus two is four ” is the lone trial executed:
Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.
This code block is written in JavaScript
test.skip(& # x27; adds 1 + 2 to equalise 3 & # x27;, () = & gt; {
expect (sum (1, 2)) .toBe (3);
});
test (& # x27; two plus two is four & # x27;, () = & gt; {
expect (2 + 2) .toBe (4);
});
This also work for & # x27; it.skip & # x27; and & # x27; describe.skip & # x27;. Using & # x27; x & # x27; as a prefix act for & # x27; xit & # x27;, & # x27; xtest & # x27;, and & # x27; xdescribe & # x27;.
3. Using a tryout name form
Execute exclusively those test whose name align with the outlined regular reflection (regex). This is a practical approach because it perform not ask to modify 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 unparalleled test names.
Using the same examination file, if we want to execute only the & quot; two plus two is four & quot; examination, we necessitate to execute the next command:
This codification block is meant to be executed on a cuticle
jest -- testNamePattern= & quot; two plus two is four & quot;
# or
jest -- testNamePattern= & quot; two plus & quot;
# or
jest -t= & quot; two plus two is four & quot;
8 Jest Best Practices
Usually, it & # x27; s not a standard practice to include codification that employ the & # x27; .only & # x27; or & # x27; .skip & # x27; feature in your version control scheme. This feature is more suitable for debug, volunteer a way to focus on specific tests during troubleshooting exclusively. Once the identified issues are resolved, and the tests are work correctly, removing the & quot; .only & quot; or ‘ .skip ’ designation is recommended, ensuring your deposit sustain a light and comprehensive set of tests.
More mostly, hither is a list of full pattern with Jest (most of them use to testing in general):
Use descriptive test name:Use meaningful and descriptive names for your test cases. This makes it easier to understand their purpose.
Ensure test isolation:Ensure tests are insulate from each other to forbid interference. For example, use tools & # x27; jest.mock () & # x27; to isolate dependence.
Arrange-Act-Assert Pattern:Structure your tests with a clear breakup of the Arrange (setup), Act (execution), and Assert (verification) phases.
Use matchers: Leverage Jest & # x27; s built-in matcher tools like & # x27; toBe & # x27; and & # x27; toEqual & # x27; for precise assertions.
Use before and after hooks:Utilize ‘ beforeEach & # x27; and & # x27; afterEach & # x27; hook to set up and pick up examination weather, ensuring test consistency.
Monitor reporting reports:Monitor code coverage using Jest & # x27; s built-in coverage tools. Aim for comprehensive coverage to place unseasoned areas.
Use watch way:Use Jest & # x27; s watch mode for nimble feedback during growth. It automatically re-runs relevant tests on code changes.
Strive for uninterrupted improvement:Regularly reappraisal and refactor your trial for lucidness, efficiency, and coverage.
Conclusion
As a wrap to this Jest guide to leverage debugging options and efficient examination, these three tricks shew how to run a individual test. Now, you can dig into specific parts of your codebase and understand why a test betray.
Related resources
Staff Software Engineer at Sauce Labs
Share this post
Need to test flop now? Get start free.
Ship codification that behaves incisively as it should, faster.
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free


