【问题标题】:Github Action with DynamoDB Docker Container使用 DynamoDB Docker 容器的 Github 操作
【发布时间】:2021-04-11 19:16:03
【问题描述】:

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

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

我使用 GitHub Actions 来运行单元测试,但我还想继续使用 GitHub Actions 来进行这些集成测试。我目前的一个无法运行 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

【问题讨论】:

    标签: node.js docker amazon-dynamodb github-actions


    【解决方案1】:

    您可以在工作流程中使用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);
    });
    

    【讨论】:

      猜你喜欢
      • 2021-07-05
      • 1970-01-01
      • 2021-08-17
      • 2023-03-27
      • 2022-01-24
      • 2023-01-27
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      相关资源
      最近更新 更多