Skip test conditionally with Cypress

February 8th, 2021
| v9.6.0
4 min read

In this blog, I’d like to show you various strategies and tools how you can skip your test and run them conditionally. In the past, I wrote a blogpost on how you can grep your tests using module API. You might like that as well.

.skip and .only

The easiest way to skip or filter a test is to use .only and .skip functions. You can use them with multiple tests in a single spec, so that you’ll run only those test that you want. This code will tun only test #1 and test #3

Copy to clipboard

And this test will only run test #1.

Copy to clipboard

These work whether you use them in GUI or CI. By the way, to avoid accidentally leaving .only in your test, make sure you setup precommit hook that will prevent this, or simply install this nice plugin. You can also use these keywords for your describe blocks. In that case, tests will work like this:

Copy to clipboard

Config file

In your cypress.json you can specify which tests you want to run or skip. You can use the name of your tests or use minimatch to pick which tests should run. Let’s say you have three tests:

Copy to clipboard

To just run test1, you’d put this in your cypress.json file:

Copy to clipboard

If you use subfolders in your integration folder, you can select all tests with the same name inside all of those folder by passing **/test.ts. More importantly, if you use a naming convention for your smoke tests, like test.smoke.ts, you can filter these by passing *.smoke.ts to your testFiles. So for example this configuration will run just our test.smoke.ts file:

Copy to clipboard

All these principles apply to skipping tests as well. For ignoring your test files, use ignoreTestFiles. You can also pass arrays these attributes. These can contain either test name or minimatch, so you can do stuff like:

Copy to clipboard

This configuration will run only our test2.ts file. To get a better understanding of how minimatch works, you can experiment on globster.xyz or on this nice little site.

Test configuration

If you want to add more complex conditions to your tests, you can use spec configuration. For example, you can configure your test to run only in a certain browser.

Copy to clipboard

This test configuration enables you to do much more, but as far as test skipping and filtering goes, this is it. But then again, Cypress tests are all JavaScript. Nothing stops you from writing a condition right inside your spec file:

Copy to clipboard

This test will only run when the set viewport is larger than 350 pixels.

Plugin

There’s a really nice plugin that enables you to skip your tests based on various conditions. It makes it easier to run certain tests only on Mac or only on Windows, as well as skip them using these conditions. It looks something like this:

Copy to clipboard

There are more cool examples on readme page, make sure you check them out. What’s even cooler, you can set any condition you like with this plugin, so our mobile-only example from above would be written like this:

Copy to clipboard

Using CLI

Similarly to our cypress.json configuration, we can pass CLI arguments to run only those tests we want. To run only smoke tests we’ll run:

Copy to clipboard

To run all tests except the smoke ones, we’ll run a command like this:

Copy to clipboard

Your own logic

There are quite a lot options you can use out there. I described one of them in my blog. With Module API, there’s really nothing stopping you from organizing your test suite in various different ways. There’s also an option for grepping all of your tests via plugin. Whichever you choose!

If you liked this blog, you can help me grow it by sharing it on your favourite social network. I write posts like these every week, so if you like them, you can subscribe to my newsletter down below this article.

Let’s keep in touch

From time to time I send some useful tips to your inbox and let you know about upcoming events. Sign up if you want to stay in loop.

is required.

I treat your email address like I would my own. That means no ads. Just notifications of when I do cool stuff. Unsubscribe anytime. Click here to read about how I handle your data