Skip to content

Commit 08d82f7

Browse files
committed
Update node-sdk example tests for flag overrides API
1 parent cc7b8ad commit 08d82f7

4 files changed

Lines changed: 34 additions & 14 deletions

File tree

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
import request from "supertest";
22
import app, { todos } from "./app";
3-
import { beforeEach, describe, it, expect, beforeAll } from "vitest";
3+
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";
44

55
import reflag from "./reflag";
66

7+
function flag(name: string, enabled: boolean): void {
8+
let remove: (() => void) | undefined;
9+
10+
beforeEach(() => {
11+
remove = reflag.pushFlagOverrides({ [name]: enabled });
12+
});
13+
14+
afterEach(() => {
15+
remove?.();
16+
remove = undefined;
17+
});
18+
}
19+
720
beforeAll(async () => await reflag.initialize());
21+
822
beforeEach(() => {
9-
reflag.featureOverrides = {
23+
reflag.setFlagOverrides({
1024
"show-todos": true,
11-
};
25+
});
26+
});
27+
28+
afterEach(() => {
29+
reflag.clearFlagOverrides();
1230
});
1331

1432
describe("API Tests", () => {
@@ -24,12 +42,13 @@ describe("API Tests", () => {
2442
expect(response.body).toEqual({ todos });
2543
});
2644

27-
it("should return no todos when list is disabled", async () => {
28-
reflag.featureOverrides = () => ({
29-
"show-todos": false,
45+
describe("with show-todos temporarily disabled", () => {
46+
flag("show-todos", false);
47+
48+
it("should return no todos", async () => {
49+
const response = await request(app).get("/todos");
50+
expect(response.status).toBe(200);
51+
expect(response.body).toEqual({ todos: [] });
3052
});
31-
const response = await request(app).get("/todos");
32-
expect(response.status).toBe(200);
33-
expect(response.body).toEqual({ todos: [] });
3453
});
3554
});

packages/node-sdk/examples/express/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import reflag from "./reflag";
22
import express from "express";
3-
import { BoundReflagClient } from "../src";
3+
import type { BoundReflagClient } from "../../src";
44

55
// Augment the Express types to include the `reflagUser` property on the `res.locals` object
66
// This will allow us to access the ReflagClient instance in our route handlers

packages/node-sdk/examples/express/bucket.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { ReflagClient, Context, FlagOverrides } from "../../";
1+
import { ReflagClient, Context, FlagOverrides } from "../../src";
22

33
type CreateConfigPayload = {
44
minimumLength: number;
55
};
66

77
// Extending the Flags interface to define the available features
8-
declare module "../../types" {
8+
declare module "../../src/types" {
99
interface Flags {
1010
"show-todos": boolean;
1111
"create-todos": {
@@ -18,7 +18,7 @@ declare module "../../types" {
1818
}
1919
}
2020

21-
let featureOverrides = (_: Context): FlagOverrides => {
21+
const flagOverrides = (_: Context): FlagOverrides => {
2222
return {
2323
"create-todos": {
2424
isEnabled: true,
@@ -39,5 +39,5 @@ let featureOverrides = (_: Context): FlagOverrides => {
3939
export default new ReflagClient({
4040
// Optional: Set a logger to log debug information, errors, etc.
4141
logger: console,
42-
featureOverrides, // Optional: Set feature overrides
42+
flagOverrides, // Optional: Set flag overrides
4343
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from "./bucket";

0 commit comments

Comments
 (0)