Skip to content
2,594 changes: 975 additions & 1,619 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
"@types/node": "^20.16.10",
"@types/semver": "^7.3.9",
"@types/uuid": "^8.3.4",
"@vitest/coverage-v8": "^2.1.2",
"esbuild": "^0.25.0",
"@vitest/coverage-v8": "^4.1.4",
"esbuild": "^0.28.0",
"husky": "^7.0.4",
"install": "^0.13.0",
"json-schema-to-typescript": "^15.0.4",
Expand All @@ -85,6 +85,6 @@
"prettier": "^2.2.1",
"typescript": "^4.9.5",
"undici": "^6.19.8",
"vitest": "^2.1.2"
"vitest": "^4.1.4"
}
}
3 changes: 0 additions & 3 deletions tests/engine/unit/segments/segment_evaluators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,6 @@ describe.skipIf(isEsmBuild)('percentage split operator', () => {
segments: {},
features: {}
};
beforeEach(() => {
vi.clearAllMocks();
});

test.each([
[25.5, 30, true],
Expand Down
3 changes: 2 additions & 1 deletion tests/sdk/analytics.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getUserAgent } from '../../sdk/utils.js';
import { analyticsProcessor, fetch } from './utils.js';
import { fetch } from './fetchMock.js';
import { analyticsProcessor } from './utils.js';

test('test_analytics_processor_track_feature_updates_analytics_data', () => {
const aP = analyticsProcessor();
Expand Down
32 changes: 32 additions & 0 deletions tests/sdk/fetchMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { readFileSync } from 'fs';

const DATA_DIR = __dirname + '/data/';

export const environmentJSON = readFileSync(DATA_DIR + 'environment.json', 'utf-8');
export const flagsJSON = readFileSync(DATA_DIR + 'flags.json', 'utf-8');
export const identitiesJSON = readFileSync(DATA_DIR + 'identities.json', 'utf-8');

export function fetchImpl(url: string, options?: RequestInit) {
const headers = options?.headers as Record<string, string>;
if (!headers) throw new Error('missing request headers');
const env = headers['X-Environment-Key'];
if (!env)
return Promise.resolve(new Response('missing x-environment-key header', { status: 404 }));
if (url.includes('/environment-document')) {
if (env.startsWith('ser.')) {
return Promise.resolve(new Response(environmentJSON, { status: 200 }));
}
return Promise.resolve(
new Response('environment-document called without a server-side key', { status: 401 })
);
}
if (url.includes('/flags')) {
return Promise.resolve(new Response(flagsJSON, { status: 200 }));
}
if (url.includes('/identities')) {
return Promise.resolve(new Response(identitiesJSON, { status: 200 }));
}
return Promise.resolve(new Response('unknown url ' + url, { status: 404 }));
}

export const fetch = vi.fn(fetchImpl);
11 changes: 2 additions & 9 deletions tests/sdk/flagsmith-cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import {
fetch,
environmentJSON,
environmentModel,
flagsJSON,
flagsmith,
identitiesJSON,
TestCache
} from './utils.js';
import { fetch, environmentJSON, flagsJSON, identitiesJSON } from './fetchMock.js';
import { environmentModel, flagsmith, TestCache } from './utils.js';
import { generateIdentityCacheKey } from '../../sdk/utils.js';

test('test_empty_cache_not_read_but_populated', async () => {
Expand Down
3 changes: 2 additions & 1 deletion tests/sdk/flagsmith-environment-flags.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Flagsmith from '../../sdk/index.js';
import { environmentJSON, environmentModel, flagsJSON, flagsmith, fetch } from './utils.js';
import { fetch, environmentJSON, flagsJSON } from './fetchMock.js';
import { environmentModel, flagsmith } from './utils.js';
import { DefaultFlag } from '../../sdk/models.js';
import { getUserAgent } from '../../sdk/utils.js';

Expand Down
4 changes: 1 addition & 3 deletions tests/sdk/flagsmith-identity-flags.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import Flagsmith from '../../sdk/index.js';
import { fetch, environmentJSON, identitiesJSON } from './fetchMock.js';
import {
fetch,
environmentJSON,
flagsmith,
identitiesJSON,
identityWithTransientTraitsJSON,
transientIdentityJSON,
badFetch
Expand Down
10 changes: 2 additions & 8 deletions tests/sdk/flagsmith.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import Flagsmith from '../../sdk/index.js';
import { EnvironmentDataPollingManager } from '../../sdk/polling_manager.js';
import {
environmentJSON,
environmentModel,
flagsmith,
fetch,
offlineEnvironmentJSON,
badFetch
} from './utils.js';
import { fetch, environmentJSON } from './fetchMock.js';
import { environmentModel, flagsmith, offlineEnvironmentJSON, badFetch } from './utils.js';
import { DefaultFlag, Flags } from '../../sdk/models.js';
import { delay, getUserAgent } from '../../sdk/utils.js';
import { EnvironmentModel } from '../../flagsmith-engine/environments/models.js';
Expand Down
30 changes: 1 addition & 29 deletions tests/sdk/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AnalyticsProcessor } from '../../sdk/analytics.js';
import Flagsmith, { FlagsmithConfig } from '../../sdk/index.js';
import { Fetch, FlagsmithCache } from '../../sdk/types.js';
import { Flags } from '../../sdk/models.js';
import { fetch } from './fetchMock.js';

const DATA_DIR = __dirname + '/data/';

Expand All @@ -19,29 +20,6 @@ export class TestCache implements FlagsmithCache {
}
}

export const fetch = vi.fn((url: string, options?: RequestInit) => {
const headers = options?.headers as Record<string, string>;
if (!headers) throw new Error('missing request headers');
const env = headers['X-Environment-Key'];
if (!env)
return Promise.resolve(new Response('missing x-environment-key header', { status: 404 }));
if (url.includes('/environment-document')) {
if (env.startsWith('ser.')) {
return Promise.resolve(new Response(environmentJSON, { status: 200 }));
}
return Promise.resolve(
new Response('environment-document called without a server-side key', { status: 401 })
);
}
if (url.includes('/flags')) {
return Promise.resolve(new Response(flagsJSON, { status: 200 }));
}
if (url.includes('/identities')) {
return Promise.resolve(new Response(identitiesJSON, { status: 200 }));
}
return Promise.resolve(new Response('unknown url ' + url, { status: 404 }));
});

export const badFetch: Fetch = () => {
throw new Error('fetch failed');
};
Expand All @@ -68,18 +46,12 @@ export function flagsmith(params: FlagsmithConfig = {}) {
});
}

export const environmentJSON = readFileSync(DATA_DIR + 'environment.json', 'utf-8');

export const offlineEnvironmentJSON = readFileSync(DATA_DIR + 'offline-environment.json', 'utf-8');

export function environmentModel(environmentJSON: any) {
return buildEnvironmentModel(environmentJSON);
}

export const flagsJSON = readFileSync(DATA_DIR + 'flags.json', 'utf-8');

export const identitiesJSON = readFileSync(DATA_DIR + 'identities.json', 'utf-8');

export const transientIdentityJSON = readFileSync(DATA_DIR + 'transient-identity.json', 'utf-8');

export const identityWithTransientTraitsJSON = readFileSync(
Expand Down
6 changes: 6 additions & 0 deletions tests/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { fetch as mockFetch, fetchImpl } from './sdk/fetchMock.js';

beforeEach(() => {
mockFetch.mockReset();
Comment thread
khvn26 marked this conversation as resolved.
Outdated
mockFetch.mockImplementation(fetchImpl);
});
2 changes: 2 additions & 0 deletions vitest.config.esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default defineConfig({
test: {
globals: true,
restoreMocks: true,
clearMocks: true,
setupFiles: ['./tests/setup.ts'],
exclude: ['**/node_modules/**'],
server: {
deps: {
Expand Down
2 changes: 2 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export default defineConfig({
test: {
globals: true,
restoreMocks: true,
clearMocks: true,
setupFiles: ['./tests/setup.ts'],
coverage: {
reporter: ['text'],
exclude: ['build/**'],
Expand Down
Loading