Jest.js 在合约测试中,提供程序测试失败,出现TypeError:Cannot read properties of undefined(阅读“logLevel”)

w6lpcovy  于 2023-11-15  发布在  Jest
关注(0)|答案(1)|浏览(265)

我已经设置了一个简单的端点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或pact路径会得到相同的结果,所以我肯定遗漏了一些东西

qzlgjiam

qzlgjiam1#

根据文档,你在then里面缺少了函数。2请看这里。

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.");
});


我希望这能帮上忙。

相关问题