NPM Scripts | Bondar Academy
Course: Cypress UI Testing with JavaScript
Module: Advanced Features
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore npm scripts , their usage, and their importance in simplifying command-line operations for running tests, particularly with Cypress . Running Cypress Tests Previously, tests were run using the terminal command: nbx-cypress-run nbx-e2e-firsttest.cy.js This command executes tests in a specified browser, such as Firefox or Chrome , but can be cumbersome due to its length. Creating Custom NPM Scripts To simplify this process, you can create custom scripts in the package.json file: Locate the scripts section in package.json . Add a new script, for example: "Cypress Smoke Firefox": "nbx-cypress-run nbx-e2e-firsttest.cy.js --browser firefox" Run the script using: npm run "Cypress Smoke Firefox" Combining and Running Scripts You can create multiple scripts and run them sequentially or in parallel: Sequential execution can be done using && : npm run cypress-smoke-firefox && npm run cypress-smoke-chrome For parallel execution on Linux/Mac, use a single & : npm run cypress-smoke-firefox & npm run cypress-smoke-chrome On Windows, install npm run-all to run scripts in parallel: npm run-all cypress-smoke-chrome cypress-smoke-firefox Flexibility of NPM Scripts NPM scripts are versatile and can execute any terminal command, not just Cypress tests. This allows for: API calls using curl Building applications Triggering shell scripts This flexibility is particularly beneficial in CI/CD environments, where a simple script in package.json can streamline complex operations. In summary, leveraging npm scripts enhances efficiency and organization in testing workflows.