Node.js 协议退出,代码为1
我正在尝试使用Node.js 协议退出,代码为1,node.js,testing,pact,pact-node,Node.js,Testing,Pact,Pact Node,我正在尝试使用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/inte
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测试可以正常运行。我已经将要测试的服务从失败的项目转移到执行这些测试的项目,并且它正在工作(至少启动了pact模拟服务器)。此其他项目中的依赖项与problemathic项目中的依赖项几乎相同:
"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
转到节点命令行),复制/粘贴以下内容:
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的任何版本;遗憾的是,我们不得不用pact节点打包ruby.exe,它使用ruby创建了一个新的模拟服务器实例,因为它是用ruby编写的。有时,确实会发生这样的情况:节点只是“失去了对ruby实例的跟踪”,并且没有正确地关闭它
这是一个我们所熟知的问题,我们正在积极尝试通过将所有核心pact代码整合到一个共享库中来解决这个问题,该库可以跨所有语言和操作系统使用,而无需启动额外的实例。请耐心等待我们解决这些问题:)从退出代码1判断,服务器似乎无法正确启动。这可能是端口
8989
上的端口冲突,因此值得检查
它可能与(长度有限的Windows文件路径)有关
请使用logLevel:'DEBUG'
启用DEBUG
日志记录,并注意它的输出,并共享任何*.log
文件。可能是因为启动服务器时参数的格式不正确
正如您所注意到的,它无法启动底层的Ruby进程,因此我们需要了解到底。最后,在@Matthew Fellows回答并阅读了他的链接之后,我将我的项目移动到了一个较短的路径位置,类似于
D:\myproject\
,然后启动了pact
mock服务器,我的测试通过了。因此,在找到更好的解决方案之前,我将在短路径目录下工作,以避免更多问题
请阅读上一个链接,以获取有关此错误的更多信息,因为有很好的解释。如果是端口冲突,冲突不在端口8989上吗?11108看起来是PID。@J_A_X如Matthew所示,端口是8989,11108是Pact生成的内部PID,在每次执行测试时都是不同的。无论如何,我也试图更改端口号,但仍然存在相同的问题。我已经启动了带有调试日志的测试,并在问题中显示了输出。没有生成日志文件,所以我只有这些信息。我认为这不是端口冲突,因为如果我在其他项目中执行pact测试,它可以正常工作,但在这个项目中,它总是给我那个错误,即使我更改了端口号。是的!这是一个路径长度问题…但我感到困惑,因为我在控制台日志中没有看到任何大于260个字符的路径。无论如何,我在一个较短的位置路径下载了这个项目,服务器启动了。@christiansr85有趣。那你能回答你自己的问题吗?它可能对其他人有用。@matthew fellows这是来自Ruby方面还是节点方面?我想知道它是否与?