Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of better way to manage flags and mocks #55

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/controllers/start.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { NextFunction, Request, Response } from "express";

import * as config from "../config";
import { hasFeature } from "utils/feature.flag";

export const get = (_req: Request, res: Response, _next: NextFunction) => {
// example error message using the locales json files
const errorMessage = res.locals.i18n["startPage"]["errorStartAddressMissing"];

if (hasFeature().FLAG_1) {
console.log("***** FLAG_1");
}
if (hasFeature().FLAG_2) {
console.log("***** FLAG_2");
}
return res.render(config.START_TEMPLATE, {
errorMessage
});
Expand Down
19 changes: 19 additions & 0 deletions src/test/flag.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import app from "app";
import request from "supertest";
import * as config from "../config";
import { hasFeature } from "../utils/feature.flag";

jest.mock("../utils/feature.flag");

(hasFeature as jest.Mock).mockReturnValue({
"FLAG_1": false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could have a pre-defined list of all the feature flags somewhere and then just override to set individual ones to true, in specific tests, when required.

"FLAG_2": true
});

describe("Experimental flags test", () => {
test("something", async () => {
await request(app).get(config.START_URL);

// check console to see what is logged
});
});
4 changes: 4 additions & 0 deletions src/test/global.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ export default () => {
process.env.PIWIK_SITE_ID = "24";
process.env.PIWIK_URL = "piwik.url";
process.env.PORT = "3000";

process.env.FLAG_1 = "false";
process.env.FLAG_2 = "false";

};
12 changes: 12 additions & 0 deletions src/utils/feature.flag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getEnvironmentValueAsBoolean } from "./environment.value";

type FeatureFlags = {
[key: string]: boolean
}

const flags: FeatureFlags = {
"FLAG_1": getEnvironmentValueAsBoolean("FLAG_1"),
"FLAG_2": getEnvironmentValueAsBoolean("FLAG_2")
};

export const hasFeature = (): FeatureFlags => flags;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasFeature() doesn't return a boolean but a list of feature flag boolean values, so that should be reflected in the function name.