skip to Main Content

To confirm whether the tests specified in skaffold.yaml are run in a container or locally, I printed the node version.

>node --version
v18.12.1

I ran the same command in skaffold.yaml.

apiVersion: skaffold/v4beta2
kind: Config
metadata:
  name: microservices-quiz-app
build:
  artifacts:
    - image: auth-img
      context: auth
      docker:
        dockerfile: Dockerfile
      sync:
        manual:
          - src: src/**/*.js
            dest: .
test:
  - image: auth-img
    context: auth
    custom:
      - command: node --version
manifests:
  rawYaml:
  - auth/k8s/*.yml
  - ingress-srv.yml

node version after skaffold dev

The phrasing in the documentation indicates it is run locally "It will run on the local machine". documentation

If I connect directly to the container built using auth-img after skaffold dev, I see a different version.

node version in container

Is there a way to run the tests in a container with the necessary dependencies (e.g. jest, supertest) to run the tests?

Why isn’t Skaffold using the image being tested; are we supposed to not include dev dependencies in the image?

FROM node:18-alpine
# ENV NODE_ENV=production

WORKDIR /app

COPY ["package.json", "package-lock.json*", "./"]

# RUN npm install --production
RUN npm install

COPY . .

# CMD ["npm", "start"]
CMD [ "npm", "run", "dev" ]

2

Answers


  1. Chosen as BEST ANSWER

    We can use docker to run a container using the generated image. Skaffold provides the variable IMAGE.

    test:
      # auth
      - image: auth-img-dev
        context: auth
        custom:
          # run test in container
          - command: echo $IMAGE && docker run -e CI=true $IMAGE npm test
    

  2. Personally, I recommend using multi-stage build. Then you are not locked into a specific tool such as Skaffold.

    FROM node:18-alpine as builder
    
    WORKDIR /app
    
    COPY ["package.json", "package-lock.json*", "./"]
    
    COPY src src  # this includes __tests__ folder
    
    ENV CI=true
    RUN npm ci  # this runs tests
    
    FROM node:18-alpine
    WORKDIR /app
    
    COPY --from=builder ["/app/package.json", "/app/package-lock.json*", "./"]
    
    COPY . .
    
    ENV NODE_ENV=production
    RUN npm ci --omit=dev
    
    CMD ["npm", "run", "start"]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search