source: frontend/node_modules/is-core-module/test/index.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 7.7 KB
Line 
1'use strict';
2
3var test = require('tape');
4var keys = require('object-keys');
5var semver = require('semver');
6var mockProperty = require('mock-property');
7
8var isCore = require('../');
9var data = require('../core.json');
10
11var supportsNodePrefix = semver.satisfies(process.versions.node, '^14.18 || >= 16', { includePrerelease: true });
12
13test('core modules', function (t) {
14 t.test('isCore()', function (st) {
15 st.ok(isCore('fs'));
16 st.ok(isCore('net'));
17 st.ok(isCore('http'));
18
19 st.ok(!isCore('seq'));
20 st.ok(!isCore('../'));
21
22 st.ok(!isCore('toString'));
23
24 st.end();
25 });
26
27 t.test('core list', function (st) {
28 var cores = keys(data);
29 st.plan(cores.length);
30
31 for (var i = 0; i < cores.length; ++i) {
32 var mod = cores[i];
33 var requireFunc = function () { require(mod); }; // eslint-disable-line no-loop-func
34 if (isCore(mod)) {
35 st.doesNotThrow(requireFunc, mod + ' supported; requiring does not throw');
36 } else {
37 st['throws'](requireFunc, mod + ' not supported; requiring throws');
38 }
39 }
40
41 st.end();
42 });
43
44 t.test('core via repl module', { skip: !data.repl }, function (st) {
45 var libs = require('repl')._builtinLibs; // eslint-disable-line no-underscore-dangle
46 if (!libs) {
47 st.skip('repl._builtinLibs does not exist');
48 } else {
49 for (var i = 0; i < libs.length; ++i) {
50 var mod = libs[i];
51 st.ok(data[mod], mod + ' is a core module');
52 st.doesNotThrow(
53 function () { require(mod); }, // eslint-disable-line no-loop-func
54 'requiring ' + mod + ' does not throw'
55 );
56 if (mod.slice(0, 5) !== 'node:') {
57 if (supportsNodePrefix) {
58 st.doesNotThrow(
59 function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
60 'requiring node:' + mod + ' does not throw'
61 );
62 } else {
63 st['throws'](
64 function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
65 'requiring node:' + mod + ' throws'
66 );
67 }
68 }
69 }
70 }
71 st.end();
72 });
73
74 t.test('core via builtinModules list', { skip: !data.module }, function (st) {
75 var Module = require('module');
76 var libs = Module.builtinModules;
77 if (!libs) {
78 st.skip('module.builtinModules does not exist');
79 } else {
80 var excludeList = [
81 '_debug_agent',
82 'v8/tools/tickprocessor-driver',
83 'v8/tools/SourceMap',
84 'v8/tools/tickprocessor',
85 'v8/tools/profile'
86 ];
87
88 // see https://github.com/nodejs/node/issues/42785
89 if (semver.satisfies(process.version, '>= 18')) {
90 libs = libs.concat('node:test');
91 }
92 if (semver.satisfies(process.version, '^20.12 || >= 21.7')) {
93 libs = libs.concat('node:sea');
94 }
95 if (semver.satisfies(process.version, '>= 23.4')) {
96 libs = libs.concat('node:sqlite');
97 }
98
99 for (var i = 0; i < libs.length; ++i) {
100 var mod = libs[i];
101 if (excludeList.indexOf(mod) === -1) {
102 st.ok(data[mod], mod + ' is a core module');
103
104 if (Module.isBuiltin) {
105 st.ok(Module.isBuiltin(mod), 'module.isBuiltin(' + mod + ') is true');
106 }
107
108 st.doesNotThrow(
109 function () { require(mod); }, // eslint-disable-line no-loop-func
110 'requiring ' + mod + ' does not throw'
111 );
112
113 if (process.getBuiltinModule) {
114 st.equal(
115 process.getBuiltinModule(mod),
116 require(mod),
117 'process.getBuiltinModule(' + mod + ') === require(' + mod + ')'
118 );
119 }
120
121 if (mod.slice(0, 5) !== 'node:') {
122 if (supportsNodePrefix) {
123 st.doesNotThrow(
124 function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
125 'requiring node:' + mod + ' does not throw'
126 );
127 } else {
128 st['throws'](
129 function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
130 'requiring node:' + mod + ' throws'
131 );
132 }
133 }
134 }
135 }
136 }
137
138 st.end();
139 });
140
141 t.test('isCore with explicit nodeVersion', function (st) {
142 st.ok(isCore('async_hooks', '8.0.0'), 'async_hooks is core in 8.0.0 (exact match on >= specifier)');
143 st.ok(isCore('buffer_ieee754', '0.5.0'), 'buffer_ieee754 is core in 0.5.0');
144 st.ok(!isCore('buffer_ieee754', '0.9.7'), 'buffer_ieee754 is not core in 0.9.7');
145 st.ok(!isCore('buffer_ieee754', '1.0.0'), 'buffer_ieee754 is not core in 1.0.0');
146 st.ok(isCore('buffer_ieee754', '0.8.0'), 'buffer_ieee754 is core in 0.8.0');
147
148 st['throws'](
149 function () { isCore('async_hooks', null); },
150 TypeError,
151 'isCore with non-string non-undefined nodeVersion throws TypeError'
152 );
153 st['throws'](
154 function () { isCore('async_hooks', 123); },
155 TypeError,
156 'isCore with numeric nodeVersion throws TypeError'
157 );
158
159 st.end();
160 });
161
162 t.test('isCore with non-boolean specifier and invalid nodeVersion', function (st) {
163 st['throws'](
164 function () { isCore('async_hooks', null); },
165 TypeError,
166 'isCore with null nodeVersion on non-boolean module throws TypeError'
167 );
168 st['throws'](
169 function () { isCore('async_hooks', 123); },
170 TypeError,
171 'isCore with numeric nodeVersion on non-boolean module throws TypeError'
172 );
173
174 st.end();
175 });
176
177 t.test('isCore with undefined nodeVersion and non-string process.versions.node', function (st) {
178 st.teardown(mockProperty(process, 'versions', { value: { node: null } }));
179
180 st['throws'](
181 function () { isCore('async_hooks'); },
182 TypeError,
183 'isCore throws when process.versions.node is not a string'
184 );
185
186 st.end();
187 });
188
189 t.test('isCore with undefined nodeVersion and falsy process.versions', function (st) {
190 st.teardown(mockProperty(process, 'versions', { value: null }));
191
192 st['throws'](
193 function () { isCore('async_hooks'); },
194 TypeError,
195 'isCore throws when process.versions is falsy'
196 );
197
198 st.end();
199 });
200
201 t.test('specifierIncluded with = operator (bare version)', function (st) {
202 var testKey = '__test_equal_op';
203 data[testKey] = '14.18';
204 st.teardown(function () { delete data[testKey]; });
205
206 st.ok(!isCore(testKey, '14.17.0'), 'returns false when minor version does not match with = op');
207 st.ok(!isCore(testKey, '15.0.0'), 'returns false when major version does not match with = op');
208 st.ok(!isCore(testKey, '14.18.0'), 'returns false for exact match with = op (= is not >=)');
209
210 st.end();
211 });
212
213 t.test('isCore with short version strings', function (st) {
214 st.ok(isCore('async_hooks', '8'), 'async_hooks is core with single-part version "8"');
215 st.ok(!isCore('async_hooks', '7'), 'async_hooks is not core with single-part version "7"');
216 st.ok(isCore('cluster', '0.5'), 'cluster is core with two-part version "0.5"');
217
218 st.end();
219 });
220
221 t.test('matchesRange with empty specifiers array', function (st) {
222 var testKey = '__test_empty_split';
223 var testRange = '__test_range__';
224 data[testKey] = testRange;
225 st.teardown(function () { delete data[testKey]; });
226
227 var origSplit = String.prototype.split;
228 st.teardown(mockProperty(String.prototype, 'split', {
229 value: function () {
230 if (String(this) === testRange) {
231 return [];
232 }
233 return origSplit.apply(this, arguments);
234 }
235 }));
236
237 st.ok(!isCore(testKey, '8.0.0'), 'returns false when specifiers array is empty');
238
239 st.end();
240 });
241
242 t.test('Object.prototype pollution', function (st) {
243 var nonKey = 'not a core module';
244 st.teardown(mockProperty(Object.prototype, 'fs', { value: false }));
245 st.teardown(mockProperty(Object.prototype, 'path', { value: '>= 999999999' }));
246 st.teardown(mockProperty(Object.prototype, 'http', { value: data.http }));
247 st.teardown(mockProperty(Object.prototype, nonKey, { value: true }));
248
249 st.equal(isCore('fs'), true, 'fs is a core module even if Object.prototype lies');
250 st.equal(isCore('path'), true, 'path is a core module even if Object.prototype lies');
251 st.equal(isCore('http'), true, 'path is a core module even if Object.prototype matches data');
252 st.equal(isCore(nonKey), false, '"' + nonKey + '" is not a core module even if Object.prototype lies');
253
254 st.end();
255 });
256
257 t.end();
258});
Note: See TracBrowser for help on using the repository browser.