e22m4u 1 неделя назад
Родитель
Сommit
67649146ac
6 измененных файлов с 45 добавлено и 44 удалено
  1. 15 17
      dist/cjs/index.cjs
  2. 2 0
      package.json
  3. 1 1
      src/create-spies-group.d.ts
  4. 2 4
      src/create-spies-group.js
  5. 6 3
      src/create-spy.d.ts
  6. 19 19
      src/create-spy.js

+ 15 - 17
dist/cjs/index.cjs

@@ -27,27 +27,27 @@ __export(index_exports, {
 module.exports = __toCommonJS(index_exports);
 
 // src/create-spy.js
-function _parseSpyArgs(target, methodNameOrImplFromSpy, customImplForMethodFromSpy) {
+function _parseSpyArgs(target, methodNameOrImpl, customImplForMethod) {
   let originalFn;
   let customImplementation;
   let isMethodSpy = false;
   let objToSpyOn;
   let methodName;
   let hasOwnMethod = false;
-  const isLikelyFunctionSpy = typeof target === "function" && customImplForMethodFromSpy === void 0;
-  const isLikelyMethodSpy = typeof target === "object" && target !== null && typeof methodNameOrImplFromSpy === "string";
+  const isLikelyFunctionSpy = typeof target === "function" && customImplForMethod === void 0;
+  const isLikelyMethodSpy = typeof target === "object" && target !== null && typeof methodNameOrImpl === "string";
   if (isLikelyFunctionSpy) {
     originalFn = target;
-    if (methodNameOrImplFromSpy !== void 0) {
-      if (typeof methodNameOrImplFromSpy !== "function") {
+    if (methodNameOrImpl !== void 0) {
+      if (typeof methodNameOrImpl !== "function") {
         throw new TypeError(
           "When spying on a function, the second argument (custom implementation) must be a function if provided."
         );
       }
-      customImplementation = methodNameOrImplFromSpy;
+      customImplementation = methodNameOrImpl;
     }
   } else if (isLikelyMethodSpy) {
-    methodName = methodNameOrImplFromSpy;
+    methodName = methodNameOrImpl;
     objToSpyOn = target;
     isMethodSpy = true;
     hasOwnMethod = Object.prototype.hasOwnProperty.call(objToSpyOn, methodName);
@@ -63,19 +63,19 @@ function _parseSpyArgs(target, methodNameOrImplFromSpy, customImplForMethodFromS
       );
     }
     originalFn = propertyToSpyOn;
-    if (customImplForMethodFromSpy !== void 0) {
-      if (typeof customImplForMethodFromSpy !== "function") {
+    if (customImplForMethod !== void 0) {
+      if (typeof customImplForMethod !== "function") {
         throw new TypeError(
           "When spying on a method, the third argument (custom implementation) must be a function if provided."
         );
       }
-      customImplementation = customImplForMethodFromSpy;
+      customImplementation = customImplForMethod;
     }
   } else {
-    if (target === null && methodNameOrImplFromSpy === void 0 && customImplForMethodFromSpy === void 0) {
+    if (target === null && methodNameOrImpl === void 0 && customImplForMethod === void 0) {
       throw new TypeError("Attempted to spy on null.");
     }
-    if (methodNameOrImplFromSpy === void 0 && typeof target !== "function") {
+    if (methodNameOrImpl === void 0 && typeof target !== "function") {
       throw new TypeError(
         "Attempted to spy on a non-function value. To spy on an object method, you must provide the method name as the second argument."
       );
@@ -96,7 +96,7 @@ function _parseSpyArgs(target, methodNameOrImplFromSpy, customImplForMethodFromS
   };
 }
 __name(_parseSpyArgs, "_parseSpyArgs");
-function createSpy(target, methodNameOrImpl, customImplForMethod) {
+function createSpy(target = void 0, methodNameOrImpl = void 0, customImplForMethod = void 0) {
   if (typeof target === "undefined" && typeof methodNameOrImpl === "undefined" && typeof customImplForMethod === "undefined") {
     target = /* @__PURE__ */ __name(function() {
     }, "target");
@@ -176,11 +176,9 @@ var SpiesGroup = class {
     __name(this, "SpiesGroup");
   }
   /**
-   * Constructor.
+   * Spies.
    */
-  constructor() {
-    this.spies = [];
-  }
+  spies = [];
   /**
    * Создает шпиона для отдельной функции
    * или метода объекта и добавляет его в группу.

+ 2 - 0
package.json

@@ -40,6 +40,8 @@
     "@commitlint/cli": "~20.1.0",
     "@commitlint/config-conventional": "~20.0.0",
     "@eslint/js": "~9.39.1",
+    "@types/chai": "~5.2.3",
+    "@types/mocha": "~10.0.10",
     "c8": "~10.1.3",
     "chai": "~6.2.1",
     "esbuild": "~0.27.0",

+ 1 - 1
src/create-spies-group.d.ts

@@ -11,7 +11,7 @@ export declare class SpiesGroup {
    * созданные в этой группе. Не предназначен
    * для прямого доступа.
    */
-  readonly spies: Spy<any>[];
+  spies: Spy<any>[];
 
   /**
    * Создает шпиона для отдельной функции

+ 2 - 4
src/create-spies-group.js

@@ -6,11 +6,9 @@ import {createSpy} from './create-spy.js';
  */
 export class SpiesGroup {
   /**
-   * Constructor.
+   * Spies.
    */
-  constructor() {
-    this.spies = [];
-  }
+  spies = [];
 
   /**
    * Создает шпиона для отдельной функции

+ 6 - 3
src/create-spy.d.ts

@@ -78,7 +78,7 @@ export interface Spy<TFunc extends AnyCallable = AnyCallable> {
 /**
  * Создает шпиона.
  */
-export function createSpy(): Spy<(...args: any[]) => void>;
+export declare function createSpy(): Spy<(...args: any[]) => void>;
 
 /**
  * Создает шпиона для отдельной функции.
@@ -86,7 +86,7 @@ export function createSpy(): Spy<(...args: any[]) => void>;
  * @param targetFn
  * @param customImpl
  */
-export function createSpy<TFunc extends AnyCallable>(
+export declare function createSpy<TFunc extends AnyCallable>(
   targetFn: TFunc,
   customImpl?: TFunc,
 ): Spy<TFunc>;
@@ -100,7 +100,10 @@ export function createSpy<TFunc extends AnyCallable>(
  * @param methodName
  * @param customImpl
  */
-export function createSpy<TObj extends object, K extends MethodKey<TObj>>(
+export declare function createSpy<
+  TObj extends object,
+  K extends MethodKey<TObj>,
+>(
   targetObject: TObj,
   methodName: K,
   customImpl?: TObj[K],

+ 19 - 19
src/create-spy.js

@@ -6,11 +6,7 @@
  * @param {Function|undefined} customImplForMethod
  * @returns {object}
  */
-function _parseSpyArgs(
-  target,
-  methodNameOrImplFromSpy,
-  customImplForMethodFromSpy,
-) {
+function _parseSpyArgs(target, methodNameOrImpl, customImplForMethod) {
   // объявление переменных для хранения
   // состояния и результатов разбора аргументов
   let originalFn;
@@ -22,13 +18,13 @@ function _parseSpyArgs(
   // определение вероятности того, что
   // создается шпион для отдельной функции
   const isLikelyFunctionSpy =
-    typeof target === 'function' && customImplForMethodFromSpy === undefined;
+    typeof target === 'function' && customImplForMethod === undefined;
   // определение вероятности того, что
   // создается шпион для метода объекта
   const isLikelyMethodSpy =
     typeof target === 'object' &&
     target !== null &&
-    typeof methodNameOrImplFromSpy === 'string';
+    typeof methodNameOrImpl === 'string';
   // обработка сценария шпионажа
   // за отдельной функцией
   if (isLikelyFunctionSpy) {
@@ -36,10 +32,10 @@ function _parseSpyArgs(
     originalFn = target;
     // проверка наличия второго аргумента, который
     // может быть пользовательской реализацией
-    if (methodNameOrImplFromSpy !== undefined) {
+    if (methodNameOrImpl !== undefined) {
       // генерация ошибки, если второй аргумент
       // (пользовательская реализация) не является функцией
-      if (typeof methodNameOrImplFromSpy !== 'function') {
+      if (typeof methodNameOrImpl !== 'function') {
         throw new TypeError(
           'When spying on a function, the second argument (custom ' +
             'implementation) must be a function if provided.',
@@ -47,14 +43,14 @@ function _parseSpyArgs(
       }
       // пользовательская реализация присваивается,
       // если она предоставлена и является функцией
-      customImplementation = methodNameOrImplFromSpy;
+      customImplementation = methodNameOrImpl;
     }
     // обработка сценария шпионажа
     // за методом объекта
   } else if (isLikelyMethodSpy) {
     // установка параметров для
     // шпионажа за методом
-    methodName = methodNameOrImplFromSpy;
+    methodName = methodNameOrImpl;
     objToSpyOn = target;
     isMethodSpy = true;
     hasOwnMethod = Object.prototype.hasOwnProperty.call(objToSpyOn, methodName);
@@ -81,10 +77,10 @@ function _parseSpyArgs(
     originalFn = propertyToSpyOn;
     // проверка наличия третьего аргумента, который может
     // быть пользовательской реализацией для метода
-    if (customImplForMethodFromSpy !== undefined) {
+    if (customImplForMethod !== undefined) {
       // генерация ошибки, если третья (пользовательская
       // реализация метода) не является функцией
-      if (typeof customImplForMethodFromSpy !== 'function') {
+      if (typeof customImplForMethod !== 'function') {
         throw new TypeError(
           'When spying on a method, the third argument (custom ' +
             'implementation) must be a function if provided.',
@@ -92,7 +88,7 @@ function _parseSpyArgs(
       }
       // пользовательская реализация метода присваивается,
       // если она предоставлена и является функцией
-      customImplementation = customImplForMethodFromSpy;
+      customImplementation = customImplForMethod;
     }
     // обработка невалидных
     // комбинаций аргументов
@@ -101,14 +97,14 @@ function _parseSpyArgs(
     // для попытки шпионить за null
     if (
       target === null &&
-      methodNameOrImplFromSpy === undefined &&
-      customImplForMethodFromSpy === undefined
+      methodNameOrImpl === undefined &&
+      customImplForMethod === undefined
     ) {
       throw new TypeError('Attempted to spy on null.');
     }
     // генерация ошибки, если target не функция
     // и имя метода не предоставлено
-    if (methodNameOrImplFromSpy === undefined && typeof target !== 'function') {
+    if (methodNameOrImpl === undefined && typeof target !== 'function') {
       throw new TypeError(
         'Attempted to spy on a non-function value. To spy on an object method, ' +
           'you must provide the method name as the second argument.',
@@ -146,12 +142,16 @@ function _parseSpyArgs(
  * Шпионить за методом объекта:
  * createSpy(targetObject, methodName, [customImplementation])
  *
- * @param {Function|object} target
+ * @param {Function|object|undefined} target
  * @param {Function|string|undefined} methodNameOrImpl
  * @param {Function|undefined} customImplForMethod
  * @returns {Function}
  */
-export function createSpy(target, methodNameOrImpl, customImplForMethod) {
+export function createSpy(
+  target = undefined,
+  methodNameOrImpl = undefined,
+  customImplForMethod = undefined,
+) {
   // если аргументы не передавались,
   // то определяется функция-пустышка
   if (