error_on_private.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.installErrorOnPrivate = installErrorOnPrivate;
  6. // prettier-ignore
  7. const disabledGlobals = {
  8. fail: 'Illegal usage of global `fail`, prefer throwing an error, or the `done.fail` callback.',
  9. pending: 'Illegal usage of global `pending`, prefer explicitly skipping a test using `test.skip`',
  10. spyOn: 'Illegal usage of global `spyOn`, prefer `jest.spyOn`.',
  11. spyOnProperty: 'Illegal usage of global `spyOnProperty`, prefer `jest.spyOn`.'
  12. };
  13. // prettier-ignore
  14. /**
  15. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  16. *
  17. * This source code is licensed under the MIT license found in the
  18. * LICENSE file in the root directory of this source tree.
  19. *
  20. *
  21. */
  22. const disabledJasmineMethods = {
  23. addMatchers: 'Illegal usage of `jasmine.addMatchers`, prefer `expect.extends`.',
  24. any: 'Illegal usage of `jasmine.any`, prefer `expect.any`.',
  25. anything: 'Illegal usage of `jasmine.anything`, prefer `expect.anything`.',
  26. arrayContaining: 'Illegal usage of `jasmine.arrayContaining`, prefer `expect.arrayContaining`.',
  27. createSpy: 'Illegal usage of `jasmine.createSpy`, prefer `jest.fn`.',
  28. objectContaining: 'Illegal usage of `jasmine.objectContaining`, prefer `expect.objectContaining`.',
  29. stringMatching: 'Illegal usage of `jasmine.stringMatching`, prefer `expect.stringMatching`.'
  30. };
  31. function installErrorOnPrivate(global) {
  32. const jasmine = global.jasmine;
  33. Object.keys(disabledGlobals).forEach(functionName => {
  34. global[functionName] = () => {
  35. throwAtFunction(disabledGlobals[functionName], global[functionName]);
  36. };
  37. });
  38. Object.keys(disabledJasmineMethods).forEach(methodName => {
  39. jasmine[methodName] = () => {
  40. throwAtFunction(disabledJasmineMethods[methodName], jasmine[methodName]);
  41. };
  42. });
  43. function set() {
  44. throwAtFunction(
  45. 'Illegal usage of `jasmine.DEFAULT_TIMEOUT_INTERVAL`, prefer `jest.setTimeout`.',
  46. set
  47. );
  48. }
  49. const original = jasmine.DEFAULT_TIMEOUT_INTERVAL;
  50. // $FlowFixMe Flow seems to be confused about accessors and tries to enfoce having a `value` property.
  51. Object.defineProperty(jasmine, 'DEFAULT_TIMEOUT_INTERVAL', {
  52. configurable: true,
  53. enumerable: true,
  54. get: () => original,
  55. set: set
  56. });
  57. }
  58. function throwAtFunction(message, fn) {
  59. const e = new Error(message);
  60. if (Error.captureStackTrace) {
  61. Error.captureStackTrace(e, fn);
  62. }
  63. throw e;
  64. }