Skip to content

Commit

Permalink
use supertest
Browse files Browse the repository at this point in the history
  • Loading branch information
cstrnt committed Dec 17, 2023
1 parent 32b070c commit 6442836
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 35 deletions.
2 changes: 2 additions & 0 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
"devDependencies": {
"@types/express": "^4.17.17",
"@types/koa": "^2.13.12",
"@types/supertest": "^2.0.16",
"express": "^4.18.2",
"koa": "^2.14.2",
"node-fetch": "^3.3.0",
"supertest": "^6.3.3",
"tsconfig": "workspace:*",
"tsup": "^6.5.0",
"typescript": "^4.9.3",
Expand Down
49 changes: 15 additions & 34 deletions packages/node/tests/express.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import express from "express";
import { Server } from "http";
import { createAbbyMiddleWare } from "../src/express";
import { ABBY_AB_STORAGE_PREFIX } from "@tryabby/core";
import fetch from "node-fetch";
import request from "supertest";

const app = express();
const PORT = 5555;
const SERVER_URL = `http://localhost:${PORT}`;

let server: Server | undefined = undefined;

const testVariants = ["OldFooter", "NewFooter"] as const;
const test2Variants = ["SimonsText", "MatthiasText", "TomsText", "TimsText"] as const;
Expand Down Expand Up @@ -52,44 +47,30 @@ app.get("/test", middleware, (req, res) => {
});
});

beforeAll(async () => {
server = app.listen(PORT);
});

afterAll(() => {
server?.close();
});

it("should work with feature flags", async () => {
const data = await fetch(`${SERVER_URL}`).then((r) => r.json());
expect(data).toEqual({
const res = await request(app).get("/");
expect(res.body).toEqual({
flag1: true,
flag2: false,
});
});

it("should work with A/B tests", async () => {
const data = (await fetch(`${SERVER_URL}/test`).then((r) => r.json())) as {
test: string;
test2: string;
};

expect(data.test).to.be.oneOf(testVariants);
expect(data.test2).to.be.oneOf(test2Variants);
const res = await request(app).get("/test");
expect(res.body.test).to.be.oneOf(testVariants);
expect(res.body.test2).to.be.oneOf(test2Variants);
});

it("should work with a cookie seed", async () => {
const data = (await fetch(`${SERVER_URL}/test`, {
headers: {
cookie: `${ABBY_AB_STORAGE_PREFIX}${abby.getConfig().projectId}_test=${
const res = await request(app)
.get("/test")
.set(
"Cookie",
`${ABBY_AB_STORAGE_PREFIX}${abby.getConfig().projectId}_test=${
testVariants[0]
};${ABBY_AB_STORAGE_PREFIX}${abby.getConfig().projectId}_test2=${test2Variants[2]}`,
},
}).then((r) => r.json())) as {
test: string;
test2: string;
};
};${ABBY_AB_STORAGE_PREFIX}${abby.getConfig().projectId}_test2=${test2Variants[2]}`
);

expect(data.test).toEqual(testVariants[0]);
expect(data.test2).toEqual(test2Variants[2]);
expect(res.body.test).toEqual(testVariants[0]);
expect(res.body.test2).toEqual(test2Variants[2]);
});
90 changes: 89 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6442836

Please sign in to comment.