Skip to content
File

Blob: src/workerd/api/tests/compat-flags-test.js

javascript62 lines
1// Copyright (c) 2024 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4import { ok, strictEqual, throws } from 'node:assert';
5 
6const { compatibilityFlags } = globalThis.Cloudflare;
7ok(Object.isSealed(compatibilityFlags));
8ok(Object.isFrozen(compatibilityFlags));
9ok(!Object.isExtensible(compatibilityFlags));
10 
11// It shuld be possible to shadow the Cloudflare global
12const Cloudflare = 1;
13strictEqual(Cloudflare, 1);
14 
15export const compatFlagsTest = {
16 test() {
17 // The compatibility flags object should be sealed, frozen, and not extensible.
18 throws(() => (compatibilityFlags.no_nodejs_compat_v2 = '...'));
19 throws(() => (compatibilityFlags.not_a_real_compat_flag = '...'));
20 throws(() => {
21 delete compatibilityFlags['nodejs_compat_v2'];
22 });
23 
24 // The compatibility flags object should have no prototype.
25 strictEqual(Object.getPrototypeOf(compatibilityFlags), null);
26 
27 // The compatibility flags object should have the expected properties...
28 // That is... the only keys that should appear on the compatibilityFlags
29 // object are the enable flags.
30 //
31 // If a key does not appear, it can mean one of three things:
32 // 1. It is a disable flag.
33 // 2. It is an experimental flag and the experimental option is not set.
34 // 3. The flag does not exist.
35 //
36 // At this level we make no attempt to differentiate between these cases
37 ok(compatibilityFlags['nodejs_compat_v2']);
38 ok(compatibilityFlags['url_standard']);
39 
40 ok(!compatibilityFlags['no_nodejs_compat_v2']);
41 ok(!compatibilityFlags['url_original']);
42 strictEqual(compatibilityFlags['no_nodejs_compat_v2'], undefined);
43 strictEqual(compatibilityFlags['url_original'], undefined);
44 
45 // Since we are not specifying the experimental flag, experimental flags should
46 // not be included in the output.
47 strictEqual(compatibilityFlags['durable_object_rename'], undefined);
48 strictEqual('durable_object_rename' in compatibilityFlags, false);
49 
50 // If a flag does not exist, the value will be undefined.
51 strictEqual(compatibilityFlags['not-a-real-compat-flag'], undefined);
52 strictEqual('not-a-real-compat-flag' in compatibilityFlags, false);
53 
54 // The compatibility flags object should have the expected keys.
55 const keys = Object.keys(compatibilityFlags);
56 ok(keys.includes('nodejs_compat_v2'));
57 ok(keys.includes('url_standard'));
58 ok(!keys.includes('url_original'));
59 ok(!keys.includes('not-a-real-compat-flag'));
60 },
61};