如何使用打字稿协议

问题描述 投票:3回答:2

我试图用打字稿实现协议节点。 (https://github.com/pact-foundation/pact-node)。我有一些问题和产生的错误消息是不是很描述。这可能是一些我在设置做错了,有很多可在线使用pact.js文档和例子,并且存在一定的差异。下面是我的代码:

   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>

任何人有任何想法,我做错了什么?如果不行的话,没有任何人有他们使用打字稿如何实现协议的例子吗?

谢谢!

javascript node.js typescript testing pact
2个回答
3
投票

有没有你为什么不使用https://github.com/pact-foundation/pact-js理由吗?

条约节点是一个较低的水平库可能不是非常适合你在做什么。条约JS是测试更高水平的DSL为您创建。

书中有一个打字稿例子。

更新:您可能需要增加超时,似乎你的系统时间超过2秒启动模拟服务器,并想逃。

描述第二个错误看起来无关的协议。


0
投票

有时候,我已经看到了改变节点端口还解决超时问题。

© www.soinside.com 2019 - 2024. All rights reserved.