node.jsmongodbunit-testinggithub-actions

Connect MongoDB (mongoose) to GitHub-Actions


I want to create a CI/CD, so, I'm setting up Unit Tests with Github-Actions. So, I wrote some tests, they're working perfectly fine on my computer, but when I push my code on github and start test (on Github-Actions), my tests seems to not connect to MongoDB. May you help?

This is the output from my computer :

Auth/User Service Unit Tests
    CRUD functionality
      ✔ Should successfully add an user (1045ms)
      ✔ Should successfuly find an user
      ✔ Should successfuly modify an user
      ✔ Should successfuly delete an user (69ms)
    JWT functionalities
      ✔ Should successfully generate a new access token (JWT) and add it to whitelist
      ✔ Should successfully remove an access token (JWT) from whitelist
      ✔ Should successfuly fins an accessToken in whiteList

  Offer Service Unit Test
    CRUD Offer functionalities
      ✔ Should successfuly create an offer (49ms)
      ✔ Should successfuly retrieve an offer
      ✔ Should successfuly modify an offer
      ✔ Success if offer is deleted (And NotFound throw) (58ms)

This is the output from GitHub-Actions :

 Auth/User Service Unit Tests
    CRUD functionalities
      1) Should successfully add an user
      2) Should successfuly find an user
      3) Should successfuly modify an user
      4) Should successfuly delete an user
    JWT functionalities
      5) Should successfully generate a new access token (JWT) and add it to whitelist
      6) Should successfully remove an access token (JWT) from whitelist
      7) Should successfuly fins an accessToken in whiteList

  Offer Service Unit Test
    CRUD Offer functionalities
      8) Should successfuly create an offer
      9) Should successfuly retrieve an offer
      10) Should successfuly modify an offer
      11) Success if offer is deleted (And NotFound throw)


  0 passing (10s)
  11 failing

  1) Auth/User Service Unit Tests
       CRUD functionalities
         Should successfully add an user:
     Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/runner/work/API/API/src/test/auth.test.js)
      at listOnTimeout (internal/timers.js:557:17)
      at processTimers (internal/timers.js:500:7)

And this is my workflow :

name: Node.js CI

on:
  push:
    branches: [ "master", "unit-test" ]
  pull_request:
    branches: [ "master", "unit-test" ]
  

env:
  DB_URL: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  EMAIL: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  EMAIL_PWD: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  TWILIO_SID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  TWILIO_AUTH: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  TWILIO_NUMBER: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


jobs:
  build:

    runs-on: windows-latest

    strategy:
      matrix:
        node-version: [12.x, 14.x, 16.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm ci
    - run: npm install mocha dotenv
    - run: npm test
    - run: npm start

Here, you have no "import" of MongoDB in this code. But, I tried with this :

    - name: Start MongoDB 6.0.7
      uses: supercharge/mongodb-github-action@1.7.0
      with: 
        mongodb-version: 6.0.7

but I had this error :

  Run supercharge/mongodb-github-action@1.7.0
Error: Container action is only supported on Linux

Do you have any solution for me please? :)


Solution

  • I found out about the problem.

    The origin was the fact that my mongodb have a whitelist of IP, so github wasn't able to connect.

    The best way to use github actions in this situation is probably to use Docker.