user2297996
user2297996

Reputation: 1564

How to run a single test suite in Jest?

I have many test suites. I wanna to run a singe one and skip all the others, and I would like to do this on the code level.

I know I can do this using .only() and .skip() in a test file, but that supports only the tests / describes defined in that file.

Is there a way to do this globally? Like is there something like .only() which - when called on the top level describe - runs only that test suite and all others are skipped? Or: when called on a single test ( it().only() ), then only that test runs and nothing else?

I see nothing like this in the API, but maybe Jest can be configured to work this way?

Is this possible with Jest or is this something I can only do via CLI?

Upvotes: 1

Views: 4350

Answers (2)

We'll See
We'll See

Reputation: 510

I think if you adapt this answer: https://stackoverflow.com/a/59487370/14553660 it should give you what you need.

For example:

testsuite.test.js

import { signuptests } from './signup.test.js'
import { logintests } from './login.test.js'
import { logouttests } from './logout.test.js'

describe('Signup', signuptests)
describe.only('Login', logintests)
describe('Logout', logouttests)

signup.test.js

export const signuptests = () => {
     it('Should have login elements', () => {});
     it('Should Signup', () => {}});
}

login.test.js

export const logintests = () => {
    it.only('Should Login', () => {}});
    it('etc',()=>{});
}

logout.test.js

export const logouttests = () => {
    it('Should Logout', () => {}});
    it('etc',()=>{});
}

You will be able to use .only at the top level - in the testsuite file - to determine which test files are run, and you can also use .only within each individual test file (e.g. within login.test.js) to only run one particular test from that file.

I don't know that there is a limit to nesting describe blocks, so I imagine you could even make a 'master-testsuite' that imports different testsuites (which in turn import different test files...etc)

Upvotes: 0

Wesley Williams
Wesley Williams

Reputation: 326

If I understand correctly: You want to run just one test suite/file.

You can do this from the command line with jest path/to/filename.test.js.

Within a file, you can use test.only(name, fn, timeout) to only run that test. This won't stop Jest from moving on to the next testing file though.

Full Jest CLI docs

As far as I am aware, you cannot do this from within the test file itself.
The closest I can think of would be to set the `testmatch' in Jest's config to a pattern that only matches the file(s) you want run.

package.json

{
  "name": "my-project",
  "jest": {
    "testmatch": "**/my.test.js"
  }
}

Upvotes: 1

Related Questions