259

I want to toggle only running one test, so I don't have to wait for my other tests to see the result of one test.

Currently, I comment out my other tests, but this is really annoying.

Is there a way to toggle only running one test in Cypress?

kuceb
  • 16,573
  • 7
  • 42
  • 56
  • 4
    Possible duplicate of [In Cypress.io is there anyway to control the test run?](https://stackoverflow.com/questions/51850851/in-cypress-io-is-there-anyway-to-control-the-test-run) – fkoessler Jul 29 '19 at 16:14
  • 1
    On macOS, use a variation of the correct answer below to run one file: `npx cypress run --spec path/to/file.spec.js` – Chris Perry Jun 03 '22 at 23:15

16 Answers16

387

to run only one file

cypress run --spec path/to/file.spec.js

or using glob patterns:

cypress run --spec 'path/to/files/*.spec.js'

Note: you need to wrap your glob patterns in single quotes to avoid shell expansion!

to run only one test in a file

You can use a .only as described in the Cypress docs

it.only('only run this one', () => {
  // similarly use it.skip(...) to skip a test
})

it('not this one', () => {
})

Also, you can do the same with describe and context blocks

edit:

there's also a nice VSCode extension to make adding/removing .only's easier with keyboard shortcuts. It's called Test Utils (install with ext install chrisbreiding.test-utils). It works with js, coffee, and typescript:

enter image description here

kuceb
  • 16,573
  • 7
  • 42
  • 56
  • 12
    It's worth noting that `.only()` can be appended to multiple tests, and only these tests will run. So, you can use this to run more than just one test case. – awesame Jul 28 '21 at 16:16
  • 5
    Horrible practice. I must not need to edit my test files to exclude which tests are run. – Dragas Oct 05 '22 at 12:39
  • nice suggestion, .only() is helpful in reality when u is almost guaranteed to handle multiple tests in one file – Goldie Apr 21 '23 at 05:27
  • @Dragas Can you please elaborate on your comment and explain your suggested approach? You're calling out a perfectly valid way to run a small section of tests to speed up the development process. Why else would the .only function exist and be recommended by the Cypress documentation? https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Excluding-and-Including-Tests – PhillyStafford Apr 24 '23 at 11:42
  • @PhillyStafford Because I then need to edit the test files which goes against the core concept of testing. See maven surefire plugin's `-Dtest` flag which has ability to specify which tests should be run, and can even specify which *single* test to run. This helps to isolate which test modifies the SUT and does not clean up after itself causing other tests to randomly fail. Editing tests is not valid. – Dragas Apr 25 '23 at 15:29
  • 1
    Fair enough, I understand that. But this isn't maven. If you want/need to run only 1 cypress test, you have to add a `.only()` to a test in order to achieve that. Which is technically, editing the test. Are you saying `.only()` is horrible practice or commenting out the tests is horrible practice? Either way, both of these options give you the same result. I agree, commenting out tests isn't ideal but the OP realised that and raised the question. – PhillyStafford Apr 25 '23 at 16:29
62

There are multiple ways of achieving this.

  1. You can add .onlyto it or describe see @bkucera answer
  2. You can do it from the terminal as explained in the doc here
     npx cypress run --record --spec "cypress/integration/my-spec.js"
    
     npm run cypress -- --record --spec "cypress/integration/my-spec.js"
    
Morlo Mbakop
  • 3,518
  • 20
  • 21
  • 4
    Including the CLI example made this answer the most complete and helpful for me – BradGreens Oct 08 '19 at 20:42
  • @Morlo Mbakop, could you help https://stackoverflow.com/questions/71605624/how-to-generate-only-test-coverage-from-a-specific-folder-with-tests-in-cypress – Asking Mar 24 '22 at 18:01
11

You can run the test like this.

cypress run --spec **/file.js

Marko
  • 119
  • 1
  • 2
  • This should have more upvotes. Jest has a really nice CLI where you can do a partial file name and it will find it. This is as close to that as you can get with Cypress. – Ryan E. Jul 20 '22 at 02:17
9

You can mute not needed test suites and particular cases by prepending x to testrunner methods call (describe, it, etc.)

So it would look like:

// this whole testsuite will be muted
xdescribe('Visit google', () => { 
  it('should visit google', () => { cy.visit('https://google.com/'); });
});

// this testsuite will run
describe('Visit youtube', () => {
  it('should visit youtube', () => { cy.visit('https://youtube.com/'); });

  // this testcase will be muted
  xit('is not necessary', () => { ... });
});
Oleksandr Tkalenko
  • 1,098
  • 11
  • 23
9

The best way to do such kind runs are by using the .only keyword that cypress provide.

To run all the test cases in one describe function from many describe functions add the .only in the required describe.

describe("1st describe", () => {
  it("Should check xx", async function(){
  });
  it("Should check yy", async function(){
  });
});   
describe.only("2nd describe", () => {
  it("Should check xx", async function(){
  });
  it("Should check yy", async function(){
  });
}); 
describe("3rd describe", () => {
  it("Should check xx", async function(){
  });
  it("Should check yy", async function(){
  });
}); 

So here only the 2nd describe will run.

Similarly if you want to run some test cases in 1 describe add the .only in front of all the test cases that you want to run.

describe("describe statement", () => {
  it("Should check xx", async function(){
  });
  it.only("Should check yy", async function(){
  });
  it.only("Should check zz", async function(){
  });
});

So here the it for yy and zz will run

This is similar to the fit and fdescribe in karma and jasmine that you might be familiar with.

You can skip the test in cypress with it.skip or xit

Utkarsh Joshi
  • 141
  • 1
  • 3
6

There is one way I have found to skip tests which I don't need to run (in the current test), and that is to use: this.skip();

it('test page', function () {
    // skip this test for now
    this.skip();
    cy.visit('http://example.com/')
    cy.contains('test page').click()
    cy.url()
        .should('include', '/test-page/')
})

1. it is important to use regular function as second argument of it, this will not be available in arrow function
2. Whole of the test will be skipped no matter where we write this.skip()

Akshay Vijay Jain
  • 13,461
  • 8
  • 60
  • 73
Kevdog777
  • 908
  • 7
  • 20
  • 43
2

The best-known solution for that already exists and requires adding just one simple argument in the console. https://github.com/cypress-io/cypress/tree/develop/npm/grep

Simply run: npx cypress run --env grep="TestName" --spec "filename"

Cypress .only() function is used only for development.

  • 1
    note that this currently requires a separate npm package / plugin: https://github.com/cypress-io/cypress/tree/develop/npm/grep – burnettk Nov 16 '22 at 03:37
2

put .only for the test you want to execute and then run the spec as npx cypress run --spec path/to/your-file.spec.js

1

You can use this

cypress run -- --spec 'path/to/files/*.spec.js'

or

npm run --spec 'path/to/files/*.spec.js'

It worked for me.

Many thanks

saurabhshcs
  • 797
  • 5
  • 6
1

My test files have a structure like this path/something.test.jsx and commands npx cypress run --spec path/something.test.jsx gives the following exception in the terminal:

Can't run because no spec files were found.
We searched for any files matching this glob pattern:
...

Surprisingly enough the following works and run the test exactly for one file (providing you have jest installed):

jest path/something.test.jsx
Roman
  • 19,236
  • 15
  • 93
  • 97
  • could you help https://stackoverflow.com/questions/71605624/how-to-generate-only-test-coverage-from-a-specific-folder-with-tests-in-cypress – Asking Mar 24 '22 at 18:01
1
  1. A very easy solution is to prefix your tests in with numbers, as testing frameworks will typically will run tests in alpha/numeric order by default - so if I have to check one spec file - I will copy the contents into a file 0-[file-name].spec and re-run the test command. Once the test completes - you terminate the test run - as you will have the results you were looking for. This answer is targeted at projects where your testing framework is abstracted and as a developer, you do not have all available options for your testing framework. Not the best answer, but it works and is intuitive and super easy to do. I have found this to be a way to avoid adding a bunch of conditional skips() or only() calls that will not make it to production, will have to be removed and you can easily add the file pattern to .gitignore file so these local files do not get checked in.
M4V3R1CK
  • 765
  • 3
  • 9
  • 22
0

You can use specPattern or excludeSpecPattern in cypress.config.ts

Examples:

import { defineConfig } from 'cypress'

export default defineConfig({
  e2e: {
    specPattern:'cypress/e2e/test-to-debug.cy.ts'
  }
})
import { defineConfig } from 'cypress'

export default defineConfig({
  e2e: {
    excludeSpecPattern:'**/not-needed-tests/*.ts'
  }
})

Refer to docs for details

0

You could use

it.only() to execute single testcase

it.skip() to skip single testcase

or you could also try;

describe.only() to run only one suite

describe.skip() to skip the entire suite when you have multiple test suite in single file

newbNox
  • 1,000
  • 2
  • 15
  • 25
-1

use the @focus keyword in the test scripts when execute using cypress open

ThimiraR
  • 167
  • 2
  • 7
-1

To run a single file in cypress, use 1.npx cypress run --record --spec path/to/file.spec.js for eg

npx cypress run --record --spec "cypress/e2e/my-spec.cy.js"
Abhishek
  • 29
  • 2
-2

To run a specific file through Terminal:

 npx cypress run --record --spec "cypress/integration/my-spec.js"

 npm run cypress -- --record --spec "cypress/integration/my-spec.js"
GHULAM NABI
  • 498
  • 5
  • 15