poolBuilder.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.PoolBuilder = void 0;
  6. var _fixtures = require("./fixtures");
  7. var _util = require("../util");
  8. /**
  9. * Copyright Microsoft Corporation. All rights reserved.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. class PoolBuilder {
  24. static createForLoader() {
  25. return new PoolBuilder('loader');
  26. }
  27. static createForWorker(project) {
  28. return new PoolBuilder('worker', project);
  29. }
  30. constructor(type, project) {
  31. this._project = void 0;
  32. this._testTypePools = new Map();
  33. this._type = void 0;
  34. this._type = type;
  35. this._project = project;
  36. }
  37. buildPools(suite, testErrors) {
  38. suite.forEachTest(test => {
  39. const pool = this._buildPoolForTest(test, testErrors);
  40. if (this._type === 'loader') test._poolDigest = pool.digest;
  41. if (this._type === 'worker') test._pool = pool;
  42. });
  43. }
  44. _buildPoolForTest(test, testErrors) {
  45. let pool = this._buildTestTypePool(test._testType, testErrors);
  46. const parents = [];
  47. for (let parent = test.parent; parent; parent = parent.parent) parents.push(parent);
  48. parents.reverse();
  49. for (const parent of parents) {
  50. if (parent._use.length) pool = new _fixtures.FixturePool(parent._use, e => this._handleLoadError(e, testErrors), pool, parent._type === 'describe');
  51. for (const hook of parent._hooks) pool.validateFunction(hook.fn, hook.type + ' hook', hook.location);
  52. for (const modifier of parent._modifiers) pool.validateFunction(modifier.fn, modifier.type + ' modifier', modifier.location);
  53. }
  54. pool.validateFunction(test.fn, 'Test', test.location);
  55. return pool;
  56. }
  57. _buildTestTypePool(testType, testErrors) {
  58. if (!this._testTypePools.has(testType)) {
  59. var _this$_project$projec, _this$_project, _this$_project$projec2, _this$_project2;
  60. const optionOverrides = {
  61. overrides: (_this$_project$projec = (_this$_project = this._project) === null || _this$_project === void 0 ? void 0 : (_this$_project$projec2 = _this$_project.project) === null || _this$_project$projec2 === void 0 ? void 0 : _this$_project$projec2.use) !== null && _this$_project$projec !== void 0 ? _this$_project$projec : {},
  62. location: {
  63. file: `project#${(_this$_project2 = this._project) === null || _this$_project2 === void 0 ? void 0 : _this$_project2.id}`,
  64. line: 1,
  65. column: 1
  66. }
  67. };
  68. const pool = new _fixtures.FixturePool(testType.fixtures, e => this._handleLoadError(e, testErrors), undefined, undefined, optionOverrides);
  69. this._testTypePools.set(testType, pool);
  70. }
  71. return this._testTypePools.get(testType);
  72. }
  73. _handleLoadError(e, testErrors) {
  74. if (testErrors) testErrors.push(e);else throw new Error(`${(0, _util.formatLocation)(e.location)}: ${e.message}`);
  75. }
  76. }
  77. exports.PoolBuilder = PoolBuilder;