Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. 'use strict';
  2. var global = (function () {
  3. if (typeof globalThis !== 'undefined') {
  4. return globalThis;
  5. } else if (typeof global !== 'undefined') {
  6. return global;
  7. } else if (typeof self !== 'undefined') {
  8. return self;
  9. } else if (typeof window !== 'undefined') {
  10. return window;
  11. } else {
  12. return Function('return this')();
  13. }
  14. })();
  15. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  16. var matcherUtils = _interopRequireWildcard(require('jest-matcher-utils'));
  17. var _asymmetricMatchers = require('./asymmetricMatchers');
  18. var _extractExpectedAssertionsErrors = _interopRequireDefault(
  19. require('./extractExpectedAssertionsErrors')
  20. );
  21. var _jasmineUtils = require('./jasmineUtils');
  22. var _jestMatchersObject = require('./jestMatchersObject');
  23. var _matchers = _interopRequireDefault(require('./matchers'));
  24. var _spyMatchers = _interopRequireDefault(require('./spyMatchers'));
  25. var _toThrowMatchers = _interopRequireWildcard(require('./toThrowMatchers'));
  26. var _utils = require('./utils');
  27. function _interopRequireDefault(obj) {
  28. return obj && obj.__esModule ? obj : {default: obj};
  29. }
  30. function _getRequireWildcardCache(nodeInterop) {
  31. if (typeof WeakMap !== 'function') return null;
  32. var cacheBabelInterop = new WeakMap();
  33. var cacheNodeInterop = new WeakMap();
  34. return (_getRequireWildcardCache = function (nodeInterop) {
  35. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  36. })(nodeInterop);
  37. }
  38. function _interopRequireWildcard(obj, nodeInterop) {
  39. if (!nodeInterop && obj && obj.__esModule) {
  40. return obj;
  41. }
  42. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  43. return {default: obj};
  44. }
  45. var cache = _getRequireWildcardCache(nodeInterop);
  46. if (cache && cache.has(obj)) {
  47. return cache.get(obj);
  48. }
  49. var newObj = {};
  50. var hasPropertyDescriptor =
  51. Object.defineProperty && Object.getOwnPropertyDescriptor;
  52. for (var key in obj) {
  53. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  54. var desc = hasPropertyDescriptor
  55. ? Object.getOwnPropertyDescriptor(obj, key)
  56. : null;
  57. if (desc && (desc.get || desc.set)) {
  58. Object.defineProperty(newObj, key, desc);
  59. } else {
  60. newObj[key] = obj[key];
  61. }
  62. }
  63. }
  64. newObj.default = obj;
  65. if (cache) {
  66. cache.set(obj, newObj);
  67. }
  68. return newObj;
  69. }
  70. var global = (function () {
  71. if (typeof globalThis !== 'undefined') {
  72. return globalThis;
  73. } else if (typeof global !== 'undefined') {
  74. return global;
  75. } else if (typeof self !== 'undefined') {
  76. return self;
  77. } else if (typeof window !== 'undefined') {
  78. return window;
  79. } else {
  80. return Function('return this')();
  81. }
  82. })();
  83. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  84. var global = (function () {
  85. if (typeof globalThis !== 'undefined') {
  86. return globalThis;
  87. } else if (typeof global !== 'undefined') {
  88. return global;
  89. } else if (typeof self !== 'undefined') {
  90. return self;
  91. } else if (typeof window !== 'undefined') {
  92. return window;
  93. } else {
  94. return Function('return this')();
  95. }
  96. })();
  97. var Promise = global[Symbol.for('jest-native-promise')] || global.Promise;
  98. function _defineProperty(obj, key, value) {
  99. if (key in obj) {
  100. Object.defineProperty(obj, key, {
  101. value: value,
  102. enumerable: true,
  103. configurable: true,
  104. writable: true
  105. });
  106. } else {
  107. obj[key] = value;
  108. }
  109. return obj;
  110. }
  111. class JestAssertionError extends Error {
  112. constructor(...args) {
  113. super(...args);
  114. _defineProperty(this, 'matcherResult', void 0);
  115. }
  116. }
  117. const isPromise = obj =>
  118. !!obj &&
  119. (typeof obj === 'object' || typeof obj === 'function') &&
  120. typeof obj.then === 'function';
  121. const createToThrowErrorMatchingSnapshotMatcher = function (matcher) {
  122. return function (received, testNameOrInlineSnapshot) {
  123. return matcher.apply(this, [received, testNameOrInlineSnapshot, true]);
  124. };
  125. };
  126. const getPromiseMatcher = (name, matcher) => {
  127. if (name === 'toThrow' || name === 'toThrowError') {
  128. return (0, _toThrowMatchers.createMatcher)(name, true);
  129. } else if (
  130. name === 'toThrowErrorMatchingSnapshot' ||
  131. name === 'toThrowErrorMatchingInlineSnapshot'
  132. ) {
  133. return createToThrowErrorMatchingSnapshotMatcher(matcher);
  134. }
  135. return null;
  136. };
  137. const expect = (actual, ...rest) => {
  138. if (rest.length !== 0) {
  139. throw new Error('Expect takes at most one argument.');
  140. }
  141. const allMatchers = (0, _jestMatchersObject.getMatchers)();
  142. const expectation = {
  143. not: {},
  144. rejects: {
  145. not: {}
  146. },
  147. resolves: {
  148. not: {}
  149. }
  150. };
  151. const err = new JestAssertionError();
  152. Object.keys(allMatchers).forEach(name => {
  153. const matcher = allMatchers[name];
  154. const promiseMatcher = getPromiseMatcher(name, matcher) || matcher;
  155. expectation[name] = makeThrowingMatcher(matcher, false, '', actual);
  156. expectation.not[name] = makeThrowingMatcher(matcher, true, '', actual);
  157. expectation.resolves[name] = makeResolveMatcher(
  158. name,
  159. promiseMatcher,
  160. false,
  161. actual,
  162. err
  163. );
  164. expectation.resolves.not[name] = makeResolveMatcher(
  165. name,
  166. promiseMatcher,
  167. true,
  168. actual,
  169. err
  170. );
  171. expectation.rejects[name] = makeRejectMatcher(
  172. name,
  173. promiseMatcher,
  174. false,
  175. actual,
  176. err
  177. );
  178. expectation.rejects.not[name] = makeRejectMatcher(
  179. name,
  180. promiseMatcher,
  181. true,
  182. actual,
  183. err
  184. );
  185. });
  186. return expectation;
  187. };
  188. const getMessage = message =>
  189. (message && message()) ||
  190. matcherUtils.RECEIVED_COLOR('No message was specified for this matcher.');
  191. const makeResolveMatcher =
  192. (matcherName, matcher, isNot, actual, outerErr) =>
  193. (...args) => {
  194. const options = {
  195. isNot,
  196. promise: 'resolves'
  197. };
  198. if (!isPromise(actual)) {
  199. throw new JestAssertionError(
  200. matcherUtils.matcherErrorMessage(
  201. matcherUtils.matcherHint(matcherName, undefined, '', options),
  202. `${matcherUtils.RECEIVED_COLOR('received')} value must be a promise`,
  203. matcherUtils.printWithType(
  204. 'Received',
  205. actual,
  206. matcherUtils.printReceived
  207. )
  208. )
  209. );
  210. }
  211. const innerErr = new JestAssertionError();
  212. return actual.then(
  213. result =>
  214. makeThrowingMatcher(matcher, isNot, 'resolves', result, innerErr).apply(
  215. null,
  216. args
  217. ),
  218. reason => {
  219. outerErr.message =
  220. matcherUtils.matcherHint(matcherName, undefined, '', options) +
  221. '\n\n' +
  222. `Received promise rejected instead of resolved\n` +
  223. `Rejected to value: ${matcherUtils.printReceived(reason)}`;
  224. return Promise.reject(outerErr);
  225. }
  226. );
  227. };
  228. const makeRejectMatcher =
  229. (matcherName, matcher, isNot, actual, outerErr) =>
  230. (...args) => {
  231. const options = {
  232. isNot,
  233. promise: 'rejects'
  234. };
  235. const actualWrapper = typeof actual === 'function' ? actual() : actual;
  236. if (!isPromise(actualWrapper)) {
  237. throw new JestAssertionError(
  238. matcherUtils.matcherErrorMessage(
  239. matcherUtils.matcherHint(matcherName, undefined, '', options),
  240. `${matcherUtils.RECEIVED_COLOR(
  241. 'received'
  242. )} value must be a promise or a function returning a promise`,
  243. matcherUtils.printWithType(
  244. 'Received',
  245. actual,
  246. matcherUtils.printReceived
  247. )
  248. )
  249. );
  250. }
  251. const innerErr = new JestAssertionError();
  252. return actualWrapper.then(
  253. result => {
  254. outerErr.message =
  255. matcherUtils.matcherHint(matcherName, undefined, '', options) +
  256. '\n\n' +
  257. `Received promise resolved instead of rejected\n` +
  258. `Resolved to value: ${matcherUtils.printReceived(result)}`;
  259. return Promise.reject(outerErr);
  260. },
  261. reason =>
  262. makeThrowingMatcher(matcher, isNot, 'rejects', reason, innerErr).apply(
  263. null,
  264. args
  265. )
  266. );
  267. };
  268. const makeThrowingMatcher = (matcher, isNot, promise, actual, err) =>
  269. function throwingMatcher(...args) {
  270. let throws = true;
  271. const utils = {
  272. ...matcherUtils,
  273. iterableEquality: _utils.iterableEquality,
  274. subsetEquality: _utils.subsetEquality
  275. };
  276. const matcherContext = {
  277. // When throws is disabled, the matcher will not throw errors during test
  278. // execution but instead add them to the global matcher state. If a
  279. // matcher throws, test execution is normally stopped immediately. The
  280. // snapshot matcher uses it because we want to log all snapshot
  281. // failures in a test.
  282. dontThrow: () => (throws = false),
  283. ...(0, _jestMatchersObject.getState)(),
  284. equals: _jasmineUtils.equals,
  285. error: err,
  286. isNot,
  287. promise,
  288. utils
  289. };
  290. const processResult = (result, asyncError) => {
  291. _validateResult(result);
  292. (0, _jestMatchersObject.getState)().assertionCalls++;
  293. if ((result.pass && isNot) || (!result.pass && !isNot)) {
  294. // XOR
  295. const message = getMessage(result.message);
  296. let error;
  297. if (err) {
  298. error = err;
  299. error.message = message;
  300. } else if (asyncError) {
  301. error = asyncError;
  302. error.message = message;
  303. } else {
  304. error = new JestAssertionError(message); // Try to remove this function from the stack trace frame.
  305. // Guard for some environments (browsers) that do not support this feature.
  306. if (Error.captureStackTrace) {
  307. Error.captureStackTrace(error, throwingMatcher);
  308. }
  309. } // Passing the result of the matcher with the error so that a custom
  310. // reporter could access the actual and expected objects of the result
  311. // for example in order to display a custom visual diff
  312. error.matcherResult = {...result, message};
  313. if (throws) {
  314. throw error;
  315. } else {
  316. (0, _jestMatchersObject.getState)().suppressedErrors.push(error);
  317. }
  318. }
  319. };
  320. const handleError = error => {
  321. if (
  322. matcher[_jestMatchersObject.INTERNAL_MATCHER_FLAG] === true &&
  323. !(error instanceof JestAssertionError) &&
  324. error.name !== 'PrettyFormatPluginError' && // Guard for some environments (browsers) that do not support this feature.
  325. Error.captureStackTrace
  326. ) {
  327. // Try to remove this and deeper functions from the stack trace frame.
  328. Error.captureStackTrace(error, throwingMatcher);
  329. }
  330. throw error;
  331. };
  332. let potentialResult;
  333. try {
  334. potentialResult =
  335. matcher[_jestMatchersObject.INTERNAL_MATCHER_FLAG] === true
  336. ? matcher.call(matcherContext, actual, ...args) // It's a trap specifically for inline snapshot to capture this name
  337. : // in the stack trace, so that it can correctly get the custom matcher
  338. // function call.
  339. (function __EXTERNAL_MATCHER_TRAP__() {
  340. return matcher.call(matcherContext, actual, ...args);
  341. })();
  342. if (isPromise(potentialResult)) {
  343. const asyncResult = potentialResult;
  344. const asyncError = new JestAssertionError();
  345. if (Error.captureStackTrace) {
  346. Error.captureStackTrace(asyncError, throwingMatcher);
  347. }
  348. return asyncResult
  349. .then(aResult => processResult(aResult, asyncError))
  350. .catch(handleError);
  351. } else {
  352. const syncResult = potentialResult;
  353. return processResult(syncResult);
  354. }
  355. } catch (error) {
  356. return handleError(error);
  357. }
  358. };
  359. expect.extend = matchers =>
  360. (0, _jestMatchersObject.setMatchers)(matchers, false, expect);
  361. expect.anything = _asymmetricMatchers.anything;
  362. expect.any = _asymmetricMatchers.any;
  363. expect.not = {
  364. arrayContaining: _asymmetricMatchers.arrayNotContaining,
  365. objectContaining: _asymmetricMatchers.objectNotContaining,
  366. stringContaining: _asymmetricMatchers.stringNotContaining,
  367. stringMatching: _asymmetricMatchers.stringNotMatching
  368. };
  369. expect.objectContaining = _asymmetricMatchers.objectContaining;
  370. expect.arrayContaining = _asymmetricMatchers.arrayContaining;
  371. expect.stringContaining = _asymmetricMatchers.stringContaining;
  372. expect.stringMatching = _asymmetricMatchers.stringMatching;
  373. const _validateResult = result => {
  374. if (
  375. typeof result !== 'object' ||
  376. typeof result.pass !== 'boolean' ||
  377. (result.message &&
  378. typeof result.message !== 'string' &&
  379. typeof result.message !== 'function')
  380. ) {
  381. throw new Error(
  382. 'Unexpected return from a matcher function.\n' +
  383. 'Matcher functions should ' +
  384. 'return an object in the following format:\n' +
  385. ' {message?: string | function, pass: boolean}\n' +
  386. `'${matcherUtils.stringify(result)}' was returned`
  387. );
  388. }
  389. };
  390. function assertions(expected) {
  391. const error = new Error();
  392. if (Error.captureStackTrace) {
  393. Error.captureStackTrace(error, assertions);
  394. }
  395. (0, _jestMatchersObject.getState)().expectedAssertionsNumber = expected;
  396. (0, _jestMatchersObject.getState)().expectedAssertionsNumberError = error;
  397. }
  398. function hasAssertions(...args) {
  399. const error = new Error();
  400. if (Error.captureStackTrace) {
  401. Error.captureStackTrace(error, hasAssertions);
  402. }
  403. matcherUtils.ensureNoExpected(args[0], '.hasAssertions');
  404. (0, _jestMatchersObject.getState)().isExpectingAssertions = true;
  405. (0, _jestMatchersObject.getState)().isExpectingAssertionsError = error;
  406. } // add default jest matchers
  407. (0, _jestMatchersObject.setMatchers)(_matchers.default, true, expect);
  408. (0, _jestMatchersObject.setMatchers)(_spyMatchers.default, true, expect);
  409. (0, _jestMatchersObject.setMatchers)(_toThrowMatchers.default, true, expect);
  410. expect.addSnapshotSerializer = () => void 0;
  411. expect.assertions = assertions;
  412. expect.hasAssertions = hasAssertions;
  413. expect.getState = _jestMatchersObject.getState;
  414. expect.setState = _jestMatchersObject.setState;
  415. expect.extractExpectedAssertionsErrors =
  416. _extractExpectedAssertionsErrors.default;
  417. const expectExport = expect;
  418. module.exports = expectExport;