index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. var test = require('tape');
  3. var keys = require('object-keys');
  4. var semver = require('semver');
  5. var isCore = require('../');
  6. var data = require('../core.json');
  7. var supportsNodePrefix = semver.satisfies(process.versions.node, '>= 16');
  8. test('core modules', function (t) {
  9. t.test('isCore()', function (st) {
  10. st.ok(isCore('fs'));
  11. st.ok(isCore('net'));
  12. st.ok(isCore('http'));
  13. st.ok(!isCore('seq'));
  14. st.ok(!isCore('../'));
  15. st.ok(!isCore('toString'));
  16. st.end();
  17. });
  18. t.test('core list', function (st) {
  19. var cores = keys(data);
  20. st.plan(cores.length);
  21. for (var i = 0; i < cores.length; ++i) {
  22. var mod = cores[i];
  23. var requireFunc = function () { require(mod); }; // eslint-disable-line no-loop-func
  24. if (isCore(mod)) {
  25. st.doesNotThrow(requireFunc, mod + ' supported; requiring does not throw');
  26. } else {
  27. st['throws'](requireFunc, mod + ' not supported; requiring throws');
  28. }
  29. }
  30. st.end();
  31. });
  32. t.test('core via repl module', { skip: !data.repl }, function (st) {
  33. var libs = require('repl')._builtinLibs; // eslint-disable-line no-underscore-dangle
  34. if (!libs) {
  35. st.skip('module.builtinModules does not exist');
  36. } else {
  37. for (var i = 0; i < libs.length; ++i) {
  38. var mod = libs[i];
  39. st.ok(data[mod], mod + ' is a core module');
  40. st.doesNotThrow(
  41. function () { require(mod); }, // eslint-disable-line no-loop-func
  42. 'requiring ' + mod + ' does not throw'
  43. );
  44. if (supportsNodePrefix) {
  45. st.doesNotThrow(
  46. function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
  47. 'requiring node:' + mod + ' does not throw'
  48. );
  49. } else {
  50. st['throws'](
  51. function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
  52. 'requiring node:' + mod + ' throws'
  53. );
  54. }
  55. }
  56. }
  57. st.end();
  58. });
  59. t.test('core via builtinModules list', { skip: !data.module }, function (st) {
  60. var libs = require('module').builtinModules;
  61. if (!libs) {
  62. st.skip('module.builtinModules does not exist');
  63. } else {
  64. var excludeList = [
  65. '_debug_agent',
  66. 'v8/tools/tickprocessor-driver',
  67. 'v8/tools/SourceMap',
  68. 'v8/tools/tickprocessor',
  69. 'v8/tools/profile'
  70. ];
  71. for (var i = 0; i < libs.length; ++i) {
  72. var mod = libs[i];
  73. if (excludeList.indexOf(mod) === -1) {
  74. st.ok(data[mod], mod + ' is a core module');
  75. st.doesNotThrow(
  76. function () { require(mod); }, // eslint-disable-line no-loop-func
  77. 'requiring ' + mod + ' does not throw'
  78. );
  79. if (supportsNodePrefix) {
  80. st.doesNotThrow(
  81. function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
  82. 'requiring node:' + mod + ' does not throw'
  83. );
  84. } else {
  85. st['throws'](
  86. function () { require('node:' + mod); }, // eslint-disable-line no-loop-func
  87. 'requiring node:' + mod + ' throws'
  88. );
  89. }
  90. }
  91. }
  92. }
  93. st.end();
  94. });
  95. t.end();
  96. });