diff --git a/packages/server/src/automations/tests/n8n.spec.ts b/packages/server/src/automations/tests/n8n.spec.ts index d60a08b53b..0c18f313b1 100644 --- a/packages/server/src/automations/tests/n8n.spec.ts +++ b/packages/server/src/automations/tests/n8n.spec.ts @@ -1,4 +1,5 @@ import { getConfig, afterAll, runStep, actions } from "./utilities" +import nock from "nock" describe("test the outgoing webhook action", () => { let config = getConfig() @@ -9,31 +10,33 @@ describe("test the outgoing webhook action", () => { afterAll() + beforeEach(() => { + nock.cleanAll() + }) + it("should be able to run the action and default to 'get'", async () => { + nock("http://www.example.com/").get("/").reply(200, { foo: "bar" }) const res = await runStep(actions.n8n.stepId, { url: "http://www.example.com", body: { test: "IGNORE_ME", }, }) - expect(res.response.url).toEqual("http://www.example.com") - expect(res.response.method).toEqual("GET") - expect(res.response.body).toBeUndefined() + expect(res.response.foo).toEqual("bar") expect(res.success).toEqual(true) }) it("should add the payload props when a JSON string is provided", async () => { - const payload = `{ "name": "Adam", "age": 9 }` + nock("http://www.example.com/") + .post("/", { name: "Adam", age: 9 }) + .reply(200) const res = await runStep(actions.n8n.stepId, { body: { - value: payload, + value: JSON.stringify({ name: "Adam", age: 9 }), }, method: "POST", url: "http://www.example.com", }) - expect(res.response.url).toEqual("http://www.example.com") - expect(res.response.method).toEqual("POST") - expect(res.response.body).toEqual(`{"name":"Adam","age":9}`) expect(res.success).toEqual(true) }) @@ -53,6 +56,9 @@ describe("test the outgoing webhook action", () => { }) it("should not append the body if the method is HEAD", async () => { + nock("http://www.example.com/") + .head("/", body => body === "") + .reply(200) const res = await runStep(actions.n8n.stepId, { url: "http://www.example.com", method: "HEAD", @@ -60,9 +66,6 @@ describe("test the outgoing webhook action", () => { test: "IGNORE_ME", }, }) - expect(res.response.url).toEqual("http://www.example.com") - expect(res.response.method).toEqual("HEAD") - expect(res.response.body).toBeUndefined() expect(res.success).toEqual(true) }) }) diff --git a/packages/server/src/tests/jestSetup.ts b/packages/server/src/tests/jestSetup.ts index c01f415f9e..bc6384e4cd 100644 --- a/packages/server/src/tests/jestSetup.ts +++ b/packages/server/src/tests/jestSetup.ts @@ -1,6 +1,7 @@ import env from "../environment" import { env as coreEnv, timers } from "@budibase/backend-core" import { testContainerUtils } from "@budibase/backend-core/tests" +import nock from "nock" if (!process.env.CI) { // set a longer timeout in dev for debugging 100 seconds @@ -9,6 +10,15 @@ if (!process.env.CI) { jest.setTimeout(30 * 1000) } +nock.disableNetConnect() +nock.enableNetConnect(host => { + return ( + host.includes("localhost") || + host.includes("127.0.0.1") || + host.includes("::1") + ) +}) + testContainerUtils.setupEnv(env, coreEnv) afterAll(() => {