0
0
Cypresstesting~15 mins

Cypress installation (npm install cypress) - Build an Automation Script

Choose your learning style9 modes available
Verify Cypress installation via npm
Preconditions (2)
Step 1: Open terminal or command prompt
Step 2: Navigate to the project folder or create a new folder
Step 3: Run the command 'npm init -y' to create a package.json file
Step 4: Run the command 'npm install cypress' to install Cypress
Step 5: Wait for the installation to complete
Step 6: Verify that the 'node_modules' folder contains 'cypress'
Step 7: Verify that 'cypress' is listed under dependencies in package.json
✅ Expected Result: Cypress is installed successfully without errors, 'node_modules/cypress' folder exists, and package.json lists Cypress as a dependency
Automation Requirements - Node.js with Mocha and Chai
Assertions Needed:
Verify 'cypress' folder exists inside 'node_modules'
Verify 'cypress' is listed in package.json dependencies
Best Practices:
Use Node.js fs module to check folder existence
Use JSON parsing to read package.json
Use assertions from Chai for clear pass/fail
Keep test isolated and clean up if needed
Automated Solution
Cypress
import { expect } from 'chai';
import fs from 'fs';
import path from 'path';

describe('Cypress installation verification', () => {
  const projectDir = process.cwd();
  const nodeModulesCypressPath = path.join(projectDir, 'node_modules', 'cypress');
  const packageJsonPath = path.join(projectDir, 'package.json');

  it('should have cypress folder inside node_modules', () => {
    const exists = fs.existsSync(nodeModulesCypressPath);
    expect(exists).to.be.true;
  });

  it('should list cypress as a dependency in package.json', () => {
    const packageJsonRaw = fs.readFileSync(packageJsonPath, 'utf-8');
    const packageJson = JSON.parse(packageJsonRaw);
    const dependencies = packageJson.dependencies || {};
    expect(dependencies).to.have.property('cypress');
  });
});

This test suite checks if Cypress is installed correctly after running npm install cypress.

First, it uses Node.js fs.existsSync to check if the node_modules/cypress folder exists, which means Cypress files are present.

Second, it reads package.json and parses it as JSON to verify that Cypress is listed under dependencies. This confirms npm recorded Cypress as a project dependency.

We use chai assertions for clear pass/fail results. The tests run in the current working directory, assuming the commands were run there.

Common Mistakes - 3 Pitfalls
Checking only package.json without verifying node_modules folder
Using asynchronous fs methods without handling promises
Hardcoding absolute paths instead of using path.join and process.cwd()
Bonus Challenge

Now add data-driven testing to verify installation in three different project folders

Show Hint