core.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. var test = require('tape');
  2. var keys = require('object-keys');
  3. var semver = require('semver');
  4. var resolve = require('../');
  5. var brokenNode = semver.satisfies(process.version, '11.11 - 11.13');
  6. test('core modules', function (t) {
  7. t.test('isCore()', function (st) {
  8. st.ok(resolve.isCore('fs'));
  9. st.ok(resolve.isCore('net'));
  10. st.ok(resolve.isCore('http'));
  11. st.ok(!resolve.isCore('seq'));
  12. st.ok(!resolve.isCore('../'));
  13. st.ok(!resolve.isCore('toString'));
  14. st.end();
  15. });
  16. t.test('core list', function (st) {
  17. var cores = keys(resolve.core);
  18. st.plan(cores.length);
  19. for (var i = 0; i < cores.length; ++i) {
  20. var mod = cores[i];
  21. // note: this must be require, not require.resolve, due to https://github.com/nodejs/node/issues/43274
  22. var requireFunc = function () { require(mod); }; // eslint-disable-line no-loop-func
  23. t.comment(mod + ': ' + resolve.core[mod]);
  24. if (resolve.core[mod]) {
  25. st.doesNotThrow(requireFunc, mod + ' supported; requiring does not throw');
  26. } else if (brokenNode) {
  27. st.ok(true, 'this version of node is broken: attempting to require things that fail to resolve breaks "home_paths" tests');
  28. } else {
  29. st.throws(requireFunc, mod + ' not supported; requiring throws');
  30. }
  31. }
  32. st.end();
  33. });
  34. t.test('core via repl module', { skip: !resolve.core.repl }, function (st) {
  35. var libs = require('repl')._builtinLibs; // eslint-disable-line no-underscore-dangle
  36. if (!libs) {
  37. st.skip('module.builtinModules does not exist');
  38. return st.end();
  39. }
  40. for (var i = 0; i < libs.length; ++i) {
  41. var mod = libs[i];
  42. st.ok(resolve.core[mod], mod + ' is a core module');
  43. st.doesNotThrow(
  44. function () { require(mod); }, // eslint-disable-line no-loop-func
  45. 'requiring ' + mod + ' does not throw'
  46. );
  47. }
  48. st.end();
  49. });
  50. t.test('core via builtinModules list', { skip: !resolve.core.module }, function (st) {
  51. var libs = require('module').builtinModules;
  52. if (!libs) {
  53. st.skip('module.builtinModules does not exist');
  54. return st.end();
  55. }
  56. var blacklist = [
  57. '_debug_agent',
  58. 'v8/tools/tickprocessor-driver',
  59. 'v8/tools/SourceMap',
  60. 'v8/tools/tickprocessor',
  61. 'v8/tools/profile'
  62. ];
  63. for (var i = 0; i < libs.length; ++i) {
  64. var mod = libs[i];
  65. if (blacklist.indexOf(mod) === -1) {
  66. st.ok(resolve.core[mod], mod + ' is a core module');
  67. st.doesNotThrow(
  68. function () { require(mod); }, // eslint-disable-line no-loop-func
  69. 'requiring ' + mod + ' does not throw'
  70. );
  71. }
  72. }
  73. st.end();
  74. });
  75. t.end();
  76. });