Pact exited with code 1

3.4k views Asked by At

I'm trying to execute some tests with Pact library and I'm getting some errors. Here is the test configuration:

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);
        });
    });
});

This is my package.json file content, with all dependencies I've installed:

{
  "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"
  }
}

And this is the error I get: enter image description here

Basically, right now I don't mind at all if the tests are well or not. The main problem right now is that the pact mock server is not being started.

The weird thing here is that I have other project where the pact tests run properly. I've moved the service I want to test from the project where is failing to the one which executes those tests fine, and it is working (at least the pact mock server is launched). The dependencies in this other project are almost the same as the problemathic project:

"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"
  }

What's going on with this situation?

EDIT: I've launched the pact tests with DEBUG flag and this is the log generated: enter image description here enter image description here

3

There are 3 answers

4
Matthew Fellows On BEST ANSWER

It looks like the server is unable to start up correctly, judging by the exit code of 1. This could be a port conflict on port 8989, so that's worth checking.

It could be related to https://github.com/pact-foundation/pact-js/issues/64 (Windows file paths limited in length).

Could you please enable DEBUG logging with logLevel: 'DEBUG' and note what it outputs, and also share any *.log file. It could be an argument is not being correctly formatted when starting the server.

As you note, it is unable to start the underlying Ruby process so we need to get to the bottom of that.

2
J_A_X On

Looks to me that the server isn't starting up properly, this is normally because the port is already being used. To see if this is the case, in your node command line (in cmd, type node to go to the node command line), copy/paste the following:

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

The 'listen' function takes in the port number, which I took from your screenshot. Look at what node outputs; my guess is it will show something like this:

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)

This means that there is in fact a port conflict. Look at your task manager and make sure there aren't any instances of node running in the background that might be interfering with your tests spinning up a new server. You might also want to check for any versions of the ruby.exe running; sadly, we have to package the ruby.exe with pact-node, and it spins up a new instance of the mock server using ruby, since that's the language it's written in. From time to time, it does happen that node simply "loses track" of the ruby instance and doesn't shut it off properly.

This is a problem that is well known to us and is something we're actively trying to fix by consolidating all core pact code into a single shared library that can be used across all languages and operating systems without the need to spin up an extra instance. Please bear with us as we get some of these kinks out :)

0
christiansr85 On

Finally, after @Matthew Fellows answer and after reading his link to https://github.com/pact-foundation/pact-js/issues/64, I moved my project to a shorter path location, simimlar to D:\myproject\, and then the pact mock server was launched and my tests was passed. So until a better solution is discovered I'll work under that short path directory in order to avoid more problems.

Please, read the previous link in order to get more information about this bug because there is very well explained.