Search code examples
githubjwtnestjsgithub-actions

Jest test fails in GitHub Actions but passes locally


There's only one test that's failing with this message 'ERROR [ExceptionsHandler] secretOrPrivateKey must have a value ... at JwtService.sign', which I find weird, because a lot of tests cases use this service too, and locally the test passes correctly.

Here's the log of the CI pipeline in debug mode https://github.com/mateomaza/todo-app-api/actions/runs/7005451263/job/19055682105

I have my .env values in GitHub Secrets, that shouldn't be the issue: Repository Secrets

Here's my .yml file:

name: Backend CI

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Check Out Repository
        uses: actions/checkout@v2

      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 18

        env:
          MONGO_URI: ${{secrets.MONGO_URI}}
          JWT_SECRET_KEY: ${{secrets.JWT_SECRET_KEY}}
          REDIS_HOST: ${{secrets.REDIS_HOST}}
          REDIS_PORT: ${{secrets.REDIS_PORT}}
          REDIS_PASSWORD: ${{secrets.REDIS_PASSWORD}}
          REDIS_TLS: ${{secrets.REDIS_TLS}}
          NODE_ENV: 'test'

      - name: Install Dependencies
        run: npm install

      - name: Run Tests
        run: npm run test

And here's the 'it block' where the issue is happening (just in case):

it('should refresh access token', async () => {
    const mockRefreshToken = 'mock-new-token';
    authService.verifyRefreshToken.mockResolvedValue(mockCreatedUser as User);
    const response = await request(app.getHttpServer())
      .post('/api/auth/refresh')
      .set('Cookie', [`refresh_token=${mockRefreshToken}`])
      .expect(HttpStatus.CREATED);
    expect(response.body).toHaveProperty('access_token');
  });

Solution

  • The error you got is likely complaining about missing JWT_SECRET_KEY in your Run Tests step.

    While you did set env variables at the Setup Node.js step, it is only confined into the scope of that particular step. The Run Tests step did not get the env variables.

    One possible solution is to move your env variables up to the job level:

    jobs:
      build:
        runs-on: ubuntu-latest
    
        # Env is now available for all steps within the 'build' job
        env:
          MONGO_URI: ${{secrets.MONGO_URI}}
          JWT_SECRET_KEY: ${{secrets.JWT_SECRET_KEY}}
          REDIS_HOST: ${{secrets.REDIS_HOST}}
          REDIS_PORT: ${{secrets.REDIS_PORT}}
          REDIS_PASSWORD: ${{secrets.REDIS_PASSWORD}}
          REDIS_TLS: ${{secrets.REDIS_TLS}}
          NODE_ENV: 'test'
    
        steps:
          - name: Check Out Repository
            uses: actions/checkout@v2
    
          - name: Setup Node.js
            uses: actions/setup-node@v2
            with:
              node-version: 18
    
          - name: Install Dependencies
            run: npm install
    
          - name: Run Tests
            run: npm run test