【问题标题】:How to use pact with typescript如何使用带有打字稿的协定
【发布时间】:2018-05-15 20:58:30
【问题描述】:

我正在尝试使用 typescript 实现 pact-node。 (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>

有人知道我做错了什么吗?或者失败了,有没有人有他们如何使用打字稿实现协议的例子?

谢谢!

【问题讨论】:

  • 您能否提供一个我们可以用来重现的可运行示例?上面的错误(X 射线位)与 Pact 无关。

标签: javascript node.js typescript testing pact


【解决方案1】:

你不使用https://github.com/pact-foundation/pact-js有什么原因吗?

Pact Node 是一个较低级别的库,可能不太适合您的工作。 Pact JS 是您创建的用于测试的更高级别的 DSL。

里面有一个 TypeScript 示例。

更新:您可能需要增加超时时间,您的系统似乎需要超过 2 秒才能启动模拟服务器并且正在退出。

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

【讨论】:

  • 谢谢马修。不幸的是,这并没有奏效。我仍然遇到同样的错误。我用更新的代码更新了我的原始问题以供阅读。
  • @user1523236 因为 Pact 是一个单独的二进制文件,所以有时需要一段时间才能启动并准备好。我的测试通常有 15 秒的超时时间,但通常需要大约 2-3 秒。正因为如此,您最好只在 before 函数中为所有测试启动 pact 模拟服务,但在每次测试后清除我们在 afterEach 中的所有交互,以便一切运行得更快。
  • 嗨,伙计们,是的,超时是这个问题。感谢您的回复。
【解决方案2】:

有时我看到更改节点端口也可以解决超时问题。

【讨论】:

  • 欢迎来到 Stack Overflow!请详细说明以使此答案更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 2017-02-19
  • 2021-04-22
  • 1970-01-01
相关资源
最近更新 更多