How to run multiple or single file or test blocks with examples

This tutorial explains how to run Cypress Single or multiple tests with an example

Cypress is a testing framework to write unit tests in javascript applications.

Cypress to Running all test files in a javascript

Cypress tests files executed using either the npm command or cypress cli command line execution.

  • Using npm command,

First, define scripts in package.json

{
"scripts": {
"test": "cypress run --spec filepath"
}
}

you can use the npm run test or npm test command to execute all tests.

cypress commands works after install it either globally or locally in a project, and configure in a project.ct.

You can also use the cypress cli command to execute.

If cypress is installed globally, use the below command

cypress run --spec src/

To run it locally

./node_modules/.bin/cypress run --spec src/

How do I test a single file using Jest?

Sometimes we want to run a single test file using the jest command

npm test -- --spec src/components/button.spec.js

If the test command is mapped to npm scripts in package.json -- tells to pass the remaining as a command line argument to the npm test script.

Another uses the jest command line. If jest is installed globally, use the below command

jest src/components/button.spec.js

To run it locally

./node_modules/.bin/jest src/components/button.spec.js

For example, the calculator.spec.ts file contains below

describe("calculator", () => {

it("add", () => {
expect(2+3).toBe(5);
});

it("substract", () => {
expect(20-6).toBe(14);
});

});

To run a single test file, use the -i option as given below

jest -i 'calculator`

calculator matched with spec file name ie calculator.spec.js

you can also use --testNamePattern or -toption that matched with test block

Using npm scripts:

{
"scripts": {
"test": "jest -i "calculator.spec.ts"
}
}

And run below command

npm run test