【问题标题】:JSDOM - ECONNREFUSED 127.0.0.1:80 when running test in Azure DevOpsJSDOM - 在 Azure DevOps 中运行测试时 ECONNREFUSED 127.0.0.1:80
【发布时间】:2020-11-20 07:27:35
【问题描述】:

当作业在 Azure DevOps yarn test:unit 上运行时,会多次发生以下错误。这不会阻止测试通过。该项目运行vuejest 进行测试。在本地运行时,不会出现错误。

jest.config.js

const Vue = require('vue');
const Vuetify = require('vuetify');

Vue.use(Vuetify);

module.exports = {
  preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel',
  collectCoverage: false,
  collectCoverageFrom: [
    'src/**/*.{js,vue}',
    '!src/registerServiceWorker.js',
    '!src/helpers.js',
    '!src/constants/**',
    '!src/mixins/**',
    '!src/plugins/**',
    '!src/router/**',
    '!src/test/**',
    '!src/main.js', // No need to cover bootstrap file
  ],
  transform: {
    'vee-validate/dist/rules': 'babel-jest',
  },
  transformIgnorePatterns: [
    '<roodDir>/node_modules/(?!vee-validate/dist/rules)',
  ],

  testEnvironmentOptions: {
    // Allow test environment to fire onload event
    // See https://github.com/jsdom/jsdom/issues/1816#issuecomment-355188615
    resources: 'usable',
  },
};

axios.js

let baseURL = '';
switch (process.env.NODE_ENV) {
  case 'development': {
    baseURL = 'https://localhost:44359/api/v1';
    break;
  }
  case 'production': {
    baseURL = 'webapi/api/v1';
    break;
  }
  case 'docker': {
    const port = process.env.VUE_APP_WebAPI_PORT;
    const path = process.env.VUE_APP_WebAPI_Path;
    const version = process.env.VUE_APP_WebAPI_Version;
    const url = new URL(window.location.origin);
    url.port = port;
    baseURL = `${url.origin.toString()}/${path}/${version}`;
    break;
  }
  default: baseURL = 'webapi/api/v1';
}

/* eslint-disable import/prefer-default-export */
export const http = axios.create({
  baseURL,
  withCredentials: true,
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json',
  },
  paramsSerializer: (params) => {
    Object.keys(params).forEach((key) => {
      if (_isArray(params[key]) && !params[key].length) {
        delete params[key];
      }
    });
    return qs.stringify(params, { arrayFormat: 'repeat' });
  },
});

错误

错误:错误:连接 ECONNREFUSED 127.0.0.1:80 在 Object.dispatchError (/home/vsts/work/1/s/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:54:19) 在请求。 (/home/vsts/work/1/s/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:675:20) 在 Request.emit (events.js:327:22) 在 Request.onRequestError (/home/vsts/work/1/s/node_modules/request/request.js:877:8) 在 ClientRequest.emit (events.js:315:20) 在 Socket.socketErrorListener (_http_client.js:426:9) 在 Socket.emit (events.js:315:20) 在 emitErrorNT (internal/streams/destroy.js:92:8) 在 emitErrorAndCloseNT (internal/streams/destroy.js:60:3) 在 processTicksAndRejections (internal/process/task_queues.js:84:21) 未定义 console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29

欢迎任何帮助

【问题讨论】:

  • 你能展示你的管道吗?您如何构建和测试这个项目?
  • 我附加了正在运行的作业。该项目使用yarn:build 命令构建,该命令基本上运行vue-cli-service build,并使用vue-cli-service test:unit 进行测试

标签: vue.js azure-devops jestjs


【解决方案1】:

此错误可能是由于使用了 i18n。我注意到,当我遇到同样的问题并对其进行调试时,它会收到此错误,因为它无法访问本地化文件。您可以使用 moduleNameMapper 设置作为解决方案。

https://jestjs.io/docs/en/configuration#modulenamemapper-objectstring-string--arraystring

【讨论】:

    猜你喜欢
    • 2021-02-24
    • 2020-02-02
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 2023-02-26
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    相关资源
    最近更新 更多