Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add cypress into my current docker?

I have got a docker-compose.yml file:

version: '2'

services:
  web:
    build: .
    command: npm run dev
    volumes:
      - .:/usr/app
      - /usr/app/node_modules
    ports:
      - "8080:8080"
    expose:
      - "8080"

And a Dockerfile

FROM node:7.7.2-alpine

WORKDIR /usr/app

COPY package.json .

RUN npm install --quiet

COPY . .

Now I want to add cypress (https://www.cypress.io/) to run test by running:

npm install --save-dev cypress

But maybe it doesn't work because I can't see the cypress folder.

After installing cypress, I run

/node_module/.bin/cypress open

I can't see cypress open.

So now I don't know how to add cypress to my docker to run testing on my host by cypress.

like image 306
Ken Avatar asked Mar 07 '23 18:03

Ken


2 Answers

If you're using docker-compose, the cleaner solution is to just use a separate, dedicated Cypress Docker container, so your docker-compose.yml becomes:

version: '2'
services:
  web:
    build: .
    entrypoint: npm run dev
    volumes:
      - .:/usr/app
      - /usr/app/node_modules
    ports:
      - "8080:8080"
  cypress:
    image: "cypress/included:3.2.0"
    depends_on:
      - web
    environment:
      - CYPRESS_baseUrl=http://web:8080
    working_dir: /e2e
    volumes:
      - ./:/e2e

The e2e directory should contain your cypress.json file and your integration/spec.js file. Your package.json file doesn't have to include Cypress at all because it's baked into the Cypress Docker image (cypress/included).

For more details, I wrote a comprehensive tutorial on using Docker Compose with Cypress:

  • "End-to-End Testing Web Apps: The Painless Way"
like image 106
mtlynch Avatar answered Mar 10 '23 10:03

mtlynch


Running into a similar issue with a similar set up

The way I temporarily fixed it was by manually going into the folder containing my node_modules folder and running node_modules/.bin/install, from there you should be able to open it with node_modules/.bin/open or $(npm bin)/cypress open.

Tried setting up a separate cypress container on my docker-compose as such

cypress:
    build:
        context: .
        dockerfile: docker/cypress
    depends_on:
        - node
    volumes:
        - .:/code

with the dockerfile being Cypress's prebuilt docker-container

Was able to get docker-compose exec cypress node_modules/.bin/cypress verify to work, but when I try to open Cypress it just hangs.

Hope this helps OP, but hope someone can provide a more concrete answer that will help us run Cypress fully through docker

like image 22
Skimchee Avatar answered Mar 10 '23 10:03

Skimchee