setup_jest_globals.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = setupJestGlobals;
  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 {suppressedErrors} = (0, _expect.getState)();
  19. (0, _expect.setState)({
  20. suppressedErrors: []
  21. });
  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,
  28. expected: '',
  29. matcherName: '',
  30. message: error.message,
  31. passed: false,
  32. stack: error.stack
  33. }));
  34. }
  35. };
  36. const addAssertionErrors = result => {
  37. const assertionErrors = (0, _expect.extractExpectedAssertionsErrors)();
  38. if (assertionErrors.length) {
  39. const jasmineErrors = assertionErrors.map(({actual, error, expected}) => ({
  40. actual,
  41. expected,
  42. message: error.stack,
  43. passed: false
  44. }));
  45. result.status = 'failed';
  46. result.failedExpectations = result.failedExpectations.concat(jasmineErrors);
  47. }
  48. };
  49. const patchJasmine = () => {
  50. global.jasmine.Spec = (realSpec => {
  51. class Spec extends realSpec {
  52. constructor(attr) {
  53. const resultCallback = attr.resultCallback;
  54. attr.resultCallback = function (result) {
  55. addSuppressedErrors(result);
  56. addAssertionErrors(result);
  57. resultCallback.call(attr, result);
  58. };
  59. const onStart = attr.onStart;
  60. attr.onStart = context => {
  61. (0, _expect.setState)({
  62. currentTestName: context.getFullName()
  63. });
  64. onStart && onStart.call(attr, context);
  65. };
  66. super(attr);
  67. }
  68. }
  69. return Spec;
  70. })(global.jasmine.Spec);
  71. };
  72. async function setupJestGlobals({
  73. config,
  74. globalConfig,
  75. localRequire,
  76. testPath
  77. }) {
  78. // Jest tests snapshotSerializers in order preceding built-in serializers.
  79. // Therefore, add in reverse because the last added is the first tested.
  80. config.snapshotSerializers
  81. .concat()
  82. .reverse()
  83. .forEach(path => {
  84. (0, _jestSnapshot.addSerializer)(localRequire(path));
  85. });
  86. patchJasmine();
  87. const {expand, updateSnapshot} = globalConfig;
  88. const {prettierPath, snapshotFormat} = config;
  89. const snapshotResolver = await (0, _jestSnapshot.buildSnapshotResolver)(
  90. config,
  91. localRequire
  92. );
  93. const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
  94. const snapshotState = new _jestSnapshot.SnapshotState(snapshotPath, {
  95. expand,
  96. prettierPath,
  97. snapshotFormat,
  98. updateSnapshot
  99. }); // @ts-expect-error: snapshotState is a jest extension of `expect`
  100. (0, _expect.setState)({
  101. snapshotState,
  102. testPath
  103. }); // Return it back to the outer scope (test runner outside the VM).
  104. return snapshotState;
  105. }