笑话无法访问的主机:`localhost'此服务在“local-env”区域可能不可用

问题描述

我在 nodejs 和无服务器技术上编写服务。我需要使用集成测试来测试服务。作为测试和运行库,我选择了 Jest。我也指定了 dynamodb 配置来测试它。 当我尝试在本地机器上对 jest 运行测试时,测试成功完成,没有任何错误。但是当测试在 bitbucket 管道中运行时会出现错误

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

这是我的管道:

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"
                },"KeyType": "RANGE"
                }
            ]
        },{
            "TableName": "types","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"
    ]
  }
}

我该如何解决

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)