-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtelemetryValidation.test.ts
More file actions
69 lines (59 loc) · 3.45 KB
/
telemetryValidation.test.ts
File metadata and controls
69 lines (59 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import * as sinon from "sinon";
import * as fs from "node:fs/promises";
import { FeatureManager, ConfigurationObjectFeatureFlagProvider } from "@microsoft/feature-management";
import { createTelemetryPublisher as createNodeTelemetryPublisher } from "@microsoft/feature-management-applicationinsights-node";
import { createTelemetryPublisher as createBrowserTelemetryPublisher } from "@microsoft/feature-management-applicationinsights-browser";
import { FILE_PATH, SAMPLE_JSON_KEY, TESTS_JSON_KEY, validateFeatureEvaluation, validateTelemetry, FeatureFlagTest } from "./utils.js";
import { ApplicationInsights } from "@microsoft/applicationinsights-web";
import applicationInsights from "applicationinsights";
// For telemetry validation
let eventNameToValidate;
let eventPropertiesToValidate;
applicationInsights.setup("DUMMY-CONNECTION-STRING").start();
sinon.stub(applicationInsights.defaultClient, "trackEvent").callsFake((event) => {
eventNameToValidate = event.name;
eventPropertiesToValidate = event.properties;
});
const appInsights = new ApplicationInsights({ config: { connectionString: "DUMMY-CONNECTION-STRING" }});
sinon.stub(appInsights, "trackEvent").callsFake((event, customProperties) => {
eventNameToValidate = event.name;
eventPropertiesToValidate = customProperties;
});
async function runTestWithNodePackage(testName: string) {
const config = JSON.parse(await fs.readFile(FILE_PATH + testName + SAMPLE_JSON_KEY, "utf8"));
const testcases: FeatureFlagTest[] = JSON.parse(await fs.readFile(FILE_PATH + testName + TESTS_JSON_KEY, "utf8"));
const ffProvider = new ConfigurationObjectFeatureFlagProvider(config);
const fm = new FeatureManager(ffProvider, { onFeatureEvaluated: createNodeTelemetryPublisher(applicationInsights.defaultClient) });
for (const testcase of testcases) {
const featureFlagName = testcase.FeatureFlagName;
const context = { userId: testcase.Inputs?.User, groups: testcase.Inputs?.Groups };
await fm.getVariant(featureFlagName, context);
validateTelemetry(testcase, undefined, eventNameToValidate, eventPropertiesToValidate);
validateFeatureEvaluation(testcase, fm);
}
}
async function runTestWithBrowserPackage(testName: string) {
const config = JSON.parse(await fs.readFile(FILE_PATH + testName + SAMPLE_JSON_KEY, "utf8"));
const testcases: FeatureFlagTest[] = JSON.parse(await fs.readFile(FILE_PATH + testName + TESTS_JSON_KEY, "utf8"));
const ffProvider = new ConfigurationObjectFeatureFlagProvider(config);
const fm = new FeatureManager(ffProvider, { onFeatureEvaluated: createBrowserTelemetryPublisher(appInsights) });
for (const testcase of testcases) {
const featureFlagName = testcase.FeatureFlagName;
const context = { userId: testcase.Inputs?.User, groups: testcase.Inputs?.Groups };
await fm.getVariant(featureFlagName, context);
validateTelemetry(testcase, undefined, eventNameToValidate, eventPropertiesToValidate);
validateFeatureEvaluation(testcase, fm);
}
}
describe("telemetry with provider and node package", function () {
it("should pass BasicTelemetry test", async () => {
await runTestWithNodePackage("BasicTelemetry");
});
});
describe("telemetry with provider and browser package", function () {
it("should pass BasicTelemetry test", async () => {
await runTestWithBrowserPackage("BasicTelemetry");
});
});