Pact 以代码 1 退出

Pact exited with code 1

我正在尝试使用 Pact 库执行一些测试,但遇到了一些错误。这是测试配置:

const path = require('path');
const Pact = require('pact');
const expect = require('expect.js');
const config = require('../../../src/server/config');
const service = require('../../../src/routes/interactions/interactions.service');

describe('@component/interactions tests', () => {
    const url = 'http://localhost';
    const port = 8989;

    const provider = Pact({
        port: port,
        log: path.resolve(process.cwd(), 'test/component/interactions/log/interactions-pact.log'),
        dir: path.resolve(process.cwd(), 'test/component/interactions/pacts'),
        spec: 2,
        consumer: 'cx_issue',
        provider: 'interaction',
        // logLevel: 'WARN'
    });

    config.settingsToExport.INTERACTION_URL = `${url}:${port}`;

    before(done => {
        provider.setup()
            .then(() => {
                done();
            })
            .catch(err => {
                done(err);
            });
    });

    after(done => {
        provider.finalize()
            .then(() => {
                done();
            })
            .catch(err => {
                done(err);
            });
    });

    describe('#createInteraction', () => {
        before(done => {
            const INTERACTION_BODY = {
                contact: 'contact1'
            };
            const TERM = {
                generate: '0dae5b93-9451-4b08-b7bb-f0b944fbcdf2',
                matcher: '^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$'
            };

            const pactInteractionCreate = {
                state: 'Creates a new interaction',
                uponReceiving: 'a new interaction is created successfully',
                withRequest: {
                    method: 'POST',
                    path: '/interactions',
                    body: INTERACTION_BODY
                },
                willRespondWith: {
                    status: 201,
                    body: {
                        id: Pact.Matchers.term(TERM)
                    }
                }
            };

            const promises = [
                provider.addInteraction(pactInteractionCreate)
            ];
            Promise.all(promises)
                .then(() => {
                    done();
                });
        });

        it('/api/interactions POST', done => {

            const interaction = {
                contact: 'The xx'
            };

            service.createInteraction(interaction)
                .then(response => {
                    expect(response.id).to.be.equal(TERM.generate);
                    done();
                })
                .catch(done);
        });
    });
});

这是我的 package.json 文件内容,包括我安装的所有依赖项:

{
  "name": "issueAPI",
  "version": "1.0.0",
  "private": true,
  "main": "./src/index.js",
  "scripts": {
    "dev": "nodemon -e  js ./src/index.js",
    "start": "node ./src/index.js",
    "linter": "node ./node_modules/eslint/bin/eslint.js ./src",
    "test": "mocha test",
    "test-component": "mocha test/component",
    "install-test-build": "npm install && npm test && npm run linter",
    "test-build": "npm test && npm run linter"
  },
  "jshintConfig": {
    "esversion": 6
  },
  "dependencies": {
    "ajv": "^4.11.3",
    "body-parser": "^1.17.2",
    "express": "^4.15.3",
    "express-winston": "^2.4.0",
    "request": "^2.81.0",
    "winston": "^2.3.1",
    "yamljs": "^0.2.9"
  },
  "devDependencies": {
    "@pact-foundation/pact-node": "^4.8.3",
    "dotenv": "^4.0.0",
    "eslint": "^4.2.0",
    "eslint-config-node": "^1.6.0",
    "expect.js": "^0.3.1",
    "mocha": "^3.2.0",
    "nodemon": "^1.11.0",
    "pact": "^2.3.3",
    "sinon": "^2.3.8",
    "supertest": "^3.0.0"
  }
}

这是我得到的错误:

基本上,现在我完全不介意测试好不好。现在的主要问题是 pact 模拟服务器没有启动。

奇怪的是,我还有其他项目可以正确测试 运行。我已经将我想要测试的服务从失败的项目转移到可以正常执行这些测试的项目,并且它正在运行(至少 pact 模拟服务器已启动)。这个其他项目中的依赖项与有问题的项目几乎相同:

"dependencies": {
    "ajv": "^4.11.3",
    "body-parser": "^1.16.1",
    "dotenv": "^4.0.0",
    "express": "^4.14.0",
    "jsonwebtoken": "^7.4.1",
    "jwt-simple": "^0.5.1",
    "morgan": "^1.8.1",
    "mustache-express": "^1.2.4",
    "node-env-file": "^0.1.8",
    "request": "^2.79.0",
    "when": "^3.7.8"
  },
  "devDependencies": {
    "@pact-foundation/pact-node": "^4.8.3",
    "eslint": "^3.17.1",
    "eslint-config-node": "^1.6.0",
    "expect.js": "^0.3.1",
    "mocha": "^3.2.0",
    "nodemon": "^1.11.0",
    "pact": "^2.3.3",
    "sinon": "^1.17.7",
    "supertest": "^3.0.0",
    "nock": "^9.0.13"
  }

这是怎么回事?

编辑: 我已经用 DEBUG 标志启动了 pact 测试,这是生成的日志:

在我看来,服务器没有正常启动,这通常是因为端口已被使用。要查看是否是这种情况,请在您的节点命令行中(在 cmd 中,键入 node 以转到节点命令行),copy/paste 以下内容:

require('http').createServer(function(){}).listen(11108);

'listen' 函数接受端口号,这是我从你的截图中获取的。查看节点输出什么;我猜它会显示如下内容:

Error: listen EADDRINUSE :::11108
    at Object.exports._errnoException (util.js:1018:11)
    at exports._exceptionWithHostPort (util.js:1041:20)
    at Server._listen2 (net.js:1258:14)
    at listen (net.js:1294:10)
    at Server.listen (net.js:1390:5)
    at repl:1:44
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:73:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:340:29)

这意味着实际上存在端口冲突。查看您的任务管理器并确保后台没有任何节点 运行 实例可能会干扰您启动新服务器的测试。您可能还想检查 ruby.exe 运行 的任何版本;遗憾的是,我们必须将 ruby.exe 与 pact-node 打包在一起,并且它使用 ruby 启动模拟服务器的新实例,因为这是它编写的语言。有时,它确实ruby 实例的那个节点只是 "loses track" 并且没有正确关闭它。

这是一个众所周知的问题,我们正在积极尝试通过将所有核心契约代码整合到一个共享库中来解决这个问题,该共享库可以跨所有语言和操作系统使用,而无需启动一个额外的实例。请耐心等待我们解决其中的一些问题:)

从退出代码 1 判断,服务器似乎无法正常启动。这可能是端口 8989 上的端口冲突,因此值得检查。

可能与 https://github.com/pact-foundation/pact-js/issues/64(Windows 文件路径长度受限)有关。

能否请您使用 logLevel: 'DEBUG' 启用 DEBUG 日志记录并记下它输出的内容,并共享任何 *.log 文件。可能是启动服务器时参数格式不正确。

如您所见,它无法启动基础 Ruby 进程,因此我们需要查明原因。

最后,在@Matthew Fellows 回答并阅读他的 link 到 https://github.com/pact-foundation/pact-js/issues/64 之后,我将我的项目移动到一个较短的路径位置,类似于 D:\myproject\,然后 pact 模拟服务器已启动,我的测试已通过。因此,在发现更好的解决方案之前,我将在该短路径目录下工作,以避免出现更多问题。

请阅读之前的 link 以获取有关此错误的更多信息,因为其中解释得非常好。