setup_jest_globals.js 3.2 KB

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