Skip to content

Commit 8be0a75

Browse files
fix: vitest prod mode compat
1 parent ed9c66a commit 8be0a75

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"dev": "nx run-many --target=dev --all --parallel=999 --exclude=@lwc/perf-benchmarks,@lwc/perf-benchmarks-components,@lwc/integration-tests",
2121
"test": "vitest --workspace vitest.workspace.mjs",
2222
"test:production": "VITE_NODE_ENV=production vitest --workspace vitest.workspace.mjs",
23+
"test:production:debug": "VITE_NODE_ENV=production vitest --workspace vitest.workspace.mjs --no-file-parallelism --inspect-brk",
2324
"test:bespoke": "nx run-many --target=test",
2425
"test:debug": "vitest --workspace vitest.workspace.mjs --inspect-brk --no-file-parallelism",
2526
"test:ci": "vitest run --workspace vitest.workspace.mjs --coverage",

packages/@lwc/engine-core/src/framework/__tests__/context.spec.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
* SPDX-License-Identifier: MIT
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
66
*/
7-
import { describe, it, expect, vi, beforeAll, afterEach } from 'vitest';
8-
import { setFeatureFlagForTest } from '@lwc/features';
7+
import { describe, it, expect, vi, beforeAll, afterEach, afterAll } from 'vitest';
98
import { setTrustedContextSet, setContextKeys } from '@lwc/shared';
109
import { logWarnOnce } from '../../shared/logger';
1110
import { connectContext, disconnectContext } from '../modules/context';
1211

13-
// Mock the logger to avoid console output during tests
12+
// Mock the logger to inspect console output during tests
1413
vi.mock('../../shared/logger', () => ({
1514
logWarnOnce: vi.fn(),
1615
}));
@@ -33,6 +32,17 @@ const mockVM = {
3332
renderer: mockRenderer,
3433
} as any;
3534

35+
if (!(globalThis as any).lwcRuntimeFlags) {
36+
Object.defineProperty(globalThis, 'lwcRuntimeFlags', { value: {} });
37+
}
38+
39+
/**
40+
* Need to be able to set and reset the flags at will (lwc/features doesn't provide this)
41+
*/
42+
const setFeatureFlag = (name: string, value: boolean) => {
43+
(globalThis as any).lwcRuntimeFlags[name] = value;
44+
};
45+
3646
/**
3747
* These tests test that properties are correctly validated within the connectContext and disconnectContext
3848
* functions regardless of whether trusted context has been defined or not.
@@ -50,31 +60,35 @@ describe('context functions', () => {
5060
vi.clearAllMocks();
5161
});
5262

63+
afterAll(() => {
64+
setFeatureFlag('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false);
65+
});
66+
5367
describe('without setting trusted context', () => {
5468
it('should log a warning when trustedContext is not defined and connectContext is called with legacy signal context validation', () => {
55-
setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', true);
69+
setFeatureFlag('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', true);
5670
connectContext(mockVM);
5771
expect(logWarnOnce).toHaveBeenCalledWith(
5872
'Attempted to connect to trusted context but received the following error: component[contextfulKeys[i]][connectContext2] is not a function'
5973
);
6074
});
6175

6276
it('should not log a warning when trustedContext is not defined and connectContext is called with non-legacy context validation', () => {
63-
setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false);
77+
setFeatureFlag('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false);
6478
connectContext(mockVM);
6579
expect(logWarnOnce).not.toHaveBeenCalled();
6680
});
6781

6882
it('should log a warning when trustedContext is not defined and disconnectContext is called with legacy signal context validation', () => {
69-
setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', true);
83+
setFeatureFlag('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', true);
7084
disconnectContext(mockVM);
7185
expect(logWarnOnce).toHaveBeenCalledWith(
7286
'Attempted to disconnect from trusted context but received the following error: component[contextfulKeys[i]][disconnectContext2] is not a function'
7387
);
7488
});
7589

7690
it('should not log a warning when trustedContext is not defined and disconnectContext is called with non-legacy context validation', () => {
77-
setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false);
91+
setFeatureFlag('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false);
7892
disconnectContext(mockVM);
7993
expect(logWarnOnce).not.toHaveBeenCalled();
8094
});
@@ -83,10 +97,10 @@ describe('context functions', () => {
8397
describe('with trusted context set', () => {
8498
it('should not log warnings when trustedContext is defined', () => {
8599
setTrustedContextSet(new WeakSet());
86-
setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', true);
100+
setFeatureFlag('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', true);
87101
connectContext(mockVM);
88102
disconnectContext(mockVM);
89-
setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false);
103+
setFeatureFlag('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false);
90104
expect(logWarnOnce).not.toHaveBeenCalled();
91105
});
92106
});

packages/@lwc/engine-core/src/framework/__tests__/mutation-tracker.spec.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
import { describe, it, expect, vi, afterEach, beforeEach, afterAll, beforeAll } from 'vitest';
88
import { setTrustedSignalSet } from '@lwc/shared';
9-
import { setFeatureFlagForTest } from '@lwc/features';
109
import { componentValueObserved } from '../../framework/mutation-tracker';
1110

1211
// Create a mock VM object with required properties
@@ -17,6 +16,17 @@ const mockVM = {
1716
},
1817
} as any;
1918

19+
if (!(globalThis as any).lwcRuntimeFlags) {
20+
Object.defineProperty(globalThis, 'lwcRuntimeFlags', { value: {} });
21+
}
22+
23+
/**
24+
* Need to be able to set and reset the flags at will (lwc/features doesn't provide this)
25+
*/
26+
const setFeatureFlag = (name: string, value: boolean) => {
27+
(globalThis as any).lwcRuntimeFlags[name] = value;
28+
};
29+
2030
/**
2131
* These tests check that properties are correctly validated within the mutation-tracker
2232
* regardless of whether trusted context has been defined by a state manager or not.
@@ -25,40 +35,40 @@ const mockVM = {
2535
*/
2636
describe('mutation-tracker', () => {
2737
it('should not throw when componentValueObserved is called using the new signals validation and no signal set is defined', () => {
28-
setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false);
38+
setFeatureFlag('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false);
2939
expect(() => {
3040
componentValueObserved(mockVM, 'testKey', {});
3141
}).not.toThrow();
3242
});
3343

3444
it('should throw when componentValueObserved is called using legacy signals validation and no signal set has been defined', () => {
35-
setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', true);
45+
setFeatureFlag('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', true);
3646
expect(() => {
3747
componentValueObserved(mockVM, 'testKey', {});
3848
}).toThrow();
3949
});
4050

4151
it('should not throw when a trusted signal set is defined abd componentValueObserved is called', () => {
4252
setTrustedSignalSet(new WeakSet());
43-
44-
setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false);
53+
setFeatureFlag('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false);
4554
expect(() => {
4655
componentValueObserved(mockVM, 'testKey', {});
4756
}).not.toThrow();
4857

49-
setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', true);
58+
setFeatureFlag('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', true);
5059
expect(() => {
5160
componentValueObserved(mockVM, 'testKey', {});
5261
}).not.toThrow();
5362
});
5463

5564
beforeAll(() => {
56-
setFeatureFlagForTest('ENABLE_EXPERIMENTAL_SIGNALS', true);
65+
setFeatureFlag('ENABLE_EXPERIMENTAL_SIGNALS', true);
66+
setFeatureFlag('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', true);
5767
});
5868

5969
afterAll(() => {
60-
setFeatureFlagForTest('ENABLE_EXPERIMENTAL_SIGNALS', false);
61-
setFeatureFlagForTest('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false);
70+
setFeatureFlag('ENABLE_EXPERIMENTAL_SIGNALS', false);
71+
setFeatureFlag('ENABLE_LEGACY_SIGNAL_CONTEXT_VALIDATION', false);
6272
});
6373

6474
beforeEach(() => {

0 commit comments

Comments
 (0)