Github 使用 DynamoDB 的操作 Docker 容器

Github Action with DynamoDB Docker Container

我目前正在使用 Jest、Docker 和 dynamodb-local 容器编写本地集成测试。

我通过启动容器然后 jest --watchAll --coverage --runInBand 来做到这一点,这样测试 运行 顺序进行并且不会相互中断。

我对 运行 单元测试使用 GitHub 操作,但我也想继续对这些集成测试使用 GitHub 操作。我目前拥有的那个不能 运行 NPM。如何正确配置操作?

# 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

您可以在工作流程中使用 https://github.com/marketplace/actions/start-dynamodb-in-github-actions

# 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

然后你需要testconfig.json

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

.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');

一样进行测试
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);
});