21

I have a file called "test-file-1", within it i have a few describes (test suites) with unique names and inside them i have tests which may have similar names across test suites.

To run a single test suite or test i type the command below.

npm test -- -t "Test Suite or Test"

I'm trying to figure out how to run a single test of a specific test suite.

Gabriel Souza
  • 965
  • 5
  • 16
  • 36

7 Answers7

21

Give the path of the file you want to test :

npm test path/of/your/testfile.js -t "test name"

will run only the test (or group) named "test name" of this specific file.

colinux
  • 3,989
  • 2
  • 20
  • 19
  • that's the problem, i have a file with multiple tests with the same or similar name, the only thing that differs them is the test suite. – Gabriel Souza Jul 16 '19 at 11:59
  • 1
    easily get the path of the file in VSC. Right click the file and select "Copy Relative Path". – Fer Toasted Aug 17 '22 at 21:16
14

After trying a lot of different things i found out that it was simpler than i thought.

You simply have to put the test after the test suite in the same string:

npm test -- -t "<Test Suite> <Test>"
Gabriel Souza
  • 965
  • 5
  • 16
  • 36
4
import LoggerService from '../LoggerService ';

describe('Method called****', () => {
  it('00000000', () => {
    const logEvent = jest.spyOn(LoggerService , 'logEvent');
    expect(logEvent).toBeDefined();
  });
});

run below command npm test -- tests/LoggerService.test.ts -t '00000000'

Swift
  • 829
  • 2
  • 12
  • 33
1

Point to the test suite in jest config file via "testMatch":["**/path/testSuite/*.js"]

and now execute by giving unique testname by jest testName

Here you have to update the testMatch every time which is your testSuite

Chandra Shekhar
  • 664
  • 2
  • 9
  • 24
  • i'm looking for a simpler way, manually changing every time is not practical. – Gabriel Souza Jul 16 '19 at 20:39
  • your testsuite is dynamic and testcase is static so you have to change the testsuite all the time then why dont you run using `jest -t /path/to/testSuite/testcase.js` . Everytime you have to only pass testsuite here and remove testMatch from config file – Chandra Shekhar Jul 17 '19 at 05:18
1

If you are using a jest config file, you can use the testNamePattern key to configure this. So your config file could look something like

jest-e2e.json

{
  "moduleFileExtensions": ["js", "json", "ts"],
  "rootDir": ".",
  "testEnvironment": "node",
  "testRegex": ".spec.ts$",
  "testNamePattern": "login",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  },
  "bail": true,
  "verbose": true,
  "testTimeout": 30000
}


This will only run test that has "login" in one of the test pattern

Reference: https://jestjs.io/docs/cli#--testnamepatternregex

velociraptor11
  • 576
  • 1
  • 5
  • 10
0

For me, it only works with:

npm run test -- tests/01_login.js --testcase "Should login into Dashboard"

npm run <script> -- <test suite path> --testcase "<test case>"

my script in package.json:

"test": "env-cmd -f ./.env nightwatch --retries 2 --env selenium.chrome",

at nightwatch version 1.3.4

Daniel M.
  • 49
  • 1
  • 5
-1

in latest versions of jest you could use it.only(), describe.only(), etc in which case it will just execute that single test without having to pass a file or filter parameter

cancerbero
  • 6,799
  • 1
  • 32
  • 24