From 7bf5ac3ae339f50254defa70beec65991e5804ae Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 21 Sep 2022 16:25:45 -0500 Subject: [PATCH] Add Tell Jest To Focus On Running Only One Test as a JavaScript TIL --- README.md | 3 +- ...-jest-to-focus-on-running-only-one-test.md | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 javascript/tell-jest-to-focus-on-running-only-one-test.md diff --git a/README.md b/README.md index 686aa6f..d002494 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1243 TILs and counting..._ +_1244 TILs and counting..._ --- @@ -451,6 +451,7 @@ _1243 TILs and counting..._ - [Start Node Process In Specific Timezone](javascript/start-node-process-in-specific-timezone.md) - [String Interpolation With Template Literals](javascript/string-interpolation-with-template-literals.md) - [Support Nested Matching In Custom Jest Matchers](javascript/support-nested-matching-in-custom-jest-matchers.md) +- [Tell Jest To Focus On Running Only One Test](javascript/tell-jest-to-focus-on-running-only-one-test.md) - [Tell Prettier To Not Format A Statement](javascript/tell-prettier-to-not-format-a-statement.md) - [Test Coverage Stats With Jest](javascript/test-coverage-stats-with-jest.md) - [Test Timing-Based Code With Jest Fake Timers](javascript/test-timing-based-code-with-jest-fake-timers.md) diff --git a/javascript/tell-jest-to-focus-on-running-only-one-test.md b/javascript/tell-jest-to-focus-on-running-only-one-test.md new file mode 100644 index 0000000..46aee94 --- /dev/null +++ b/javascript/tell-jest-to-focus-on-running-only-one-test.md @@ -0,0 +1,34 @@ +# Tell Jest To Focus On Running Only One Test + +Test output can be noisy. Sometimes one test is inadvertently dependent on +another. These are some of the reasons you may want to tell +[Jest](https://jestjs.io/) to focus in and only run one particular `test` +block. + +You can do this by calling +[`test.only()`](https://jestjs.io/docs/setup-teardown#general-advice) instead +of `test()`. + +Find the test block you are interested in focusing on and update it to look +like this: + +```javascript +// tests above ... + +test.only('ensure the function returns the value', () => { + // ... + // test implementation + // ... +}) + +// tests below ... +``` + +With that 5-character addition (`.only`) we instruct Jest to run that one test +while skipping all others. + +This is particularly useful when you are doing some `console.log` debugging of +a test. You can eliminate any confusion about which test is logging out by only +running one test. + +[source](https://stackoverflow.com/a/42828586/535590)