在合约测试中,提供程序测试失败并出现 TypeError: Cannot readproperties of undefined (reading 'logLevel')

问题描述 投票:0回答:1

我设置了一个简单的端点

GET /users/{id}
,它将像这样响应:

{
"email": "[email protected]",
"id": 1,
"name": "John Doe"}

我的

consumer.test.js
是这样写的:

const { Pact, Matchers } = require("@pact-foundation/pact");
const { like } = Matchers;
const axios = require("axios");

const pact = new Pact({
  consumer: "UserConsumer",
  provider: "UserProvider",
  host: "127.0.0.1",
  port: 1234,
});

describe("Pact with UserProvider", () => {
  beforeAll(() => pact.setup());

  afterAll(() => pact.finalize());

  afterEach(() => pact.verify());

  describe("given there is a user", () => {
    beforeEach(() => {
      return pact.addInteraction({
        uponReceiving: "a request for user with ID 1",
        withRequest: {
          method: "GET",
          path: "/users/1",
        },
        willRespondWith: {
          status: 200,
          body: {
            id: like(1),
            name: like("John Doe"),
            email: like("[email protected]"),
          },
        },
      });
    });

    it("returns the user", async () => {
      const response = await axios.get("http://127.0.0.1:1234/users/1");
      expect(response.data).toEqual({
        id: 1,
        name: "John Doe",
        email: "[email protected]",
      });
    });
  });

});

运行它产生了这个协议:

{"consumer": {
"name": "UserConsumer"},"interactions": [{
  "description": "a request for user with ID 1",
  "request": {
    "method": "GET",
    "path": "/users/1"
  },
  "response": {
    "body": {
      "email": "[email protected]",
      "id": 1,
      "name": "John Doe"
    },
    "headers": {
      "Content-Type": "application/json"
    },
    "matchingRules": {
      "$.body.email": {
        "match": "type"
      },
      "$.body.id": {
        "match": "type"
      },
      "$.body.name": {
        "match": "type"
      }
    },
    "status": 200
  }
}],"metadata": {
"pact-js": {
  "version": "12.1.0"
},
"pactRust": {
  "ffi": "0.4.7",
  "models": "1.1.9"
},
"pactSpecification": {
  "version": "2.0.0"
}},"provider": {
"name": "UserProvider"}}

现在我正在尝试使用

provider.test.js
来测试提供程序:

const { Verifier } = require("@pact-foundation/pact");
const path = require("node:path");

describe("Pact Verification", () => {
  it("validates the expectations of UserConsumer", async () => {
    let opts = {
      provider: "UserProvider",
      providerBaseUrl: "http://localhost:8080", // where my provider is running
      pactUrls: [path.resolve(__dirname, "./pacts")], 
    };

    await new Verifier().verifyProvider(opts);
  });
});

消费者测试通过,但提供者测试始终未通过

await new Verifier
线与

TypeError: Cannot read properties of undefined (reading 'logLevel')

我尝试设置调试环境变量没有效果。我还注意到,更改 URL 或契约路径会产生相同的结果,所以我肯定错过了一些东西

javascript jestjs pact
1个回答
0
投票

根据文档,您缺少 then 中的函数。请参阅此处

const { Verifier } = require('@pact-foundation/pact');
const opts = {
  ...
};

new Verifier(opts).verifyProvider().then(function () {
    // do something
});

是这样的

const verifier = new Verifier(opts);
await verifier.verifyProvider().then(function () {
  // Handle the result, e.g., print a message when verification is successful.
  console.log("Pact verification completed successfully.");
});

我希望这有帮助。

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