testLoader.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.defaultTimeout = void 0;
  6. exports.loadTestFile = loadTestFile;
  7. var _path = _interopRequireDefault(require("path"));
  8. var _util = _interopRequireDefault(require("util"));
  9. var _globals = require("./globals");
  10. var _test = require("./test");
  11. var _transform = require("../transform/transform");
  12. var _util2 = require("../util");
  13. var _compilationCache = require("../transform/compilationCache");
  14. var esmLoaderHost = _interopRequireWildcard(require("./esmLoaderHost"));
  15. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  16. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  18. /**
  19. * Copyright Microsoft Corporation. All rights reserved.
  20. *
  21. * Licensed under the Apache License, Version 2.0 (the "License");
  22. * you may not use this file except in compliance with the License.
  23. * You may obtain a copy of the License at
  24. *
  25. * http://www.apache.org/licenses/LICENSE-2.0
  26. *
  27. * Unless required by applicable law or agreed to in writing, software
  28. * distributed under the License is distributed on an "AS IS" BASIS,
  29. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  30. * See the License for the specific language governing permissions and
  31. * limitations under the License.
  32. */
  33. const defaultTimeout = exports.defaultTimeout = 30000;
  34. // To allow multiple loaders in the same process without clearing require cache,
  35. // we make these maps global.
  36. const cachedFileSuites = new Map();
  37. async function loadTestFile(file, rootDir, testErrors) {
  38. if (cachedFileSuites.has(file)) return cachedFileSuites.get(file);
  39. const suite = new _test.Suite(_path.default.relative(rootDir, file) || _path.default.basename(file), 'file');
  40. suite._requireFile = file;
  41. suite.location = {
  42. file,
  43. line: 0,
  44. column: 0
  45. };
  46. (0, _globals.setCurrentlyLoadingFileSuite)(suite);
  47. if (!(0, _globals.isWorkerProcess)()) {
  48. (0, _compilationCache.startCollectingFileDeps)();
  49. await esmLoaderHost.startCollectingFileDeps();
  50. }
  51. try {
  52. await (0, _transform.requireOrImport)(file);
  53. cachedFileSuites.set(file, suite);
  54. } catch (e) {
  55. if (!testErrors) throw e;
  56. testErrors.push(serializeLoadError(file, e));
  57. } finally {
  58. (0, _globals.setCurrentlyLoadingFileSuite)(undefined);
  59. if (!(0, _globals.isWorkerProcess)()) {
  60. (0, _compilationCache.stopCollectingFileDeps)(file);
  61. await esmLoaderHost.stopCollectingFileDeps(file);
  62. }
  63. }
  64. {
  65. // Test locations that we discover potentially have different file name.
  66. // This could be due to either
  67. // a) use of source maps or due to
  68. // b) require of one file from another.
  69. // Try fixing (a) w/o regressing (b).
  70. const files = new Set();
  71. suite.allTests().map(t => files.add(t.location.file));
  72. if (files.size === 1) {
  73. // All tests point to one file.
  74. const mappedFile = files.values().next().value;
  75. if (suite.location.file !== mappedFile) {
  76. // The file is different, check for a likely source map case.
  77. if (_path.default.extname(mappedFile) !== _path.default.extname(suite.location.file)) suite.location.file = mappedFile;
  78. }
  79. }
  80. }
  81. return suite;
  82. }
  83. function serializeLoadError(file, error) {
  84. if (error instanceof Error) {
  85. const result = (0, _util2.filterStackTrace)(error);
  86. // Babel parse errors have location.
  87. const loc = error.loc;
  88. result.location = loc ? {
  89. file,
  90. line: loc.line || 0,
  91. column: loc.column || 0
  92. } : undefined;
  93. return result;
  94. }
  95. return {
  96. value: _util.default.inspect(error)
  97. };
  98. }