Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit 356046e

Browse files
committed
Cypress: Test that user can modify callbackWaitsForEmptyEventLoop
Test that users can modify the Netlify Function's callbackWaitsForEmptyEventLoop behavior via the newly exposed function event and context objects. When callbackWaitsForEmptyEventLoop is true (default), the function does not finish until all async processes and timeouts are completed (or cleared). The user can set this to false to not wait for other processes to finish.
1 parent c537545 commit 356046e

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const WaitForEmptyEventLoop = () => <p>Successfully rendered page!</p>;
2+
3+
export const getServerSideProps = async ({ params, req }) => {
4+
// Set up long-running process
5+
const timeout = setTimeout(() => {}, 100000);
6+
7+
// Set behavior of whether to wait for empty event loop
8+
const wait = String(params.wait).toLowerCase() === "true";
9+
const { context: functionContext } = req.netlifyFunction;
10+
functionContext.callbackWaitsForEmptyEventLoop = wait;
11+
12+
return {
13+
props: {},
14+
};
15+
};
16+
17+
export default WaitForEmptyEventLoop;

cypress/integration/default_spec.js

+18
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,24 @@ describe("getServerSideProps", () => {
161161
});
162162
});
163163

164+
it("can modify the callbackWaitsForEmptyEventLoop behavior", () => {
165+
// netlify dev never waits on empty event loop
166+
if (Cypress.env("DEPLOY") !== "local") {
167+
cy.request({
168+
url: "/getServerSideProps/wait-on-empty-event-loop/true",
169+
failOnStatusCode: false,
170+
// Functions time out after 10s, so we need to wait a bit
171+
timeout: 15000,
172+
}).then((response) => {
173+
expect(response.status).to.eq(502);
174+
expect(response.body).to.contain("Task timed out");
175+
});
176+
}
177+
178+
cy.visit("/getServerSideProps/wait-on-empty-event-loop/false");
179+
cy.get("p").should("contain", "Successfully rendered page!");
180+
});
181+
164182
context("with static route", () => {
165183
it("loads TV shows", () => {
166184
cy.visit("/getServerSideProps/static");

0 commit comments

Comments
 (0)