index.js 7.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
'use strict';

var path = _interopRequireWildcard(require('path'));

var _sourceMap = require('@jest/source-map');

var _each = _interopRequireDefault(require('./each'));

var _errorOnPrivate = require('./errorOnPrivate');

var _reporter = _interopRequireDefault(require('./reporter'));

var _jasmineAsyncInstall = _interopRequireDefault(
  require('./jasmineAsyncInstall')
);

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : {default: obj};
}

function _getRequireWildcardCache() {
  if (typeof WeakMap !== 'function') return null;
  var cache = new WeakMap();
  _getRequireWildcardCache = function () {
    return cache;
  };
  return cache;
}

function _interopRequireWildcard(obj) {
  if (obj && obj.__esModule) {
    return obj;
  }
  if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
    return {default: obj};
  }
  var cache = _getRequireWildcardCache();
  if (cache && cache.has(obj)) {
    return cache.get(obj);
  }
  var newObj = {};
  var hasPropertyDescriptor =
    Object.defineProperty && Object.getOwnPropertyDescriptor;
  for (var key in obj) {
    if (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;
}

/**
 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
const JASMINE = require.resolve('./jasmine/jasmineLight');

async function jasmine2(globalConfig, config, environment, runtime, testPath) {
  var _runtime$unstable_sho2;

  const reporter = new _reporter.default(globalConfig, config, testPath);
  const jasmineFactory = runtime.requireInternalModule(JASMINE);
  const jasmine = jasmineFactory.create({
    process,
    testPath,
    testTimeout: globalConfig.testTimeout
  });
  const env = jasmine.getEnv();

  const jasmineInterface = jasmineFactory._interface(jasmine, env);

  Object.assign(environment.global, jasmineInterface);
  env.addReporter(jasmineInterface.jsApiReporter); // TODO: Remove config option if V8 exposes some way of getting location of caller
  // in a future version

  if (config.testLocationInResults === true) {
    const originalIt = environment.global.it;

    environment.global.it = (...args) => {
      const stack = (0, _sourceMap.getCallsite)(1, runtime.getSourceMaps());
      const it = originalIt(...args); // @ts-ignore

      it.result.__callsite = stack;
      return it;
    };

    const originalXit = environment.global.xit;

    environment.global.xit = (...args) => {
      const stack = (0, _sourceMap.getCallsite)(1, runtime.getSourceMaps());
      const xit = originalXit(...args); // @ts-ignore

      xit.result.__callsite = stack;
      return xit;
    };

    const originalFit = environment.global.fit;

    environment.global.fit = (...args) => {
      const stack = (0, _sourceMap.getCallsite)(1, runtime.getSourceMaps());
      const fit = originalFit(...args); // @ts-ignore

      fit.result.__callsite = stack;
      return fit;
    };
  }

  (0, _jasmineAsyncInstall.default)(globalConfig, environment.global);
  (0, _each.default)(environment);
  environment.global.test = environment.global.it;
  environment.global.it.only = environment.global.fit;
  environment.global.it.todo = env.todo;
  environment.global.it.skip = environment.global.xit;
  environment.global.xtest = environment.global.xit;
  environment.global.describe.skip = environment.global.xdescribe;
  environment.global.describe.only = environment.global.fdescribe;

  if (config.timers === 'fake') {
    environment.fakeTimers.useFakeTimers();
  }

  env.beforeEach(() => {
    if (config.resetModules) {
      runtime.resetModules();
    }

    if (config.clearMocks) {
      runtime.clearAllMocks();
    }

    if (config.resetMocks) {
      runtime.resetAllMocks();

      if (config.timers === 'fake') {
        environment.fakeTimers.useFakeTimers();
      }
    }

    if (config.restoreMocks) {
      runtime.restoreAllMocks();
    }
  });
  env.addReporter(reporter);
  runtime
    .requireInternalModule(path.resolve(__dirname, './jestExpect.js'))
    .default({
      expand: globalConfig.expand
    });

  if (globalConfig.errorOnDeprecated) {
    (0, _errorOnPrivate.installErrorOnPrivate)(environment.global);
  } else {
    Object.defineProperty(jasmine, 'DEFAULT_TIMEOUT_INTERVAL', {
      configurable: true,
      enumerable: true,

      get() {
        return this._DEFAULT_TIMEOUT_INTERVAL;
      },

      set(value) {
        this._DEFAULT_TIMEOUT_INTERVAL = value;
      }
    });
  }

  const snapshotState = runtime
    .requireInternalModule(path.resolve(__dirname, './setup_jest_globals.js'))
    .default({
      config,
      globalConfig,
      localRequire: runtime.requireModule.bind(runtime),
      testPath
    });

  for (const path of config.setupFilesAfterEnv) {
    var _runtime$unstable_sho;

    // TODO: remove ? in Jest 26
    const esm =
      (_runtime$unstable_sho = runtime.unstable_shouldLoadAsEsm) === null ||
      _runtime$unstable_sho === void 0
        ? void 0
        : _runtime$unstable_sho.call(runtime, path);

    if (esm) {
      await runtime.unstable_importModule(path);
    } else {
      runtime.requireModule(path);
    }
  }

  if (globalConfig.enabledTestsMap) {
    env.specFilter = spec => {
      const suiteMap =
        globalConfig.enabledTestsMap &&
        globalConfig.enabledTestsMap[spec.result.testPath];
      return (suiteMap && suiteMap[spec.result.fullName]) || false;
    };
  } else if (globalConfig.testNamePattern) {
    const testNameRegex = new RegExp(globalConfig.testNamePattern, 'i');

    env.specFilter = spec => testNameRegex.test(spec.getFullName());
  } // TODO: remove ? in Jest 26

  const esm =
    (_runtime$unstable_sho2 = runtime.unstable_shouldLoadAsEsm) === null ||
    _runtime$unstable_sho2 === void 0
      ? void 0
      : _runtime$unstable_sho2.call(runtime, testPath);

  if (esm) {
    await runtime.unstable_importModule(testPath);
  } else {
    runtime.requireModule(testPath);
  }

  await env.execute();
  const results = await reporter.getResults();
  return addSnapshotData(results, snapshotState);
}

const addSnapshotData = (results, snapshotState) => {
  results.testResults.forEach(({fullName, status}) => {
    if (status === 'pending' || status === 'failed') {
      // if test is skipped or failed, we don't want to mark
      // its snapshots as obsolete.
      snapshotState.markSnapshotsAsCheckedForTest(fullName);
    }
  });
  const uncheckedCount = snapshotState.getUncheckedCount();
  const uncheckedKeys = snapshotState.getUncheckedKeys();

  if (uncheckedCount) {
    snapshotState.removeUncheckedKeys();
  }

  const status = snapshotState.save();
  results.snapshot.fileDeleted = status.deleted;
  results.snapshot.added = snapshotState.added;
  results.snapshot.matched = snapshotState.matched;
  results.snapshot.unmatched = snapshotState.unmatched;
  results.snapshot.updated = snapshotState.updated;
  results.snapshot.unchecked = !status.deleted ? uncheckedCount : 0; // Copy the array to prevent memory leaks

  results.snapshot.uncheckedKeys = Array.from(uncheckedKeys);
  return results;
}; // eslint-disable-next-line no-redeclare

module.exports = jasmine2;