我正在尝试使用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模拟服务器)。该另一个项目中的依赖关系与问题项目几乎相同:
"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"
}
这种情况是怎么回事?
编辑:我用
pact
标志启动了DEBUG
测试,这是生成的日志:最佳答案
根据退出代码1判断,服务器似乎无法正确启动。这可能是端口8989
上的端口冲突,因此值得检查。
它可能与https://github.com/pact-foundation/pact-js/issues/64(Windows文件路径的长度受限制)有关。
您能否使用DEBUG
启用logLevel: 'DEBUG'
日志记录并记下其输出,并共享任何*.log
文件。启动服务器时,可能是参数格式不正确。
如您所述,它无法启动底层的Ruby进程,因此我们需要深入了解它。
关于node.js - 契约以代码1退出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45103155/