UNPKG

7.35 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = jasmine2;
7
8var path = _interopRequireWildcard(require('path'));
9
10var _sourceMap = require('@jest/source-map');
11
12var _each = _interopRequireDefault(require('./each'));
13
14var _errorOnPrivate = require('./errorOnPrivate');
15
16var _jasmineAsyncInstall = _interopRequireDefault(
17 require('./jasmineAsyncInstall')
18);
19
20var _reporter = _interopRequireDefault(require('./reporter'));
21
22function _interopRequireDefault(obj) {
23 return obj && obj.__esModule ? obj : {default: obj};
24}
25
26function _getRequireWildcardCache(nodeInterop) {
27 if (typeof WeakMap !== 'function') return null;
28 var cacheBabelInterop = new WeakMap();
29 var cacheNodeInterop = new WeakMap();
30 return (_getRequireWildcardCache = function (nodeInterop) {
31 return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
32 })(nodeInterop);
33}
34
35function _interopRequireWildcard(obj, nodeInterop) {
36 if (!nodeInterop && obj && obj.__esModule) {
37 return obj;
38 }
39 if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
40 return {default: obj};
41 }
42 var cache = _getRequireWildcardCache(nodeInterop);
43 if (cache && cache.has(obj)) {
44 return cache.get(obj);
45 }
46 var newObj = {};
47 var hasPropertyDescriptor =
48 Object.defineProperty && Object.getOwnPropertyDescriptor;
49 for (var key in obj) {
50 if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
51 var desc = hasPropertyDescriptor
52 ? Object.getOwnPropertyDescriptor(obj, key)
53 : null;
54 if (desc && (desc.get || desc.set)) {
55 Object.defineProperty(newObj, key, desc);
56 } else {
57 newObj[key] = obj[key];
58 }
59 }
60 }
61 newObj.default = obj;
62 if (cache) {
63 cache.set(obj, newObj);
64 }
65 return newObj;
66}
67
68/**
69 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
70 *
71 * This source code is licensed under the MIT license found in the
72 * LICENSE file in the root directory of this source tree.
73 */
74const JASMINE = require.resolve('./jasmine/jasmineLight');
75
76const jestEachBuildDir = path.dirname(require.resolve('jest-each'));
77
78async function jasmine2(globalConfig, config, environment, runtime, testPath) {
79 const reporter = new _reporter.default(globalConfig, config, testPath);
80 const jasmineFactory = runtime.requireInternalModule(JASMINE);
81 const jasmine = jasmineFactory.create({
82 process,
83 testPath,
84 testTimeout: globalConfig.testTimeout
85 });
86 const env = jasmine.getEnv();
87
88 const jasmineInterface = jasmineFactory._interface(jasmine, env);
89
90 Object.assign(environment.global, jasmineInterface);
91 env.addReporter(jasmineInterface.jsApiReporter); // TODO: Remove config option if V8 exposes some way of getting location of caller
92 // in a future version
93
94 if (config.testLocationInResults === true) {
95 function wrapIt(original) {
96 const wrapped = (testName, fn, timeout) => {
97 var _stack$getFileName;
98
99 const sourcemaps = runtime.getSourceMaps();
100 let stack = (0, _sourceMap.getCallsite)(1, sourcemaps);
101 const it = original(testName, fn, timeout);
102
103 if (
104 (_stack$getFileName = stack.getFileName()) !== null &&
105 _stack$getFileName !== void 0 &&
106 _stack$getFileName.startsWith(jestEachBuildDir)
107 ) {
108 stack = (0, _sourceMap.getCallsite)(4, sourcemaps);
109 } // @ts-expect-error
110
111 it.result.__callsite = stack;
112 return it;
113 };
114
115 return wrapped;
116 }
117
118 environment.global.it = wrapIt(environment.global.it);
119 environment.global.xit = wrapIt(environment.global.xit);
120 environment.global.fit = wrapIt(environment.global.fit);
121 }
122
123 (0, _jasmineAsyncInstall.default)(globalConfig, environment.global);
124 (0, _each.default)(environment);
125 environment.global.test = environment.global.it;
126 environment.global.it.only = environment.global.fit;
127 environment.global.it.todo = env.todo;
128 environment.global.it.skip = environment.global.xit;
129 environment.global.xtest = environment.global.xit;
130 environment.global.describe.skip = environment.global.xdescribe;
131 environment.global.describe.only = environment.global.fdescribe;
132
133 if (config.timers === 'fake' || config.timers === 'modern') {
134 environment.fakeTimersModern.useFakeTimers();
135 } else if (config.timers === 'legacy') {
136 environment.fakeTimers.useFakeTimers();
137 }
138
139 env.beforeEach(() => {
140 if (config.resetModules) {
141 runtime.resetModules();
142 }
143
144 if (config.clearMocks) {
145 runtime.clearAllMocks();
146 }
147
148 if (config.resetMocks) {
149 runtime.resetAllMocks();
150
151 if (config.timers === 'legacy') {
152 environment.fakeTimers.useFakeTimers();
153 }
154 }
155
156 if (config.restoreMocks) {
157 runtime.restoreAllMocks();
158 }
159 });
160 env.addReporter(reporter);
161 runtime
162 .requireInternalModule(path.resolve(__dirname, './jestExpect.js'))
163 .default({
164 expand: globalConfig.expand
165 });
166
167 if (globalConfig.errorOnDeprecated) {
168 (0, _errorOnPrivate.installErrorOnPrivate)(environment.global);
169 } else {
170 Object.defineProperty(jasmine, 'DEFAULT_TIMEOUT_INTERVAL', {
171 configurable: true,
172 enumerable: true,
173
174 get() {
175 return this._DEFAULT_TIMEOUT_INTERVAL;
176 },
177
178 set(value) {
179 this._DEFAULT_TIMEOUT_INTERVAL = value;
180 }
181 });
182 }
183
184 const snapshotState = await runtime
185 .requireInternalModule(path.resolve(__dirname, './setup_jest_globals.js'))
186 .default({
187 config,
188 globalConfig,
189 localRequire: runtime.requireModule.bind(runtime),
190 testPath
191 });
192
193 for (const path of config.setupFilesAfterEnv) {
194 const esm = runtime.unstable_shouldLoadAsEsm(path);
195
196 if (esm) {
197 await runtime.unstable_importModule(path);
198 } else {
199 runtime.requireModule(path);
200 }
201 }
202
203 if (globalConfig.testNamePattern) {
204 const testNameRegex = new RegExp(globalConfig.testNamePattern, 'i');
205
206 env.specFilter = spec => testNameRegex.test(spec.getFullName());
207 }
208
209 const esm = runtime.unstable_shouldLoadAsEsm(testPath);
210
211 if (esm) {
212 await runtime.unstable_importModule(testPath);
213 } else {
214 runtime.requireModule(testPath);
215 }
216
217 await env.execute();
218 const results = await reporter.getResults();
219 return addSnapshotData(results, snapshotState);
220}
221
222const addSnapshotData = (results, snapshotState) => {
223 results.testResults.forEach(({fullName, status}) => {
224 if (status === 'pending' || status === 'failed') {
225 // if test is skipped or failed, we don't want to mark
226 // its snapshots as obsolete.
227 snapshotState.markSnapshotsAsCheckedForTest(fullName);
228 }
229 });
230 const uncheckedCount = snapshotState.getUncheckedCount();
231 const uncheckedKeys = snapshotState.getUncheckedKeys();
232
233 if (uncheckedCount) {
234 snapshotState.removeUncheckedKeys();
235 }
236
237 const status = snapshotState.save();
238 results.snapshot.fileDeleted = status.deleted;
239 results.snapshot.added = snapshotState.added;
240 results.snapshot.matched = snapshotState.matched;
241 results.snapshot.unmatched = snapshotState.unmatched;
242 results.snapshot.updated = snapshotState.updated;
243 results.snapshot.unchecked = !status.deleted ? uncheckedCount : 0; // Copy the array to prevent memory leaks
244
245 results.snapshot.uncheckedKeys = Array.from(uncheckedKeys);
246 return results;
247};