Skip to content

Commit

Permalink
Merge pull request #228 from Yurickh/fix/expired-listener
Browse files Browse the repository at this point in the history
Fix expired listener and emitter
  • Loading branch information
YOU54F authored Dec 10, 2024
2 parents 7bd19d6 + 274501e commit 020b6d8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/pactFromMswServer.msw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ describe("API - With MSW mock generating a pact", () => {
expect(respProduct).toEqual(hiddenVisibilityProduct);
});

test("handle requests mocked with error", async () => {
server.use(
http.get(API.url + "/product/10", () => HttpResponse.error())
);

// This request is going to be marked as expired after the test is finished and should be handled gracefully by the subsequent writeToFile()
await expect(()=> API.getProduct("10")).rejects.toEqual(new Error('TypeError: Network error'))
})

test("unhandled route", async () => {
await expect(API.getProduct("11")).rejects.toThrow(
/^Error: connect ECONNREFUSED (127.0.0.1|::1):8081.*$/
Expand Down
4 changes: 2 additions & 2 deletions src/pactMswAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ export const setupPactMswAdapter = ({
setTimeout(() => {
const expired = { requestId, startTime, request }
const activeIdx = activeRequestIds.indexOf(requestId);
emitter.emit("pact-msw-adapter:expired", expired);
if (activeIdx >= 0) {
// Could be removed if completed or the test ended
activeRequestIds.splice(activeIdx, 1);
expiredRequests.push(expired);
}
emitter.emit("pact-msw-adapter:expired", expired);
}, options.timeout);
});

Expand Down Expand Up @@ -301,7 +301,7 @@ const transformMswToPact = async (
}

const events = [
"pact-msw-adapter:expired ",
"pact-msw-adapter:expired",
"pact-msw-adapter:match",
"pact-msw-adapter:new-test",
"pact-msw-adapter:clear",
Expand Down

0 comments on commit 020b6d8

Please sign in to comment.