我已经设置了一个简单的端点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路径会得到相同的结果,所以我肯定遗漏了一些东西
1条答案
按热度按时间qzlgjiam1#
根据文档,你在then里面缺少了函数。2请看这里。
字符串
它是这样的
型
我希望这能帮上忙。