版博士V2.0程序
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

123 satır
3.5 KiB

  1. import * as tinyspy from 'tinyspy';
  2. const spies = /* @__PURE__ */ new Set();
  3. function isMockFunction(fn2) {
  4. return typeof fn2 === "function" && "_isMockFunction" in fn2 && fn2._isMockFunction;
  5. }
  6. function spyOn(obj, method, accessType) {
  7. const dictionary = {
  8. get: "getter",
  9. set: "setter"
  10. };
  11. const objMethod = accessType ? { [dictionary[accessType]]: method } : method;
  12. const stub = tinyspy.spyOn(obj, objMethod);
  13. return enhanceSpy(stub);
  14. }
  15. let callOrder = 0;
  16. function enhanceSpy(spy) {
  17. const stub = spy;
  18. let implementation;
  19. let instances = [];
  20. let invocations = [];
  21. const mockContext = {
  22. get calls() {
  23. return stub.calls;
  24. },
  25. get instances() {
  26. return instances;
  27. },
  28. get invocationCallOrder() {
  29. return invocations;
  30. },
  31. get results() {
  32. return stub.results.map(([callType, value]) => {
  33. const type = callType === "error" ? "throw" : "return";
  34. return { type, value };
  35. });
  36. },
  37. get lastCall() {
  38. return stub.calls[stub.calls.length - 1];
  39. }
  40. };
  41. let onceImplementations = [];
  42. let implementationChangedTemporarily = false;
  43. let name = stub.name;
  44. stub.getMockName = () => name || "vi.fn()";
  45. stub.mockName = (n) => {
  46. name = n;
  47. return stub;
  48. };
  49. stub.mockClear = () => {
  50. stub.reset();
  51. instances = [];
  52. invocations = [];
  53. return stub;
  54. };
  55. stub.mockReset = () => {
  56. stub.mockClear();
  57. implementation = () => void 0;
  58. onceImplementations = [];
  59. return stub;
  60. };
  61. stub.mockRestore = () => {
  62. stub.mockReset();
  63. implementation = void 0;
  64. return stub;
  65. };
  66. stub.getMockImplementation = () => implementation;
  67. stub.mockImplementation = (fn2) => {
  68. implementation = fn2;
  69. return stub;
  70. };
  71. stub.mockImplementationOnce = (fn2) => {
  72. onceImplementations.push(fn2);
  73. return stub;
  74. };
  75. function withImplementation(fn2, cb) {
  76. const originalImplementation = implementation;
  77. implementation = fn2;
  78. implementationChangedTemporarily = true;
  79. const reset = () => {
  80. implementation = originalImplementation;
  81. implementationChangedTemporarily = false;
  82. };
  83. const result = cb();
  84. if (result instanceof Promise) {
  85. return result.then(() => {
  86. reset();
  87. return stub;
  88. });
  89. }
  90. reset();
  91. return stub;
  92. }
  93. stub.withImplementation = withImplementation;
  94. stub.mockReturnThis = () => stub.mockImplementation(function() {
  95. return this;
  96. });
  97. stub.mockReturnValue = (val) => stub.mockImplementation(() => val);
  98. stub.mockReturnValueOnce = (val) => stub.mockImplementationOnce(() => val);
  99. stub.mockResolvedValue = (val) => stub.mockImplementation(() => Promise.resolve(val));
  100. stub.mockResolvedValueOnce = (val) => stub.mockImplementationOnce(() => Promise.resolve(val));
  101. stub.mockRejectedValue = (val) => stub.mockImplementation(() => Promise.reject(val));
  102. stub.mockRejectedValueOnce = (val) => stub.mockImplementationOnce(() => Promise.reject(val));
  103. Object.defineProperty(stub, "mock", {
  104. get: () => mockContext
  105. });
  106. stub.willCall(function(...args) {
  107. instances.push(this);
  108. invocations.push(++callOrder);
  109. const impl = implementationChangedTemporarily ? implementation : onceImplementations.shift() || implementation || stub.getOriginal() || (() => {
  110. });
  111. return impl.apply(this, args);
  112. });
  113. spies.add(stub);
  114. return stub;
  115. }
  116. function fn(implementation) {
  117. return enhanceSpy(tinyspy.spyOn({ fn: implementation || (() => {
  118. }) }, "fn"));
  119. }
  120. export { fn, isMockFunction, spies, spyOn };