Skip to content

Commit 22b87d6

Browse files
committed
fix: added-esm-like-tests
1 parent 889ef31 commit 22b87d6

File tree

6 files changed

+54
-5
lines changed

6 files changed

+54
-5
lines changed

.github/workflows/pull_request.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ jobs:
3535
- run: npm test
3636
env:
3737
CI: true
38+
- run: npm run test:esm-build
39+
env:
40+
CI: true

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"scripts": {
5252
"lint": "prettier --write .",
5353
"test": "vitest --coverage --run",
54+
"test:esm-build": "npm run build && ESM_BUILD=true vitest --config vitest.config.esm.ts --run",
5455
"test:watch": "vitest",
5556
"test:debug": "vitest --inspect-brk --no-file-parallelism --coverage",
5657
"prebuild": "rm -rf ./build",

tests/engine/unit/segments/segment_evaluators.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
SegmentCondition1
1515
} from '../../../../flagsmith-engine/evaluation/models.js';
1616

17+
const isEsmBuild = process.env.ESM_BUILD === 'true';
18+
1719
// todo: work out how to implement this in a test function or before hook
1820
vi.mock('../../../../flagsmith-engine/utils/hashing', () => ({
1921
getHashedPercentageForObjIds: vi.fn(() => 1)
@@ -395,7 +397,8 @@ describe('getContextValue', () => {
395397
});
396398
});
397399

398-
describe('percentage split operator', () => {
400+
// Skip in ESM build: vi.mock doesn't work with external modules
401+
describe.skipIf(isEsmBuild)('percentage split operator', () => {
399402
const mockContext: EvaluationContext = {
400403
environment: { key: 'env', name: 'Test Env' },
401404
identity: {

tests/sdk/flagsmith.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ import { EnvironmentModel } from '../../flagsmith-engine/environments/models.js'
1414
import { BaseOfflineHandler } from '../../sdk/offline_handlers.js';
1515
import { Agent } from 'undici';
1616

17+
const isEsmBuild = process.env.ESM_BUILD === 'true';
18+
1719
vi.mock('../../sdk/polling_manager');
18-
test('test_flagsmith_starts_polling_manager_on_init_if_enabled', () => {
20+
21+
// Skip in ESM build: vi.mock doesn't work with external modules
22+
test.skipIf(isEsmBuild)('test_flagsmith_starts_polling_manager_on_init_if_enabled', () => {
1923
new Flagsmith({
2024
environmentKey: 'ser.key',
2125
enableLocalEvaluation: true
@@ -32,7 +36,8 @@ test('test_flagsmith_local_evaluation_key_required', () => {
3236
}).toThrow('Using local evaluation requires a server-side environment key');
3337
});
3438

35-
test('test_update_environment_sets_environment', async () => {
39+
// Skip in ESM build: instanceof fails across module boundaries
40+
test.skipIf(isEsmBuild)('test_update_environment_sets_environment', async () => {
3641
const flg = flagsmith({
3742
environmentKey: 'ser.key'
3843
});
@@ -513,7 +518,8 @@ test('getIdentityFlags succeeds if initial fetch failed then succeeded', async (
513518
expect(flags2.isFeatureEnabled('some_feature')).toBe(true);
514519
});
515520

516-
test('get_user_agent_extracts_version_from_package_json', async () => {
521+
// Skip in ESM build: require() path resolution differs
522+
test.skipIf(isEsmBuild)('get_user_agent_extracts_version_from_package_json', async () => {
517523
const userAgent = getUserAgent();
518524
const packageJson = require('../../package.json');
519525

tests/sdk/offline-handlers.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import * as offlineEnvironment from './data/offline-environment.json';
77
vi.mock('fs');
88

99
const offlineEnvironmentString = JSON.stringify(offlineEnvironment);
10+
const isEsmBuild = process.env.ESM_BUILD === 'true';
1011

11-
test('local file handler', () => {
12+
// Skip in ESM build: instanceof fails across module boundaries
13+
test.skipIf(isEsmBuild)('local file handler', () => {
1214
const environmentDocumentFilePath = '/some/path/environment.json';
1315

1416
// Mock the fs.readFileSync function to return environmentJson

vitest.config.esm.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { defineConfig } from 'vitest/config';
2+
import path from 'path';
3+
4+
/**
5+
* Vitest config for testing against the built ESM output.
6+
* This catches CJS/ESM interop issues (like jsonpath) that don't surface
7+
* when testing TypeScript source directly.
8+
*
9+
* Run with: npm run test:esm-build (after npm run build)
10+
*/
11+
export default defineConfig({
12+
test: {
13+
globals: true,
14+
restoreMocks: true,
15+
exclude: ['**/node_modules/**'],
16+
server: {
17+
deps: {
18+
// Don't transform built ESM - test it as-is
19+
external: [/build\/esm/]
20+
}
21+
}
22+
},
23+
resolve: {
24+
alias: {
25+
// Redirect source imports to built ESM output
26+
'../../../flagsmith-engine': path.resolve(__dirname, 'build/esm/flagsmith-engine'),
27+
'../../../../flagsmith-engine': path.resolve(__dirname, 'build/esm/flagsmith-engine'),
28+
'../../../sdk': path.resolve(__dirname, 'build/esm/sdk'),
29+
'../../../../sdk': path.resolve(__dirname, 'build/esm/sdk'),
30+
'../../sdk': path.resolve(__dirname, 'build/esm/sdk'),
31+
'../sdk': path.resolve(__dirname, 'build/esm/sdk'),
32+
}
33+
}
34+
});

0 commit comments

Comments
 (0)