113
votes

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?

9

9 Answers

175
votes

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

34
votes

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"
    
4
votes

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', () => { ... });
});
1
votes

You can run the test like this.

cypress run --spec **/file.js

1
votes

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()

0
votes

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
0
votes
  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.
-2
votes

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

-3
votes

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"