errorOnPrivate.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.installErrorOnPrivate = installErrorOnPrivate;
  6. var _jestUtil = require('jest-util');
  7. /**
  8. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  9. *
  10. * This source code is licensed under the MIT license found in the
  11. * LICENSE file in the root directory of this source tree.
  12. */
  13. // prettier-ignore
  14. const disabledGlobals = {
  15. fail: 'Illegal usage of global `fail`, prefer throwing an error, or the `done.fail` callback.',
  16. pending: 'Illegal usage of global `pending`, prefer explicitly skipping a test using `test.skip`',
  17. spyOn: 'Illegal usage of global `spyOn`, prefer `jest.spyOn`.',
  18. spyOnProperty: 'Illegal usage of global `spyOnProperty`, prefer `jest.spyOn`.'
  19. };
  20. // prettier-ignore
  21. const disabledJasmineMethods = {
  22. addMatchers: 'Illegal usage of `jasmine.addMatchers`, prefer `expect.extends`.',
  23. any: 'Illegal usage of `jasmine.any`, prefer `expect.any`.',
  24. anything: 'Illegal usage of `jasmine.anything`, prefer `expect.anything`.',
  25. arrayContaining: 'Illegal usage of `jasmine.arrayContaining`, prefer `expect.arrayContaining`.',
  26. createSpy: 'Illegal usage of `jasmine.createSpy`, prefer `jest.fn`.',
  27. objectContaining: 'Illegal usage of `jasmine.objectContaining`, prefer `expect.objectContaining`.',
  28. stringMatching: 'Illegal usage of `jasmine.stringMatching`, prefer `expect.stringMatching`.'
  29. };
  30. function installErrorOnPrivate(global) {
  31. const jasmine = global.jasmine;
  32. Object.keys(disabledGlobals).forEach(functionName => {
  33. global[functionName] = () => {
  34. throwAtFunction(disabledGlobals[functionName], global[functionName]);
  35. };
  36. });
  37. Object.keys(disabledJasmineMethods).forEach(methodName => {
  38. // @ts-expect-error
  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. Object.defineProperty(jasmine, 'DEFAULT_TIMEOUT_INTERVAL', {
  51. configurable: true,
  52. enumerable: true,
  53. get: () => original,
  54. set
  55. });
  56. }
  57. function throwAtFunction(message, fn) {
  58. throw new _jestUtil.ErrorWithStack(message, fn);
  59. }