Home > database >  Pact verification failed! with console error Missing requests: GET /users/login
Pact verification failed! with console error Missing requests: GET /users/login

Time:06-18

am very new to Pact-js and contract testing I'll try my best to explain my issue. for now, I am only trying to generate the consumer contract

here is my pact provider:

export const provider = new Pact({
    consumer: 'Users',
    provider: 'UsersService',
    port: 1234,
    log: path.resolve(process.cwd(), 'logs', 'pact.log'),
    pactfileWriteMode: 'overwrite',
    logLevel: "DEBUG",
    dir: path.resolve(process.cwd(), 'pacts'),
});

and here is my test:

jest.mock("axios");
const EXPECTED_BODY = {...}

describe("Pact tests axios", () => {
    describe("/GET login", () => {
        beforeAll(() => provider.setup());
        afterEach(()=> provider.verify())
        afterAll(() => provider.finalize());
        it("should login user and response with user object", async () => {
            await provider.addInteraction({
                state: 'user logged', uponReceiving: 'request logged user', withRequest: {
                    method: 'GET',
                    path: '/users/login',
                    body: {username: "test", password: "11223344"},
                }, willRespondWith: {
                    status: 200, headers: {
                        'Content-Type': 'application/json',
                    }, body: eachLike(EXPECTED_BODY),
                },
            });
            axios.get.mockResolvedValueOnce(EXPECTED_BODY);
            const loggedUser = await loginUser("test", "11223344")
            expect(axios.get).toHaveBeenCalledTimes(1)
            expect(axios.get).toHaveBeenLastCalledWith("http://localhost:8080/users/login", {"headers": {"Content-Type": "application/json"}, "params": {"password": "11223344", "username": "test"}})
            expect(loggedUser).toEqual(EXPECTED_BODY)
        })
    });
})

I should say that my original request takes two parameters username and password and returns an object containing all that user's information of course the user exists if not it returns null

here is the API call function if needed:

export default async function loginUser(username, password) {
    try{
        return await axios.get(("http://localhost:8080/users/login"), {
            headers: {
                "Content-Type": "application/json"
            },
            params: {
                username: username,
                password: password
            }
        })
    }catch (e){
        return null
    }
}

CodePudding user response:

Pact expects you to actually make the call to the endpoint you're mocking in Pact.

Missing requests: GET /users/login

This error says "you said you'd make a GET call to /users/login but I didn't receive it".

jest.mock("axios");

This looks like you're mocking the HTTP client Axios. Now you have a mock for the thing that needs to send requests to the Pact Mock.

In a Pact test, think of it as a unit tests for your API client code. The actual request needs to be sent to the Pact Mock and Pact will check the correct request was made, and return back the mocked response.

So the solution is simple:

  1. Remove all of the axios mocking
  2. Provide a way to modify the API target for loginUser
  3. Configure your API client to send the request to localhost:1234 instead of the real thing before running the tests

(NOTE: you can have pact find a free port dynamically by not setting the port option, and take the host from the response from the setup() call)

  • Related