setup_jest_globals.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. var _expect = require('expect');
  6. var _jestSnapshot = require('jest-snapshot');
  7. // Get suppressed errors form jest-matchers that weren't throw during
  8. // test execution and add them to the test result, potentially failing
  9. // a passing test.
  10. /**
  11. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  12. *
  13. * This source code is licensed under the MIT license found in the
  14. * LICENSE file in the root directory of this source tree.
  15. *
  16. *
  17. */
  18. const addSuppressedErrors = result => {
  19. var _getState = (0, _expect.getState)();
  20. const suppressedErrors = _getState.suppressedErrors;
  21. (0, _expect.setState)({suppressedErrors: []});
  22. if (suppressedErrors.length) {
  23. result.status = 'failed';
  24. result.failedExpectations = suppressedErrors.map(error => ({
  25. actual: '',
  26. // passing error for custom test reporters
  27. error: error,
  28. expected: '',
  29. message: error.message,
  30. passed: false,
  31. stack: error.stack
  32. }));
  33. }
  34. };
  35. const addAssertionErrors = result => {
  36. const assertionErrors = (0, _expect.extractExpectedAssertionsErrors)();
  37. if (assertionErrors.length) {
  38. const jasmineErrors = assertionErrors.map(_ref => {
  39. let actual = _ref.actual,
  40. error = _ref.error,
  41. expected = _ref.expected;
  42. return {
  43. actual: actual,
  44. expected: expected,
  45. message: error.stack,
  46. passed: false
  47. };
  48. });
  49. result.status = 'failed';
  50. result.failedExpectations = result.failedExpectations.concat(jasmineErrors);
  51. }
  52. };
  53. const patchJasmine = () => {
  54. global.jasmine.Spec = (realSpec => {
  55. const Spec = function Spec(attr) {
  56. const resultCallback = attr.resultCallback;
  57. attr.resultCallback = function(result) {
  58. addSuppressedErrors(result);
  59. addAssertionErrors(result);
  60. resultCallback.call(attr, result);
  61. };
  62. const onStart = attr.onStart;
  63. attr.onStart = context => {
  64. (0, _expect.setState)({currentTestName: context.getFullName()});
  65. onStart && onStart.call(attr, context);
  66. };
  67. realSpec.call(this, attr);
  68. };
  69. Spec.prototype = realSpec.prototype;
  70. for (const statics in realSpec) {
  71. if (Object.prototype.hasOwnProperty.call(realSpec, statics)) {
  72. Spec[statics] = realSpec[statics];
  73. }
  74. }
  75. return Spec;
  76. })(global.jasmine.Spec);
  77. };
  78. exports.default = _ref2 => {
  79. let config = _ref2.config,
  80. globalConfig = _ref2.globalConfig,
  81. localRequire = _ref2.localRequire,
  82. testPath = _ref2.testPath;
  83. // Jest tests snapshotSerializers in order preceding built-in serializers.
  84. // Therefore, add in reverse because the last added is the first tested.
  85. config.snapshotSerializers
  86. .concat()
  87. .reverse()
  88. .forEach(path => {
  89. (0, _jestSnapshot.addSerializer)(localRequire(path));
  90. });
  91. patchJasmine();
  92. const expand = globalConfig.expand,
  93. updateSnapshot = globalConfig.updateSnapshot;
  94. const snapshotState = new _jestSnapshot.SnapshotState(testPath, {
  95. expand: expand,
  96. getBabelTraverse: () => require('babel-traverse').default,
  97. getPrettier: () =>
  98. // $FlowFixMe dynamic require
  99. config.prettierPath ? require(config.prettierPath) : null,
  100. updateSnapshot: updateSnapshot
  101. });
  102. (0, _expect.setState)({snapshotState: snapshotState, testPath: testPath});
  103. // Return it back to the outer scope (test runner outside the VM).
  104. return snapshotState;
  105. };