Javascript 如何将pact与typescript一起使用
我正在尝试使用typescript实现pact节点。(). 我遇到了一些问题,因此产生的错误消息不是很具有描述性。这可能是我在设置中做错了什么,很多在线文档和示例都使用pact.js,并且存在一些差异。下面是我的代码:Javascript 如何将pact与typescript一起使用,javascript,node.js,typescript,testing,pact,Javascript,Node.js,Typescript,Testing,Pact,我正在尝试使用typescript实现pact节点。(). 我遇到了一些问题,因此产生的错误消息不是很具有描述性。这可能是我在设置中做错了什么,很多在线文档和示例都使用pact.js,并且存在一些差异。下面是我的代码: const path = require('path'); import { Pact } from '../../../node_modules/@pact-foundation/pact'; import { Interaction, Interaction
const path = require('path');
import { Pact } from '../../../node_modules/@pact-foundation/pact';
import { Interaction, InteractionObject } from '../../../node_modules/@pact-foundation/pact';
import { expect } from 'chai';
import { afterEach, before, beforeEach, describe, it, after } from 'mocha';
import { myService } from '../../main/typescript/service/test-service';
describe('My Pact Test', () => {
const port = 5428;
let service: myService;
const provider = new Pact({
port,
log: path.resolve(process.cwd(), 'logs', 'pact.log'),
dir: path.resolve(process.cwd(), 'pacts'),
spec: 2,
consumer: 'MyConsumer',
provider: 'MyProvider',
pactfileWriteMode: 'merge',
});
const EXPECTED_BODY = [{
'auctionStartTime': 1549652248000,
'auctionEndTime': 1549911448000,
'resolveTime': 1539670248000,
'openTimestamp': 1533496996000,
'closeTimestamp': 1547804158000,
'previewStartTime': 1549393048000,
}];
before(() => provider.setup());
after(() => provider.finalize());
afterEach(() => provider.verify());
describe ('should get items ', () => {
console.log ('message 1 ');
before(() => {
console.log ('message 2');
return provider.addInteraction({
state: 'item present in database,
uponReceiving: 'a request for items',
withRequest: {
method: 'GET',
path: 'path_to_my_api_endpoint,
headers: {
Accept: 'application/json',
},
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json',
},
body: EXPECTED_BODY,
},
});
});
it('returns the correct response', (done) => {
console.log ('message 3');
service.getInfo('123', '123')
.then((response: any) => {
expect(response.data).to.eql(EXPECTED_BODY);
done();
});
});
});
})
但是,当我尝试运行此操作时,会出现以下错误:
1) My Pact Test "before all" hook:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
2) My Pact Test "after all" hook:
Error: Failed to get the current sub/segment from the context.
at Object.contextMissingRuntimeError [as contextMissing] (node_modules/aws-xray-sdk-core/lib/context_utils.js:21:15)
at Object.getSegment (node_modules/aws-xray-sdk-core/lib/context_utils.js:92:45)
at Object.resolveSegment (node_modules/aws-xray-sdk-core/lib/context_utils.js:73:19)
at captureOutgoingHTTPs (node_modules/aws-xray-sdk-core/lib/patchers/http_p.js:67:31)
at captureHTTPsRequest (node_modules/aws-xray-sdk-core/lib/patchers/http_p.js:152:12)
at node_modules/popsicle/src/index.ts:126:30
at new WrappedPromise (node_modules/async-listener/es6-wrapped-promise.js:13:18)
at node_modules/popsicle/src/index.ts:112:16
at propagateAslWrapper (node_modules/async-listener/index.js:502:23)
at node_modules/async-listener/glue.js:188:31
at node_modules/async-listener/index.js:539:70
at node_modules/async-listener/glue.js:188:31
at <anonymous>
1)我的Pact测试“所有之前”挂钩:
错误:超过2000毫秒的超时时间。对于异步测试和挂钩,确保调用“done()”;如果返回承诺,请确保它已解决。
2) 我的契约测试“毕竟”挂钩:
错误:无法从上下文中获取当前子/段。
at Object.contextMissingRuntimeError[作为contextMissing](节点_模块/aws X射线sdk核心/lib/context_utils.js:21:15)
在Object.getSegment(node_modules/aws xray sdk core/lib/context_utils.js:92:45)
在Object.resolveSegment(node_modules/aws xray sdk core/lib/context_utils.js:73:19)
在captureOutgoingHTTPs(node_modules/aws X射线sdk core/lib/patchers/http_p.js:67:31)
在captureHTTPsRequest(node_modules/aws xray sdk core/lib/patchers/http_p.js:152:12)
在node_modules/popsicle/src/index.ts:126:30
在新的WrappedPromise(node_modules/async listener/es6 wrapped promise.js:13:18)
在node_modules/popsicle/src/index.ts:112:16
在propagateAslWrapper(节点\模块/async listener/index.js:502:23)
在节点_modules/async listener/glue.js:188:31
在节点_modules/async listener/index.js:539:70
在节点_modules/async listener/glue.js:188:31
在
有人知道我做错了什么吗?否则,有没有人举一个例子来说明他们是如何使用typescript实现pact的
谢谢 您不使用的原因是什么 Pact节点是一个较低级别的库,可能并不适合您所做的工作。Pact JS是您创建的用于测试的更高级别DSL 这里有一个TypeScript示例 更新:您可能需要增加超时时间,您的系统启动模拟服务器的时间似乎超过2秒,并且正在退出
描述的第二个错误与pact无关。有时我看到更改节点端口也可以解决超时问题。谢谢Matthew。不幸的是,这没有起作用。我仍然得到同样的错误。我用更新后的代码更新了原始问题以供阅读。@user1523236因为Pact是一个单独的二进制文件,所以有时需要一段时间来启动并准备就绪。我的测试通常有15秒的超时时间,但通常需要2-3秒。因此,您最好在函数之前的中只为所有测试启动pact模拟服务,但在每次测试之后的中清除所有交互,以便一切运行得更快。嗨,伙计们,是的,超时是个问题。谢谢你的回复。你能提供一个我们可以用来复制的可运行的例子吗?上面的错误(x射线位)与Pact无关。欢迎使用堆栈溢出!请详细说明,以便更好地回答这个问题。