pact-foundation / jest-pact

A Pact adaptor for to allow you to easily run tests with Jest
https://pact.io
MIT License
81 stars 12 forks source link

Pact Verification Failed for multipart/form-data request #195

Closed dula85 closed 4 years ago

dula85 commented 4 years ago

I am writing consumer pact test and application is react and test frame work is jest enzyme. multipart/form-data request as bellow,

pactWith({ consumer: "example-react-site", 
           provider: "example-api",
           logLevel:"debug",
           port: 1234
        }, provider => {
    beforeAll(() => {
        provider.removeInteractions();
    });
    beforeEach(() => {
        Constants.baseUrl = provider.mockService.baseUrl;
    });

    describe("example api endpoint", () => {
        beforeEach(async () => {
            const file = path.join(__dirname, "./", "new_file.txt");
            const data = fs.readFileSync(file);
            const rawData = atob(data);
            const formData = `----------------------------771442083308144732506745
Content-Disposition: form-data; name="model"

{
    "returnUrl": "returnurl",
    "matterReference": "test matters",
    "emailTemplate": {
        "subject": "test",
        "body": "test"
    },
    "recipients": [
        {
            "name": "test",
            "email": "test@example.com",
            "order": 1,
            "anchors": [
                {
                    "name": "<!purchaser1Signer>",
                    "mandatory": false,
                    "type": "fullName",
                    "fontColor": null,
                    "buttonText": null,
                    "bold": false,
                    "italic": false,
                    "locked": false
                }
            ]
        }
    ]
}
----------------------------771442083308144732506745
Content-Disposition: form-data; name="files"; filename="doc.pdf"

${rawData}
----------------------------771442083308144732506745--
`.replace(/\n/gi, '\r\n');

            provider.addInteraction({
                state: "example service has a document and recipients",
                uponReceiving: "a request to digitally sign a document",
                withRequest: {
                    method: "POST",
                    path: "/example/sign",
                    headers: {
                        "Content-Type": string("multipart/form-data; boundary=--------------------------771442083308144732506745"),
                        "Accept": string('application/json')
                    },
                    body: string(formData)
                },
                willRespondWith: {
                    status: 200,
                    headers: {
                        "Content-Type": string('application/json; charset=UTF-8')
                    },
                    body: {
                        responseId: string("OR-12345")
                    }
                }
            })
        });

        it("should send document sign", async () => {
            //test implementation
        });
    });
    }); 
});

new_file.txt : base64 encode file

Jest setup as follows.

"jest": {
    "testResultsProcessor": "jest-teamcity-reporter",
    "verbose": true,
    "moduleDirectories": [
      "node_modules",
      "src/taskpane"
    ],
    "roots": [
      "<rootDir>/src/taskpane"
    ],
    "testMatch": [
      "**/__tests__/**/*.+(ts|tsx|js)",
      "**/?(*.)+(spec|test).?(pact).+(ts|tsx|js)"
    ],
    "transform": {
      "^.+\\.(ts|tsx)$": "ts-jest",
      "^.+\\.(js|jsx)$": "babel-jest"
    },
    "snapshotSerializers": [
      "enzyme-to-json/serializer"
    ],
    "setupFiles": [
      "./src/setupJest.js"
    ],
    "moduleNameMapper": {
      "^.+\\.(jpg|ico|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|css|less|scss)$": "<rootDir>/src/taskpane/__mocks__/fileMock.js"
    },
    "testEnvironment": "node",
    "testTimeout": 12000
  }

Behaviour: I am getting Pact verification failed as bellow,

  console.error
      at node_modules/@pact-foundation/src/httpPact.ts:118:17

  console.error
    Pact verification failed!

      at node_modules/@pact-foundation/src/httpPact.ts:119:17

  console.error
    Actual interactions do not match expected interactions for mock MockService.

    Missing requests:
        POST /example/sign

    Unexpected requests:
        POST /example/sign

    See path/pact/logs/mockserver-integration.log for details.

      at node_modules/@pact-foundation/src/httpPact.ts:120:17

Pact between example-react-site and example-api › with 30000 ms timeout for Pact › example api endpoint › should send document to sign

using, jest: 26.0.1 jest-pact: 0.5.4

But if I remove generated log file and run pact test it will run successfully all the time. wonder what is happening and possible fix.

TimothyJones commented 4 years ago

I don't think this is related to jest-pact, but what does the log at path/pact/logs/mockserver-integration.log show?

TimothyJones commented 4 years ago

I'll close this for now as it hasn't had any activity and I don't think it's a jest-pact issue.

Feel free to re-open if you think it needs further attention.