Github Action with DynamoDB Docker Container

Viewed 547

I'm currently writing local integrations tests using Jest, Docker, and the dynamodb-local container.

I do this by starting the container and then jest --watchAll --coverage --runInBand so that the tests run sequentially and don't interrupt one another.

I was using GitHub Actions to run the unit tests, but I'd like to continue to use the GitHub Actions for these integration tests as well. The current one I have can't run NPM. How do I configure the action properly?

# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Node.js Package

on:
  push:
    branches:
      - "main"
# OLD UNIT TESTS that worked
# jobs:
#   build:
#     runs-on: ubuntu-latest
#     steps:
#       - uses: actions/checkout@v2
#       - uses: actions/setup-node@v1
#         with:
#           node-version: 12
#       - run: npm ci
#       - run: npm test
jobs:
  vm:
    runs-on: ubuntu-latest
    steps:
      - run: |
          echo This job does not specify a container.
          echo It runs directly on the virtual machine.
        name: Run on VM
  container:
    runs-on: ubuntu-latest
    container: amazon/dynamodb-local
    steps:
        name: Run in container
      - run: npm ci
      - run: npm test

1 Answers

You can use https://github.com/marketplace/actions/start-dynamodb-in-github-actions in your workflow:

# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [ 14.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
    - run: npm ci
    - run: npm run build --if-present
    - name: Setup DynamoDB Local
      uses: rrainn/dynamodb-action@v2.0.0
      with:
        dbPath: # undefined by default, if this is undefined inMemory will be used
        sharedDb: # undefined by default
        delayTransientStatuses: # undefined by default
        optimizeDbBeforeStartup: # undefined by default
        port: 8000
        cors: '*'
    - run: npm test

and then you need testconfig.json

{
  "accessKeyId": "fake",
  "secretAccessKey": "fake",
  "region":  "eu-central-1",
  "endpoint": "http://localhost:8000"
}

and .jest/setEnvVars.js

const AWS = require('aws-sdk');
// https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-json-file.html
AWS.config.loadFromPath('./testconfig.json');

to have a test like

var AWS = require('aws-sdk');

var dynamodb = new AWS.DynamoDB();

const listTables = () =>  new Promise((resolve, reject) => {
    dynamodb.listTables({} , (err, data) => {
        if(err) reject(err);
        else resolve(data);
    })
});

test('foo bar',  async () => {
    const tables = await listTables();
    console.log(tables);
});
Related