2

I write service on nodejs and serverless technology. I need to test the service with integration tests. As testing and running library I have chosen Jest. I specified dynamodb configuration to test it as well. When I try to run tests on jest in my local machine, tests completed successfully without any error. But when tests run in bitbucket pipelines it comes with an error:

UnknownEndpoint: Jest: Got error running globalSetup - /opt/atlassian/pipelines/agent/build/node_modules/@shelf/jest-dynamodb/setup.js, reason: Inaccessible host: `localhost'. This service may not be available in the `local-env' region. at Request.ENOTFOUND_ERROR (/opt/atlassian/pipelines/agent/build/node_modules/aws-sdk/lib/event_listeners.js:530:46) at Request.callListeners (/opt/atlassian/pipelines/agent/build/node_modules/aws-sdk/lib/sequential_executor.js:106:20) at Request.emit (/opt/atlassian/pipelines/agent/build/node_modules/aws-sdk/lib/sequential_executor.js:78:10) at Request.emit (/opt/atlassian/pipelines/agent/build/node_modules/aws-sdk/lib/request.js:688:14) at error (/opt/atlassian/pipelines/agent/build/node_modules/aws-sdk/lib/event_listeners.js:362:22) at ClientRequest.<anonymous> (/opt/atlassian/pipelines/agent/build/node_modules/aws-sdk/lib/http/node.js:99:9) at ClientRequest.emit (events.js:376:20) at ClientRequest.emit (domain.js:470:12) at Socket.socketErrorListener (_http_client.js:475:9) at Socket.emit (events.js:376:20) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! notification-service@1.0.0 test: `jest --config ./jest.config.json --runInBand` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the notification-service@1.0.0 test script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /root/.npm/_logs/2021-06-21T16_19_24_134Z-debug.log

This is my pipelines:

pipelines:
  default:
    - step:
        name: npm install and test
        script:
          - apk add openjdk8
          - npm install
          - sls dynamodb install
          - npm run test
        services:
          - docker
        caches:
          - node
definitions:
  services:
    docker:
      memory: 1024

jest.config.json:

{
  "testRegex": "((\\.|/*.)(spec))\\.js?$",
  "preset": "@shelf/jest-dynamodb",
  "setupFiles": [
    "./jest.init.js"
  ]
}

jest.init.js:

import 'babel-polyfill';

jest.dynamodb-config.js:

module.exports = {
    port: "8001",
    tables: [
        {
            "TableName": "templates",
            "BillingMode": "PAY_PER_REQUEST",
            "AttributeDefinitions": [
                {
                    "AttributeName": "id",
                    "AttributeType": "S"
                },
                {
                    "AttributeName": "created_at",
                    "AttributeType": "S"
                }
            ],
            "KeySchema": [
                {
                    "AttributeName": "id",
                    "KeyType": "HASH"
                },
                {
                    "AttributeName": "created_at",
                    "KeyType": "RANGE"
                }
            ]
        },
        {
            "TableName": "types",
            "BillingMode": "PAY_PER_REQUEST",
            "AttributeDefinitions": [
                {
                    "AttributeName": "id",
                    "AttributeType": "S"
                },
                {
                    "AttributeName": "created_at",
                    "AttributeType": "S"
                }
            ],
            "KeySchema": [
                {
                    "AttributeName": "id",
                    "KeyType": "HASH"
                },
                {
                    "AttributeName": "created_at",
                    "KeyType": "RANGE"
                }
            ]
        }
    ],
};

package.json:

{
  "name": "notification-service",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest --config ./jest.config.json --runInBand",
    "test:watch": "npm run test -- --watch",
    "doc": "jsdoc -r src/ -d doc -p --readme readme.md",
    "deploy_locally": "serverless offline --stage test",
    "deploy_party": "serverless deploy --stage party --region ${region:-eu-central-1} --aws-profile ${profile:-sandbox}",
    "deploy_cloud": "serverless deploy --stage cloud --region ${region:-eu-central-1} --aws-profile ${profile:-cloud}",
    "start": "sls offline start --stage test"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/preset-env": "^7.14.5",
    "@jest/globals": "^27.0.3",
    "@shelf/jest-dynamodb": "^1.8.1",
    "aws-sdk": "^2.930.0",
    "aws-sdk-mock": "^5.2.1",
    "aws-testing-library": "^1.1.1",
    "axios": "^0.21.1",
    "babel-jest": "^27.0.2",
    "babel-polyfill": "^6.26.0",
    "jest": "^27.0.4",
    "jsdoc": "^3.6.7",
    "serverless": "^2.46.0",
    "serverless-dotenv-plugin": "^3.9.0",
    "serverless-dynamodb-local": "^0.2.39",
    "serverless-offline": "^7.0.0",
    "serverless-offline-sqs": "^4.1.1",
    "serverless-pseudo-parameters": "^2.5.0",
    "serverless-webpack": "^5.5.1",
    "webpack": "^5.39.0"
  },
  "dependencies": {
    "messente_api": "^1.4.0",
    "moment": "^2.29.1",
    "uuid": "^8.3.2"
  },
  "jest": {
    "transform": {
      "^.+\\.[t|j]sx?$": "babel-jest"
    },
    "setupFilesAfterEnv": [
      "./node_modules/aws-testing-library/lib/jest/index.js"
    ]
  }
}

How can I fix it?

  • I guess 'local-env' is not a valid AWS region name. It seems your local machine has default region configuration but the pipeline doesn't. Could you check whether there is a file ~/.aws/config on Linux, macOS, or Unix (for Windows, it is C:\Users\USERNAME\.aws\config file.) and it contains the default region information? – jungyh0218 Jun 22 '21 at 01:51
  • Thanks for the answer, yes it contains. – Vahagn Kostandyan Jun 22 '21 at 13:38
  • But I tried to remove the default region and the result was the same, in the local machine my tests end successfully. – Vahagn Kostandyan Jun 22 '21 at 13:46
  • Seems like your jest test code connects to local dynamodb container successfully in your local machine, but it fails in cloud environment because the jest process tries to connect to the localhost in the container's point of view. https://stackoverflow.com/questions/66062812/unknownendpoint-inaccessible-host-localhost-when-trying-to-connect-to-local Maybe this answer could help you. – jungyh0218 Jun 23 '21 at 08:35
  • Try adding `credentials: {accessKeyId: 'fakeMyKeyId', secretAccessKey: 'fakeSecretAccessKey'}` to the DDB constructor – Vlad Holubiev Aug 18 '21 at 15:15

0 Answers0