@furystack/utils 3.1.7 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/package.json +21 -8
  2. package/src/debounce.spec.ts +20 -17
  3. package/src/deep-merge.spec.ts +1 -0
  4. package/src/deep-merge.ts +0 -1
  5. package/src/disposable.spec.ts +23 -15
  6. package/src/disposable.ts +0 -2
  7. package/src/observable-value.spec.ts +47 -20
  8. package/src/observable-value.ts +4 -7
  9. package/src/path-helper.spec.ts +1 -0
  10. package/src/path-helper.ts +0 -5
  11. package/src/sleep-async.spec.ts +1 -0
  12. package/src/sleep-async.ts +0 -1
  13. package/src/sort-by.spec.ts +1 -0
  14. package/src/trace.spec.ts +48 -26
  15. package/src/trace.ts +0 -1
  16. package/src/tuple.spec.ts +1 -0
  17. package/src/tuple.ts +0 -1
  18. package/src/value-observer.ts +1 -2
  19. package/types/deep-merge.d.ts +0 -1
  20. package/types/deep-merge.d.ts.map +1 -1
  21. package/types/disposable.d.ts +0 -2
  22. package/types/disposable.d.ts.map +1 -1
  23. package/types/observable-value.d.ts +1 -7
  24. package/types/observable-value.d.ts.map +1 -1
  25. package/types/path-helper.d.ts +0 -5
  26. package/types/path-helper.d.ts.map +1 -1
  27. package/types/sleep-async.d.ts +0 -1
  28. package/types/sleep-async.d.ts.map +1 -1
  29. package/types/trace.d.ts +0 -1
  30. package/types/trace.d.ts.map +1 -1
  31. package/types/tuple.d.ts +0 -1
  32. package/types/tuple.d.ts.map +1 -1
  33. package/types/value-observer.d.ts +1 -2
  34. package/types/value-observer.d.ts.map +1 -1
  35. package/dist/debounce.js +0 -23
  36. package/dist/debounce.js.map +0 -1
  37. package/dist/debounce.spec.js +0 -40
  38. package/dist/debounce.spec.js.map +0 -1
  39. package/dist/deep-merge.js +0 -34
  40. package/dist/deep-merge.js.map +0 -1
  41. package/dist/deep-merge.spec.js +0 -31
  42. package/dist/deep-merge.spec.js.map +0 -1
  43. package/dist/disposable.js +0 -63
  44. package/dist/disposable.js.map +0 -1
  45. package/dist/disposable.spec.js +0 -112
  46. package/dist/disposable.spec.js.map +0 -1
  47. package/dist/index.js +0 -27
  48. package/dist/index.js.map +0 -1
  49. package/dist/observable-value.js +0 -117
  50. package/dist/observable-value.js.map +0 -1
  51. package/dist/observable-value.spec.js +0 -109
  52. package/dist/observable-value.spec.js.map +0 -1
  53. package/dist/path-helper.js +0 -75
  54. package/dist/path-helper.js.map +0 -1
  55. package/dist/path-helper.spec.js +0 -62
  56. package/dist/path-helper.spec.js.map +0 -1
  57. package/dist/sleep-async.js +0 -13
  58. package/dist/sleep-async.js.map +0 -1
  59. package/dist/sleep-async.spec.js +0 -16
  60. package/dist/sleep-async.spec.js.map +0 -1
  61. package/dist/sort-by.js +0 -19
  62. package/dist/sort-by.js.map +0 -1
  63. package/dist/sort-by.spec.js +0 -22
  64. package/dist/sort-by.spec.js.map +0 -1
  65. package/dist/trace.js +0 -131
  66. package/dist/trace.js.map +0 -1
  67. package/dist/trace.spec.js +0 -169
  68. package/dist/trace.spec.js.map +0 -1
  69. package/dist/tuple.js +0 -12
  70. package/dist/tuple.js.map +0 -1
  71. package/dist/tuple.spec.js +0 -9
  72. package/dist/tuple.spec.js.map +0 -1
  73. package/dist/value-observer.js +0 -45
  74. package/dist/value-observer.js.map +0 -1
  75. package/types/debounce.spec.d.ts +0 -5
  76. package/types/debounce.spec.d.ts.map +0 -1
  77. package/types/deep-merge.spec.d.ts +0 -2
  78. package/types/deep-merge.spec.d.ts.map +0 -1
  79. package/types/disposable.spec.d.ts +0 -22
  80. package/types/disposable.spec.d.ts.map +0 -1
  81. package/types/observable-value.spec.d.ts +0 -5
  82. package/types/observable-value.spec.d.ts.map +0 -1
  83. package/types/path-helper.spec.d.ts +0 -5
  84. package/types/path-helper.spec.d.ts.map +0 -1
  85. package/types/sleep-async.spec.d.ts +0 -5
  86. package/types/sleep-async.spec.d.ts.map +0 -1
  87. package/types/sort-by.spec.d.ts +0 -2
  88. package/types/sort-by.spec.d.ts.map +0 -1
  89. package/types/trace.spec.d.ts +0 -2
  90. package/types/trace.spec.d.ts.map +0 -1
  91. package/types/tuple.spec.d.ts +0 -2
  92. package/types/tuple.spec.d.ts.map +0 -1
package/package.json CHANGED
@@ -1,18 +1,30 @@
1
1
  {
2
2
  "name": "@furystack/utils",
3
- "version": "3.1.7",
3
+ "version": "4.0.0",
4
4
  "description": "General utilities",
5
- "main": "dist/index.js",
6
- "typings": "./types/index.d.ts",
5
+ "type": "module",
6
+ "scripts": {
7
+ "build:es6": "tsc --outDir ./esm"
8
+ },
9
+ "exports": {
10
+ ".": {
11
+ "import": "./esm/index.js",
12
+ "types": "./types/index.d.ts"
13
+ },
14
+ "./package.json": "./package.json"
15
+ },
16
+ "typesVersions": {
17
+ "*": {
18
+ "*": [
19
+ "types/*"
20
+ ]
21
+ }
22
+ },
7
23
  "files": [
8
24
  "dist",
9
25
  "types",
10
26
  "src"
11
27
  ],
12
- "scripts": {
13
- "fix:prettier": "prettier \"{,!(dist|temp)/**/}*.{ts,tsx}\" --write",
14
- "build": "tsc -b"
15
- },
16
28
  "repository": {
17
29
  "type": "git",
18
30
  "url": "https://github.com/furystack/furystack.git"
@@ -31,6 +43,7 @@
31
43
  },
32
44
  "homepage": "https://github.com/furystack/furystack",
33
45
  "devDependencies": {
34
- "@types/jest": "^29.5.0"
46
+ "typescript": "^5.0.4",
47
+ "vitest": "^0.30.1"
35
48
  }
36
49
  }
@@ -1,3 +1,4 @@
1
+ import { describe, it, expect, vi } from 'vitest'
1
2
  import { debounce } from './debounce'
2
3
  import { sleepAsync } from './sleep-async'
3
4
 
@@ -5,35 +6,37 @@ import { sleepAsync } from './sleep-async'
5
6
  * Tests for debounce
6
7
  */
7
8
  export const debounceTests = describe('debounce', () => {
8
- it('Simple method execution', (done) => {
9
- const method = debounce(() => {
10
- done()
9
+ it('Simple method execution', async () => {
10
+ const method = vi.fn()
11
+ await new Promise<void>((resolve) => {
12
+ const debouncedMethod = debounce(() => {
13
+ resolve()
14
+ method()
15
+ })
16
+ debouncedMethod()
11
17
  })
12
- method()
18
+ expect(method).toBeCalledTimes(1)
13
19
  })
14
20
 
15
21
  it('Should be executed once when triggered multiple time in a given range', async () => {
16
- let counter = 0
17
- const method = debounce(() => {
18
- counter++
19
- }, 10)
22
+ const method = vi.fn()
23
+
24
+ const debouncedMethod = debounce(method, 10)
20
25
  for (let index = 0; index < 10; index++) {
21
- method()
26
+ debouncedMethod()
22
27
  }
23
28
  await sleepAsync(300)
24
- expect(counter).toBe(1)
29
+ expect(method).toBeCalledTimes(1)
25
30
  })
26
31
 
27
32
  it('Should be executed multiple times when triggered multiple times out of a a given range', async () => {
28
- let counter = 0
29
- const method = debounce(() => {
30
- counter++
31
- }, 10)
33
+ const method = vi.fn()
34
+
35
+ const debouncedMethod = debounce(method, 10)
32
36
  for (let index = 0; index < 10; index++) {
33
- method()
37
+ debouncedMethod()
34
38
  await sleepAsync(15)
35
39
  }
36
- await sleepAsync(300)
37
- expect(counter).toBe(10)
40
+ expect(method).toBeCalledTimes(10)
38
41
  })
39
42
  })
@@ -1,3 +1,4 @@
1
+ import { describe, it, expect } from 'vitest'
1
2
  import { deepMerge } from './deep-merge'
2
3
 
3
4
  describe('DeepMerge tests', () => {
package/src/deep-merge.ts CHANGED
@@ -5,7 +5,6 @@ export type DeepPartial<T> = { [K in keyof T]?: DeepPartial<T[K]> }
5
5
 
6
6
  /**
7
7
  * Deep merge two objects.
8
- *
9
8
  * @param target The source object to be merged
10
9
  * @param sources The source objects
11
10
  * @returns A new instance with the merged values
@@ -1,3 +1,4 @@
1
+ import { describe, it, expect, vi } from 'vitest'
1
2
  import type { Disposable } from './disposable'
2
3
  import { using, usingAsync } from './disposable'
3
4
 
@@ -59,42 +60,49 @@ export const disposableTests = describe('Disposable', () => {
59
60
  })
60
61
 
61
62
  describe('dispose()', () => {
62
- it('should be called on error', (done) => {
63
+ it('should be called on error', () => {
64
+ const callbackMethod = vi.fn()
63
65
  try {
64
66
  using(new MockDisposable(), (d) => {
65
67
  d.disposeCallback = () => {
66
- done()
68
+ callbackMethod()
67
69
  }
68
-
69
70
  d.whooops()
70
71
  })
71
72
  } catch {
72
73
  /** ignore */
73
74
  }
75
+ expect(callbackMethod).toBeCalled()
74
76
  })
75
77
 
76
- it('should be called with usingAsync()', (done) => {
77
- usingAsync(new MockDisposable(), async (d) => {
78
+ it('should be called with usingAsync()', async () => {
79
+ const callbackMethod = vi.fn()
80
+ await usingAsync(new MockDisposable(), async (d) => {
78
81
  d.disposeCallback = () => {
79
- done()
82
+ callbackMethod()
80
83
  }
81
84
  return new Promise((resolve) => {
82
85
  setTimeout(resolve, 1)
83
86
  })
84
87
  })
88
+ expect(callbackMethod).toBeCalled()
85
89
  })
86
90
 
87
- it('should be called when async fails', (done) => {
88
- usingAsync(new MockDisposable(), async (d) => {
89
- d.disposeCallback = () => {
90
- done()
91
- }
92
- return new Promise((_resolve, reject) => {
93
- setTimeout(reject, 1)
91
+ it('should be called when async fails', async () => {
92
+ const callbackMethod = vi.fn()
93
+ try {
94
+ await usingAsync(new MockDisposable(), async (d) => {
95
+ d.disposeCallback = () => {
96
+ callbackMethod()
97
+ }
98
+ return new Promise((_resolve, reject) => {
99
+ setTimeout(reject, 1)
100
+ })
94
101
  })
95
- }).catch(() => {
102
+ } catch (error) {
96
103
  /** ignore */
97
- })
104
+ }
105
+ expect(callbackMethod).toBeCalled()
98
106
  })
99
107
 
100
108
  it('should await dispose for asyncs with usingAsync()', async () => {
package/src/disposable.ts CHANGED
@@ -36,7 +36,6 @@ export interface Disposable {
36
36
 
37
37
  /**
38
38
  * Method that accepts an IDisposable resource that will be disposed after the callback
39
- *
40
39
  * @param resource The resource that is used in the callback and will be disposed afterwards
41
40
  * @param callback The callback that will be executed synchrounously before the resource will be disposed
42
41
  * @returns the value that will be returned by the callback method
@@ -51,7 +50,6 @@ export const using = <T extends Disposable, TReturns>(resource: T, callback: (r:
51
50
 
52
51
  /**
53
52
  * Method that accepts an IDisposable resource that will be disposed after the callback
54
- *
55
53
  * @param resource The resource that is used in the callback and will be disposed afterwards
56
54
  * @param callback The callback that will be executed asynchrounously before the resource will be disposed
57
55
  * @returns A promise that will be resolved with a return value after the resource is disposed
@@ -1,64 +1,77 @@
1
+ import { describe, it, expect, vi } from 'vitest'
1
2
  import { ObservableValue } from './observable-value'
2
3
 
3
4
  /**
4
5
  * Observable Value tests
5
6
  */
6
7
  export const observableTests = describe('Observable', () => {
7
- it('should be constructed without initial value', (done) => {
8
+ it('should be constructed without initial value', () => {
8
9
  const v = new ObservableValue()
10
+ const doneCallback = vi.fn()
9
11
  v.subscribe(() => {
10
12
  expect(v.getValue()).toBe(undefined)
11
- done()
13
+ doneCallback()
12
14
  }, true)
13
15
  expect(v).toBeInstanceOf(ObservableValue)
16
+ expect(doneCallback).toBeCalled()
14
17
  })
15
18
 
16
- it('should be constructed with initial value', (done) => {
19
+ it('should be constructed with initial value', () => {
17
20
  const v = new ObservableValue(1)
21
+ const doneCallback = vi.fn()
22
+
18
23
  v.subscribe(() => {
19
24
  expect(v.getValue()).toBe(1)
20
- done()
25
+ doneCallback()
21
26
  }, true)
27
+ expect(doneCallback).toBeCalled()
22
28
  })
23
29
 
24
30
  describe('Subscription callback', () => {
25
- it('should be triggered only when a value is changed', (done) => {
31
+ it('should be triggered only when a value is changed', () => {
26
32
  const v = new ObservableValue(1)
33
+ const doneCallback = vi.fn()
34
+
27
35
  v.subscribe(() => {
28
36
  expect(v.getValue()).toBe(2)
29
- done()
37
+ doneCallback()
30
38
  }, false)
31
39
  v.setValue(1)
32
40
  v.setValue(1)
33
41
  v.setValue(2)
42
+ expect(doneCallback).toBeCalledTimes(1)
34
43
  })
35
44
 
36
- it('should be triggered only on change when getLast is false', (done) => {
45
+ it('should be triggered only on change when getLast is false', () => {
37
46
  const v = new ObservableValue(1)
47
+ const doneCallback = vi.fn()
48
+
38
49
  v.subscribe((value) => {
39
50
  expect(value).toBe(2)
40
- done()
51
+ doneCallback()
41
52
  }, false)
42
53
  v.setValue(2)
54
+ expect(doneCallback).toBeCalledTimes(1)
43
55
  })
44
56
  })
45
57
 
46
58
  describe('Unsubscribe', () => {
47
- it('should remove the subscription on unsubscribe()', (done) => {
48
- const callback1 = () => {
49
- done(Error('Shouldnt be triggered'))
50
- }
59
+ it('should remove the subscription on unsubscribe()', () => {
60
+ const shouldNotCall = vi.fn()
51
61
 
52
- const callback2 = (value: number) => {
62
+ const doneCallback = vi.fn((value: number) => {
53
63
  expect(value).toBe(2)
54
- done()
55
- }
64
+ })
65
+
56
66
  const v = new ObservableValue(1)
57
- const observer1 = v.subscribe(callback1)
58
- v.subscribe(callback2)
67
+ const observer1 = v.subscribe(shouldNotCall)
68
+ v.subscribe(doneCallback)
59
69
 
60
70
  v.unsubscribe(observer1)
61
71
  v.setValue(2)
72
+
73
+ expect(doneCallback).toBeCalledTimes(1)
74
+ expect(shouldNotCall).not.toBeCalled()
62
75
  })
63
76
 
64
77
  it('should remove the subscription on Observable dispose', () => {
@@ -93,16 +106,28 @@ export const observableTests = describe('Observable', () => {
93
106
  expect(() => v.setValue(3)).toThrowError('Observable already disposed')
94
107
  })
95
108
 
96
- it('should throw an error for setValue() when the observer has been disposed', () => {
109
+ it('should throw an error for getValue() when the observer has been disposed', () => {
97
110
  const v = new ObservableValue(1)
98
111
  v.dispose()
99
112
  expect(() => v.getValue()).toThrowError('Observable already disposed')
100
113
  })
101
114
 
102
- it('should remove the subscription only from the disposed Observer', (done) => {
115
+ it('should throw an error for subscribe() when the observer has been disposed', () => {
116
+ const v = new ObservableValue(1)
117
+ v.dispose()
118
+ expect(() =>
119
+ v.subscribe(() => {
120
+ /** */
121
+ }),
122
+ ).toThrowError('Observable already disposed')
123
+ })
124
+
125
+ it('should remove the subscription only from the disposed Observer', () => {
126
+ const doneCallback = vi.fn()
127
+
103
128
  class Alma {
104
129
  public Callback() {
105
- done()
130
+ doneCallback()
106
131
  }
107
132
  }
108
133
  const v = new ObservableValue(1)
@@ -112,6 +137,8 @@ export const observableTests = describe('Observable', () => {
112
137
  observer.dispose()
113
138
  expect(v.getObservers().length).toBe(1)
114
139
  v.setValue(3)
140
+
141
+ expect(doneCallback).toBeCalledTimes(1)
115
142
  })
116
143
  })
117
144
  })
@@ -24,7 +24,7 @@ export type ValueChangeCallback<T> = (next: T) => void
24
24
  * ```ts
25
25
  * const observableValue = new ObservableValue<number>(0);
26
26
  * const observer = observableValue.subscribe((newValue) => {
27
- * console.log("Value changed:", newValue);
27
+ * console.log("Value changed:", newValue);
28
28
  * });
29
29
  * // To update the value
30
30
  * observableValue.setValue(Math.random());
@@ -33,7 +33,6 @@ export type ValueChangeCallback<T> = (next: T) => void
33
33
  * // if you want to dispose the whole observableValue with all of its observers:
34
34
  * observableValue.dispose();
35
35
  * ```
36
- *
37
36
  * @param T Generic argument to indicate the value type
38
37
  */
39
38
  export class ObservableValue<T> implements Disposable {
@@ -55,12 +54,14 @@ export class ObservableValue<T> implements Disposable {
55
54
 
56
55
  /**
57
56
  * Subscribes to a value changes
58
- *
59
57
  * @param callback The callback method that will be called on each change
60
58
  * @param getLast Will call the callback with the last known value right after subscription
61
59
  * @returns The ValueObserver instance
62
60
  */
63
61
  public subscribe(callback: ValueChangeCallback<T>, getLast = false) {
62
+ if (this._isDisposed) {
63
+ throw new ObservableAlreadyDisposedError()
64
+ }
64
65
  const observer = new ValueObserver<T>(this, callback)
65
66
  this.observers.add(observer)
66
67
  if (getLast) {
@@ -71,7 +72,6 @@ export class ObservableValue<T> implements Disposable {
71
72
 
72
73
  /**
73
74
  * The observer will unsubscribe from the Observable
74
- *
75
75
  * @param observer The ValueObserver instance
76
76
  * @returns if unsubscribing was successfull
77
77
  */
@@ -81,7 +81,6 @@ export class ObservableValue<T> implements Disposable {
81
81
 
82
82
  /**
83
83
  * Gets the current Value
84
- *
85
84
  * @returns The current value
86
85
  */
87
86
  public getValue(): T {
@@ -93,7 +92,6 @@ export class ObservableValue<T> implements Disposable {
93
92
 
94
93
  /**
95
94
  * Sets a new value and notifies the observers.
96
- *
97
95
  * @param newValue The new value to be set
98
96
  */
99
97
  public setValue(newValue: T) {
@@ -110,7 +108,6 @@ export class ObservableValue<T> implements Disposable {
110
108
 
111
109
  /**
112
110
  * Gets the observers
113
- *
114
111
  * @returns The subscribed observers
115
112
  */
116
113
  public getObservers() {
@@ -1,3 +1,4 @@
1
+ import { describe, it, expect } from 'vitest'
1
2
  import { PathHelper } from './path-helper'
2
3
 
3
4
  /**
@@ -6,7 +6,6 @@
6
6
  export class PathHelper {
7
7
  /**
8
8
  * Trims the slash characters from the beginning and from the end of the path to avoid duplicated slashes
9
- *
10
9
  * @param {string} path The source path that should be trimmed
11
10
  * @returns the trimmed path
12
11
  */
@@ -23,7 +22,6 @@ export class PathHelper {
23
22
  /**
24
23
  * Splits a full path into path segments,
25
24
  * e.g.: /Root/Example/stuff
26
- *
27
25
  * @param path The path to be splitted
28
26
  * @returns {string[]} the segments for the path
29
27
  */
@@ -33,7 +31,6 @@ export class PathHelper {
33
31
 
34
32
  /**
35
33
  * Method that allows to join paths without multiple or missing slashes
36
- *
37
34
  * @param args The list of the paths to join
38
35
  * @returns the joined path string
39
36
  */
@@ -43,7 +40,6 @@ export class PathHelper {
43
40
 
44
41
  /**
45
42
  * Checks if the ancestorPath is really the ancestor of the descendantPath
46
- *
47
43
  * @param {string} ancestorPath the ancestor path
48
44
  * @param {string} descendantPath the descendant path
49
45
  * @returns {boolean} if the provided path is the ancestor of the descendant
@@ -57,7 +53,6 @@ export class PathHelper {
57
53
  * e.g. "/Root/Example/Content" will return "/Root/Example"
58
54
  *
59
55
  * "Root" will always return "Root"
60
- *
61
56
  * @param path The content path
62
57
  * @returns the parent path
63
58
  */
@@ -1,3 +1,4 @@
1
+ import { describe, it, expect } from 'vitest'
1
2
  import { sleepAsync } from './sleep-async'
2
3
 
3
4
  /**
@@ -1,6 +1,5 @@
1
1
  /**
2
2
  * Returns a simple promise that will be resolved within a discrete timeout
3
- *
4
3
  * @param timeout The timeout in millisecs
5
4
  */
6
5
  export const sleepAsync = (timeout = 250) =>
@@ -1,3 +1,4 @@
1
+ import { describe, it, expect } from 'vitest'
1
2
  import './sort-by'
2
3
  describe('sortBy', () => {
3
4
  it('Should sort by ascending by default', () => {
package/src/trace.spec.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { describe, it, expect, vi } from 'vitest'
1
2
  import { Trace } from './trace'
2
3
 
3
4
  class MockClass {
@@ -30,22 +31,25 @@ class MockClass {
30
31
 
31
32
  export const traceTests = describe('Trace tests', () => {
32
33
  describe('Static method traces', () => {
33
- it('Static Methods call should be traced with args', (done) => {
34
+ it('Static Methods call should be traced with args', () => {
34
35
  const args = [1, 2, 3]
36
+ const doneCallback = vi.fn()
35
37
  const observer = Trace.method({
36
38
  object: MockClass,
37
39
  method: MockClass.addStatic,
38
40
  onCalled: (traceData) => {
39
41
  expect(args).toEqual(traceData.methodArguments)
40
42
  observer.dispose()
41
- done()
43
+ doneCallback()
42
44
  },
43
45
  })
44
46
  MockClass.addStatic(...args)
47
+ expect(doneCallback).toBeCalled()
45
48
  })
46
49
 
47
- it('Static Methods call should be traced with args and return value', (done) => {
50
+ it('Static Methods call should be traced with args and return value', () => {
48
51
  const args = [1, 2, 3]
52
+ const doneCallback = vi.fn()
49
53
  const observer = Trace.method({
50
54
  object: MockClass,
51
55
  method: MockClass.addStatic,
@@ -53,19 +57,23 @@ export const traceTests = describe('Trace tests', () => {
53
57
  expect(args).toEqual(traceData.methodArguments)
54
58
  expect(traceData.returned).toBe(1 + 2 + 3)
55
59
  observer.dispose()
56
- done()
60
+ doneCallback()
57
61
  },
58
62
  })
59
63
  MockClass.addStatic(...args)
64
+ expect(doneCallback).toBeCalled()
60
65
  })
61
66
 
62
- it("shouldn't be triggered after observer is disposed", (done) => {
67
+ it("shouldn't be triggered after observer is disposed", () => {
63
68
  const args = [1, 2, 3]
69
+ const shouldNotCall = vi.fn()
70
+ const doneCallback = vi.fn()
71
+
64
72
  const observer = Trace.method({
65
73
  object: MockClass,
66
74
  method: MockClass.addStatic,
67
75
  onCalled: () => {
68
- done("Shouldn't be triggered here")
76
+ shouldNotCall("Shouldn't be triggered here")
69
77
  },
70
78
  })
71
79
  const observer2 = Trace.method({
@@ -73,19 +81,23 @@ export const traceTests = describe('Trace tests', () => {
73
81
  method: MockClass.addStatic,
74
82
  onCalled: () => {
75
83
  observer2.dispose()
76
- done()
84
+ doneCallback()
77
85
  },
78
86
  })
79
87
  observer.dispose()
80
88
  const returned = MockClass.addStatic(...args)
81
89
  expect(returned).toEqual(1 + 2 + 3)
90
+ expect(doneCallback).toBeCalled()
91
+ expect(shouldNotCall).not.toBeCalled()
82
92
  })
83
93
  })
84
94
 
85
95
  describe('Instance method traces', () => {
86
- it('should be traced with arguments', (done) => {
96
+ it('should be traced with arguments', () => {
87
97
  const instance = new MockClass()
88
98
  const args = [1, 2, 3]
99
+ const doneCallback = vi.fn()
100
+
89
101
  const observer = Trace.method({
90
102
  object: instance,
91
103
  method: instance.addInstance,
@@ -93,15 +105,18 @@ export const traceTests = describe('Trace tests', () => {
93
105
  expect(args).toEqual(traceData.methodArguments)
94
106
  expect(traceData.returned).toBe(1 + 2 + 3)
95
107
  observer.dispose()
96
- done()
108
+ doneCallback()
97
109
  },
98
110
  })
99
111
  instance.addInstance(...args)
112
+ expect(doneCallback).toBeCalled()
100
113
  })
101
114
 
102
- it('should be traced asynchronously', (done) => {
115
+ it('should be traced asynchronously', async () => {
103
116
  const instance = new MockClass()
104
117
  const args = [1, 2, 3]
118
+ const doneCallback = vi.fn()
119
+
105
120
  const observer = Trace.method({
106
121
  object: instance,
107
122
  method: instance.addInstanceAsync,
@@ -111,14 +126,17 @@ export const traceTests = describe('Trace tests', () => {
111
126
  const { returned } = traceData
112
127
  expect(returned).toBe(1 + 2 + 3)
113
128
  observer.dispose()
114
- done()
129
+ doneCallback()
115
130
  },
116
131
  })
117
- instance.addInstanceAsync(...args)
132
+ await instance.addInstanceAsync(...args)
133
+ expect(doneCallback).toBeCalled()
118
134
  })
119
135
 
120
- it("should have a valid 'this' scope", (done) => {
136
+ it("should have a valid 'this' scope", () => {
121
137
  const instance = new MockClass('testValue')
138
+ const doneCallback = vi.fn()
139
+
122
140
  const observer = Trace.method({
123
141
  object: instance,
124
142
  method: instance.testScope,
@@ -126,15 +144,18 @@ export const traceTests = describe('Trace tests', () => {
126
144
  if (traceData.returned) {
127
145
  expect(traceData.returned).toBe('testValue')
128
146
  observer.dispose()
129
- done()
147
+ doneCallback()
130
148
  }
131
149
  },
132
150
  })
133
151
  expect(instance.testScope()).toBe('testValue')
152
+ expect(doneCallback).toBeCalled()
134
153
  })
135
154
 
136
- it('should handle throwing errors', (done) => {
155
+ it('should handle throwing errors', () => {
137
156
  const instance = new MockClass('testValue')
157
+ const doneCallback = vi.fn()
158
+
138
159
  const observer = Trace.method({
139
160
  object: instance,
140
161
  method: instance.testError,
@@ -142,17 +163,20 @@ export const traceTests = describe('Trace tests', () => {
142
163
  if (traceData.error) {
143
164
  expect(traceData.error.message).toBe('message')
144
165
  observer.dispose()
145
- done()
166
+ doneCallback()
146
167
  }
147
168
  },
148
169
  })
149
170
  expect(() => {
150
171
  instance.testError('message')
151
172
  }).toThrow()
173
+ expect(doneCallback).toBeCalled()
152
174
  })
153
175
 
154
- it('should handle throwing errors with asyncs', (done) => {
176
+ it('should handle throwing errors with asyncs', async () => {
155
177
  const instance = new MockClass('testValue')
178
+ const doneCallback = vi.fn()
179
+
156
180
  const observer = Trace.method({
157
181
  object: instance,
158
182
  method: instance.testErrorAsync,
@@ -161,18 +185,16 @@ export const traceTests = describe('Trace tests', () => {
161
185
  if (traceData.error) {
162
186
  expect(traceData.error.message).toBe('message')
163
187
  observer.dispose()
164
- done()
188
+ doneCallback()
165
189
  }
166
190
  },
167
191
  })
168
- instance
169
- .testErrorAsync('message')
170
- .then(() => {
171
- done('Should throw error')
172
- })
173
- .catch(() => {
174
- /** ignore, done handled in the onError callback */
175
- })
192
+ try {
193
+ await instance.testErrorAsync('message')
194
+ } catch (error) {
195
+ // ignore
196
+ }
197
+ expect(doneCallback).toBeCalled()
176
198
  })
177
199
  })
178
200
  })
package/src/trace.ts CHANGED
@@ -202,7 +202,6 @@ export class Trace {
202
202
 
203
203
  /**
204
204
  * Creates an observer that will be observe method calls, finishes and errors
205
- *
206
205
  * @param options The options object for the trace
207
206
  * @returns the trace object that can be disposed after usage
208
207
  */