Просмотр исходного кода

refactor: improve error checking

e22m4u 1 неделя назад
Родитель
Сommit
251198b2b2
4 измененных файлов с 27 добавлено и 7 удалено
  1. 4 2
      dist/cjs/index.cjs
  2. 4 2
      src/create-spies-group.js
  3. 7 0
      src/create-spy.spec.js
  4. 12 3
      tsconfig.json

+ 4 - 2
dist/cjs/index.cjs

@@ -176,9 +176,11 @@ var SpiesGroup = class {
     __name(this, "SpiesGroup");
   }
   /**
-   * Spies.
+   * Constructor.
    */
-  spies = [];
+  constructor() {
+    this.spies = [];
+  }
   /**
    * Создает шпиона для отдельной функции
    * или метода объекта и добавляет его в группу.

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

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

+ 7 - 0
src/create-spy.spec.js

@@ -23,11 +23,13 @@ describe('createSpy', function () {
     it('should throw if target is not a function and no method name is given', function () {
       // проверка генерации ошибки для не-функции
       // без указания имени метода
+      // @ts-ignore
       expect(() => createSpy({})).to.throw(
         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.',
       );
+      // @ts-ignore
       expect(() => createSpy(123)).to.throw(
         TypeError,
         'Attempted to spy on a non-function value. To spy on an object method, ' +
@@ -39,6 +41,7 @@ describe('createSpy', function () {
       // проверка генерации ошибки, если кастомная
       // реализация для шпиона функции не является функцией
       const targetFn = () => {};
+      // @ts-ignore
       expect(() => createSpy(targetFn, 'not a function')).to.throw(
         TypeError,
         'When spying on a function, the second argument (custom implementation) must be a function if provided.',
@@ -49,6 +52,7 @@ describe('createSpy', function () {
       // проверка генерации ошибки при попытке
       // шпионить за несуществующим методом объекта
       const obj = {};
+      // @ts-ignore
       expect(() => createSpy(obj, 'nonExistentMethod')).to.throw(
         TypeError,
         'Attempted to spy on a non-existent property: "nonExistentMethod"',
@@ -59,6 +63,7 @@ describe('createSpy', function () {
       // проверка генерации ошибки при попытке
       // шпионить за свойством объекта, не являющимся функцией
       const obj = {prop: 123};
+      // @ts-ignore
       expect(() => createSpy(obj, 'prop')).to.throw(
         TypeError,
         'Attempted to spy on "prop" which is not a function. It is a "number".',
@@ -69,6 +74,7 @@ describe('createSpy', function () {
       // проверка генерации ошибки, если кастомная реализация
       // для шпиона метода не является функцией
       const obj = {method: () => {}};
+      // @ts-ignore
       expect(() => createSpy(obj, 'method', 'not a function')).to.throw(
         TypeError,
         'When spying on a method, the third argument (custom implementation) must be a function if provided.',
@@ -190,6 +196,7 @@ describe('createSpy', function () {
         const standaloneFn = () => 'standalone result';
         const fnSpy = createSpy(standaloneFn);
         // вызов шпиона, чтобы у него была история
+        // @ts-ignore
         fnSpy('call standalone');
         expect(fnSpy.isCalled).to.be.true;
         expect(fnSpy.callCount).to.be.eq(1);

+ 12 - 3
tsconfig.json

@@ -1,9 +1,18 @@
 {
   "compilerOptions": {
-    "rootDir": "src",
     "noEmit": true,
     "target": "es2022",
     "module": "NodeNext",
-    "moduleResolution": "NodeNext"
-  }
+    "moduleResolution": "NodeNext",
+    "allowJs": true,
+    "checkJs": true,
+  },
+  "include": [
+    "./src/**/*.ts",
+    "./src/**/*.js"
+  ],
+  "exclude": [
+    "node_modules",
+    "dist"
+  ]
 }