Run Cypress tests inside Docker container
Preconditions (3)
✅ Expected Result: Cypress tests run successfully inside the Docker container and test results are visible in the container logs
const { execSync } = require('child_process'); describe('Docker execution of Cypress tests', () => { it('should build Docker image and run Cypress tests successfully', () => { // Build Docker image execSync('docker build -t cypress-test-image .', { stdio: 'inherit' }); // Run Docker container and capture output const output = execSync('docker run --rm cypress-test-image', { encoding: 'utf-8' }); // Check output contains test pass indication expect(output).to.include('All specs passed!'); }); });
This Cypress test script uses Node.js execSync to run Docker commands synchronously.
First, it builds the Docker image named cypress-test-image using the Dockerfile in the current folder.
Then, it runs the container from that image and captures the output logs.
The test asserts that the output contains the text All specs passed! which Cypress prints when tests pass.
This confirms that Cypress tests ran successfully inside the Docker container.
Using execSync with stdio: 'inherit' streams Docker build logs to console for visibility.
Now add data-driven testing by running Cypress tests inside Docker with three different environment variables
docker run -v $(pwd):/e2e -w /e2e cypress/included:12.0.0
ERROR: Cannot find module 'cypress'Pass the test and this pattern moves unknown