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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. 'use strict';
  2. var fs = _interopRequireWildcard(require('graceful-fs'));
  3. var _jestMatcherUtils = require('jest-matcher-utils');
  4. var _SnapshotResolver = require('./SnapshotResolver');
  5. var _State = _interopRequireDefault(require('./State'));
  6. var _plugins = require('./plugins');
  7. var _printSnapshot = require('./printSnapshot');
  8. var utils = _interopRequireWildcard(require('./utils'));
  9. function _interopRequireDefault(obj) {
  10. return obj && obj.__esModule ? obj : {default: obj};
  11. }
  12. function _getRequireWildcardCache(nodeInterop) {
  13. if (typeof WeakMap !== 'function') return null;
  14. var cacheBabelInterop = new WeakMap();
  15. var cacheNodeInterop = new WeakMap();
  16. return (_getRequireWildcardCache = function (nodeInterop) {
  17. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  18. })(nodeInterop);
  19. }
  20. function _interopRequireWildcard(obj, nodeInterop) {
  21. if (!nodeInterop && obj && obj.__esModule) {
  22. return obj;
  23. }
  24. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  25. return {default: obj};
  26. }
  27. var cache = _getRequireWildcardCache(nodeInterop);
  28. if (cache && cache.has(obj)) {
  29. return cache.get(obj);
  30. }
  31. var newObj = {};
  32. var hasPropertyDescriptor =
  33. Object.defineProperty && Object.getOwnPropertyDescriptor;
  34. for (var key in obj) {
  35. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  36. var desc = hasPropertyDescriptor
  37. ? Object.getOwnPropertyDescriptor(obj, key)
  38. : null;
  39. if (desc && (desc.get || desc.set)) {
  40. Object.defineProperty(newObj, key, desc);
  41. } else {
  42. newObj[key] = obj[key];
  43. }
  44. }
  45. }
  46. newObj.default = obj;
  47. if (cache) {
  48. cache.set(obj, newObj);
  49. }
  50. return newObj;
  51. }
  52. var global = (function () {
  53. if (typeof globalThis !== 'undefined') {
  54. return globalThis;
  55. } else if (typeof global !== 'undefined') {
  56. return global;
  57. } else if (typeof self !== 'undefined') {
  58. return self;
  59. } else if (typeof window !== 'undefined') {
  60. return window;
  61. } else {
  62. return Function('return this')();
  63. }
  64. })();
  65. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  66. var global = (function () {
  67. if (typeof globalThis !== 'undefined') {
  68. return globalThis;
  69. } else if (typeof global !== 'undefined') {
  70. return global;
  71. } else if (typeof self !== 'undefined') {
  72. return self;
  73. } else if (typeof window !== 'undefined') {
  74. return window;
  75. } else {
  76. return Function('return this')();
  77. }
  78. })();
  79. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  80. var global = (function () {
  81. if (typeof globalThis !== 'undefined') {
  82. return globalThis;
  83. } else if (typeof global !== 'undefined') {
  84. return global;
  85. } else if (typeof self !== 'undefined') {
  86. return self;
  87. } else if (typeof window !== 'undefined') {
  88. return window;
  89. } else {
  90. return Function('return this')();
  91. }
  92. })();
  93. var jestExistsFile =
  94. global[Symbol.for('jest-native-exists-file')] || fs.existsSync;
  95. const DID_NOT_THROW = 'Received function did not throw'; // same as toThrow
  96. const NOT_SNAPSHOT_MATCHERS = `Snapshot matchers cannot be used with ${(0,
  97. _jestMatcherUtils.BOLD_WEIGHT)('not')}`;
  98. const INDENTATION_REGEX = /^([^\S\n]*)\S/m; // Display name in report when matcher fails same as in snapshot file,
  99. // but with optional hint argument in bold weight.
  100. const printSnapshotName = (concatenatedBlockNames = '', hint = '', count) => {
  101. const hasNames = concatenatedBlockNames.length !== 0;
  102. const hasHint = hint.length !== 0;
  103. return (
  104. 'Snapshot name: `' +
  105. (hasNames ? utils.escapeBacktickString(concatenatedBlockNames) : '') +
  106. (hasNames && hasHint ? ': ' : '') +
  107. (hasHint
  108. ? (0, _jestMatcherUtils.BOLD_WEIGHT)(utils.escapeBacktickString(hint))
  109. : '') +
  110. ' ' +
  111. count +
  112. '`'
  113. );
  114. };
  115. function stripAddedIndentation(inlineSnapshot) {
  116. // Find indentation if exists.
  117. const match = inlineSnapshot.match(INDENTATION_REGEX);
  118. if (!match || !match[1]) {
  119. // No indentation.
  120. return inlineSnapshot;
  121. }
  122. const indentation = match[1];
  123. const lines = inlineSnapshot.split('\n');
  124. if (lines.length <= 2) {
  125. // Must be at least 3 lines.
  126. return inlineSnapshot;
  127. }
  128. if (lines[0].trim() !== '' || lines[lines.length - 1].trim() !== '') {
  129. // If not blank first and last lines, abort.
  130. return inlineSnapshot;
  131. }
  132. for (let i = 1; i < lines.length - 1; i++) {
  133. if (lines[i] !== '') {
  134. if (lines[i].indexOf(indentation) !== 0) {
  135. // All lines except first and last should either be blank or have the same
  136. // indent as the first line (or more). If this isn't the case we don't
  137. // want to touch the snapshot at all.
  138. return inlineSnapshot;
  139. }
  140. lines[i] = lines[i].substr(indentation.length);
  141. }
  142. } // Last line is a special case because it won't have the same indent as others
  143. // but may still have been given some indent to line up.
  144. lines[lines.length - 1] = ''; // Return inline snapshot, now at indent 0.
  145. inlineSnapshot = lines.join('\n');
  146. return inlineSnapshot;
  147. }
  148. const fileExists = (filePath, hasteFS) =>
  149. hasteFS.exists(filePath) || jestExistsFile(filePath);
  150. const cleanup = (hasteFS, update, snapshotResolver, testPathIgnorePatterns) => {
  151. const pattern = '\\.' + _SnapshotResolver.EXTENSION + '$';
  152. const files = hasteFS.matchFiles(pattern);
  153. let testIgnorePatternsRegex = null;
  154. if (testPathIgnorePatterns && testPathIgnorePatterns.length > 0) {
  155. testIgnorePatternsRegex = new RegExp(testPathIgnorePatterns.join('|'));
  156. }
  157. const list = files.filter(snapshotFile => {
  158. const testPath = snapshotResolver.resolveTestPath(snapshotFile); // ignore snapshots of ignored tests
  159. if (testIgnorePatternsRegex && testIgnorePatternsRegex.test(testPath)) {
  160. return false;
  161. }
  162. if (!fileExists(testPath, hasteFS)) {
  163. if (update === 'all') {
  164. fs.unlinkSync(snapshotFile);
  165. }
  166. return true;
  167. }
  168. return false;
  169. });
  170. return {
  171. filesRemoved: list.length,
  172. filesRemovedList: list
  173. };
  174. };
  175. const toMatchSnapshot = function (received, propertiesOrHint, hint) {
  176. const matcherName = 'toMatchSnapshot';
  177. let properties;
  178. const length = arguments.length;
  179. if (length === 2 && typeof propertiesOrHint === 'string') {
  180. hint = propertiesOrHint;
  181. } else if (length >= 2) {
  182. if (typeof propertiesOrHint !== 'object' || propertiesOrHint === null) {
  183. const options = {
  184. isNot: this.isNot,
  185. promise: this.promise
  186. };
  187. let printedWithType = (0, _jestMatcherUtils.printWithType)(
  188. 'Expected properties',
  189. propertiesOrHint,
  190. _printSnapshot.printExpected
  191. );
  192. if (length === 3) {
  193. options.secondArgument = 'hint';
  194. options.secondArgumentColor = _jestMatcherUtils.BOLD_WEIGHT;
  195. if (propertiesOrHint == null) {
  196. printedWithType += `\n\nTo provide a hint without properties: toMatchSnapshot('hint')`;
  197. }
  198. }
  199. throw new Error(
  200. (0, _jestMatcherUtils.matcherErrorMessage)(
  201. (0, _jestMatcherUtils.matcherHint)(
  202. matcherName,
  203. undefined,
  204. _printSnapshot.PROPERTIES_ARG,
  205. options
  206. ),
  207. `Expected ${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  208. 'properties'
  209. )} must be an object`,
  210. printedWithType
  211. )
  212. );
  213. } // Future breaking change: Snapshot hint must be a string
  214. // if (arguments.length === 3 && typeof hint !== 'string') {}
  215. properties = propertiesOrHint;
  216. }
  217. return _toMatchSnapshot({
  218. context: this,
  219. hint,
  220. isInline: false,
  221. matcherName,
  222. properties,
  223. received
  224. });
  225. };
  226. const toMatchInlineSnapshot = function (
  227. received,
  228. propertiesOrSnapshot,
  229. inlineSnapshot
  230. ) {
  231. const matcherName = 'toMatchInlineSnapshot';
  232. let properties;
  233. const length = arguments.length;
  234. if (length === 2 && typeof propertiesOrSnapshot === 'string') {
  235. inlineSnapshot = propertiesOrSnapshot;
  236. } else if (length >= 2) {
  237. const options = {
  238. isNot: this.isNot,
  239. promise: this.promise
  240. };
  241. if (length === 3) {
  242. options.secondArgument = _printSnapshot.SNAPSHOT_ARG;
  243. options.secondArgumentColor = _printSnapshot.noColor;
  244. }
  245. if (
  246. typeof propertiesOrSnapshot !== 'object' ||
  247. propertiesOrSnapshot === null
  248. ) {
  249. throw new Error(
  250. (0, _jestMatcherUtils.matcherErrorMessage)(
  251. (0, _jestMatcherUtils.matcherHint)(
  252. matcherName,
  253. undefined,
  254. _printSnapshot.PROPERTIES_ARG,
  255. options
  256. ),
  257. `Expected ${(0, _jestMatcherUtils.EXPECTED_COLOR)(
  258. 'properties'
  259. )} must be an object`,
  260. (0, _jestMatcherUtils.printWithType)(
  261. 'Expected properties',
  262. propertiesOrSnapshot,
  263. _printSnapshot.printExpected
  264. )
  265. )
  266. );
  267. }
  268. if (length === 3 && typeof inlineSnapshot !== 'string') {
  269. throw new Error(
  270. (0, _jestMatcherUtils.matcherErrorMessage)(
  271. (0, _jestMatcherUtils.matcherHint)(
  272. matcherName,
  273. undefined,
  274. _printSnapshot.PROPERTIES_ARG,
  275. options
  276. ),
  277. `Inline snapshot must be a string`,
  278. (0, _jestMatcherUtils.printWithType)(
  279. 'Inline snapshot',
  280. inlineSnapshot,
  281. utils.serialize
  282. )
  283. )
  284. );
  285. }
  286. properties = propertiesOrSnapshot;
  287. }
  288. return _toMatchSnapshot({
  289. context: this,
  290. inlineSnapshot:
  291. inlineSnapshot !== undefined
  292. ? stripAddedIndentation(inlineSnapshot)
  293. : undefined,
  294. isInline: true,
  295. matcherName,
  296. properties,
  297. received
  298. });
  299. };
  300. const _toMatchSnapshot = config => {
  301. const {context, hint, inlineSnapshot, isInline, matcherName, properties} =
  302. config;
  303. let {received} = config;
  304. context.dontThrow && context.dontThrow();
  305. const {currentTestName, isNot, snapshotState} = context;
  306. if (isNot) {
  307. throw new Error(
  308. (0, _jestMatcherUtils.matcherErrorMessage)(
  309. (0, _printSnapshot.matcherHintFromConfig)(config, false),
  310. NOT_SNAPSHOT_MATCHERS
  311. )
  312. );
  313. }
  314. if (snapshotState == null) {
  315. // Because the state is the problem, this is not a matcher error.
  316. // Call generic stringify from jest-matcher-utils package
  317. // because uninitialized snapshot state does not need snapshot serializers.
  318. throw new Error(
  319. (0, _printSnapshot.matcherHintFromConfig)(config, false) +
  320. '\n\n' +
  321. `Snapshot state must be initialized` +
  322. '\n\n' +
  323. (0, _jestMatcherUtils.printWithType)(
  324. 'Snapshot state',
  325. snapshotState,
  326. _jestMatcherUtils.stringify
  327. )
  328. );
  329. }
  330. const fullTestName =
  331. currentTestName && hint
  332. ? `${currentTestName}: ${hint}`
  333. : currentTestName || ''; // future BREAKING change: || hint
  334. if (typeof properties === 'object') {
  335. if (typeof received !== 'object' || received === null) {
  336. throw new Error(
  337. (0, _jestMatcherUtils.matcherErrorMessage)(
  338. (0, _printSnapshot.matcherHintFromConfig)(config, false),
  339. `${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  340. 'received'
  341. )} value must be an object when the matcher has ${(0,
  342. _jestMatcherUtils.EXPECTED_COLOR)('properties')}`,
  343. (0, _jestMatcherUtils.printWithType)(
  344. 'Received',
  345. received,
  346. _printSnapshot.printReceived
  347. )
  348. )
  349. );
  350. }
  351. const propertyPass = context.equals(received, properties, [
  352. context.utils.iterableEquality,
  353. context.utils.subsetEquality
  354. ]);
  355. if (!propertyPass) {
  356. const key = snapshotState.fail(fullTestName, received);
  357. const matched = /(\d+)$/.exec(key);
  358. const count = matched === null ? 1 : Number(matched[1]);
  359. const message = () =>
  360. (0, _printSnapshot.matcherHintFromConfig)(config, false) +
  361. '\n\n' +
  362. printSnapshotName(currentTestName, hint, count) +
  363. '\n\n' +
  364. (0, _printSnapshot.printPropertiesAndReceived)(
  365. properties,
  366. received,
  367. snapshotState.expand
  368. );
  369. return {
  370. message,
  371. name: matcherName,
  372. pass: false
  373. };
  374. } else {
  375. received = utils.deepMerge(received, properties);
  376. }
  377. }
  378. const result = snapshotState.match({
  379. error: context.error,
  380. inlineSnapshot,
  381. isInline,
  382. received,
  383. testName: fullTestName
  384. });
  385. const {actual, count, expected, pass} = result;
  386. if (pass) {
  387. return {
  388. message: () => '',
  389. pass: true
  390. };
  391. }
  392. const message =
  393. expected === undefined
  394. ? () =>
  395. (0, _printSnapshot.matcherHintFromConfig)(config, true) +
  396. '\n\n' +
  397. printSnapshotName(currentTestName, hint, count) +
  398. '\n\n' +
  399. `New snapshot was ${(0, _jestMatcherUtils.BOLD_WEIGHT)(
  400. 'not written'
  401. )}. The update flag ` +
  402. `must be explicitly passed to write a new snapshot.\n\n` +
  403. `This is likely because this test is run in a continuous integration ` +
  404. `(CI) environment in which snapshots are not written by default.\n\n` +
  405. `Received:${actual.includes('\n') ? '\n' : ' '}${(0,
  406. _printSnapshot.bReceivedColor)(actual)}`
  407. : () =>
  408. (0, _printSnapshot.matcherHintFromConfig)(config, true) +
  409. '\n\n' +
  410. printSnapshotName(currentTestName, hint, count) +
  411. '\n\n' +
  412. (0, _printSnapshot.printSnapshotAndReceived)(
  413. expected,
  414. actual,
  415. received,
  416. snapshotState.expand
  417. ); // Passing the actual and expected objects so that a custom reporter
  418. // could access them, for example in order to display a custom visual diff,
  419. // or create a different error message
  420. return {
  421. actual,
  422. expected,
  423. message,
  424. name: matcherName,
  425. pass: false
  426. };
  427. };
  428. const toThrowErrorMatchingSnapshot = function (
  429. received,
  430. hint, // because error TS1016 for hint?: string
  431. fromPromise
  432. ) {
  433. const matcherName = 'toThrowErrorMatchingSnapshot'; // Future breaking change: Snapshot hint must be a string
  434. // if (hint !== undefined && typeof hint !== string) {}
  435. return _toThrowErrorMatchingSnapshot(
  436. {
  437. context: this,
  438. hint,
  439. isInline: false,
  440. matcherName,
  441. received
  442. },
  443. fromPromise
  444. );
  445. };
  446. const toThrowErrorMatchingInlineSnapshot = function (
  447. received,
  448. inlineSnapshot,
  449. fromPromise
  450. ) {
  451. const matcherName = 'toThrowErrorMatchingInlineSnapshot';
  452. if (inlineSnapshot !== undefined && typeof inlineSnapshot !== 'string') {
  453. const options = {
  454. expectedColor: _printSnapshot.noColor,
  455. isNot: this.isNot,
  456. promise: this.promise
  457. };
  458. throw new Error(
  459. (0, _jestMatcherUtils.matcherErrorMessage)(
  460. (0, _jestMatcherUtils.matcherHint)(
  461. matcherName,
  462. undefined,
  463. _printSnapshot.SNAPSHOT_ARG,
  464. options
  465. ),
  466. `Inline snapshot must be a string`,
  467. (0, _jestMatcherUtils.printWithType)(
  468. 'Inline snapshot',
  469. inlineSnapshot,
  470. utils.serialize
  471. )
  472. )
  473. );
  474. }
  475. return _toThrowErrorMatchingSnapshot(
  476. {
  477. context: this,
  478. inlineSnapshot:
  479. inlineSnapshot !== undefined
  480. ? stripAddedIndentation(inlineSnapshot)
  481. : undefined,
  482. isInline: true,
  483. matcherName,
  484. received
  485. },
  486. fromPromise
  487. );
  488. };
  489. const _toThrowErrorMatchingSnapshot = (config, fromPromise) => {
  490. const {context, hint, inlineSnapshot, isInline, matcherName, received} =
  491. config;
  492. context.dontThrow && context.dontThrow();
  493. const {isNot, promise} = context;
  494. if (!fromPromise) {
  495. if (typeof received !== 'function') {
  496. const options = {
  497. isNot,
  498. promise
  499. };
  500. throw new Error(
  501. (0, _jestMatcherUtils.matcherErrorMessage)(
  502. (0, _jestMatcherUtils.matcherHint)(
  503. matcherName,
  504. undefined,
  505. '',
  506. options
  507. ),
  508. `${(0, _jestMatcherUtils.RECEIVED_COLOR)(
  509. 'received'
  510. )} value must be a function`,
  511. (0, _jestMatcherUtils.printWithType)(
  512. 'Received',
  513. received,
  514. _printSnapshot.printReceived
  515. )
  516. )
  517. );
  518. }
  519. }
  520. if (isNot) {
  521. throw new Error(
  522. (0, _jestMatcherUtils.matcherErrorMessage)(
  523. (0, _printSnapshot.matcherHintFromConfig)(config, false),
  524. NOT_SNAPSHOT_MATCHERS
  525. )
  526. );
  527. }
  528. let error;
  529. if (fromPromise) {
  530. error = received;
  531. } else {
  532. try {
  533. received();
  534. } catch (e) {
  535. error = e;
  536. }
  537. }
  538. if (error === undefined) {
  539. // Because the received value is a function, this is not a matcher error.
  540. throw new Error(
  541. (0, _printSnapshot.matcherHintFromConfig)(config, false) +
  542. '\n\n' +
  543. DID_NOT_THROW
  544. );
  545. }
  546. return _toMatchSnapshot({
  547. context,
  548. hint,
  549. inlineSnapshot,
  550. isInline,
  551. matcherName,
  552. received: error.message
  553. });
  554. };
  555. const JestSnapshot = {
  556. EXTENSION: _SnapshotResolver.EXTENSION,
  557. SnapshotState: _State.default,
  558. addSerializer: _plugins.addSerializer,
  559. buildSnapshotResolver: _SnapshotResolver.buildSnapshotResolver,
  560. cleanup,
  561. getSerializers: _plugins.getSerializers,
  562. isSnapshotPath: _SnapshotResolver.isSnapshotPath,
  563. toMatchInlineSnapshot,
  564. toMatchSnapshot,
  565. toThrowErrorMatchingInlineSnapshot,
  566. toThrowErrorMatchingSnapshot,
  567. utils
  568. };
  569. module.exports = JestSnapshot;