a 4.0.3 → 4.0.5

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 (3) hide show
  1. package/README.md +5 -134
  2. package/index.d.ts +138 -101
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,11 +1,10 @@
1
1
  _a_
2
2
  ===
3
- _Mocking framework_ + _testing framework_.
3
+ _Mocking framework_
4
4
 
5
5
 
6
6
  The mocking framework can be used in any JavaScript testing framework.
7
7
 
8
- The testing framework has a short and concise bdd syntax with reusable contexts.
9
8
 
10
9
  __To install:__
11
10
 
@@ -44,6 +43,8 @@ original(); //returns 'fake'
44
43
  original(); //returns 'realValue'
45
44
  ```
46
45
 
46
+ Note: Consumers do not need to provide a `thisArg`. It is optional and only used to force a specific `this` when the original fallback is called (low-level partial mock usage).
47
+
47
48
 
48
49
 
49
50
  __Strict mock__
@@ -158,7 +159,7 @@ __Ignoring all arguments__
158
159
 
159
160
  ```js
160
161
  var mock = require('a').mock();
161
- mock.expectAnything().return('fake1'); //same as expectAnything
162
+ mock.ignoreAll().return('fake1'); //same as expectAnything
162
163
 
163
164
  mock('someRandomValue', 'whatever'); //returns 'fake1'
164
165
  mock(); //throws unexpected arguments
@@ -611,134 +612,4 @@ when(c).then(it => {
611
612
  it('....').assertXXXX();
612
613
  });
613
614
 
614
- ```
615
-
616
- Release Notes
617
- ---------------
618
- __3.0.0__
619
- - The the testing framework is moved to separate repo: [npmjs.com/package/a_test](https://npmjs.com/package/a_test)
620
-
621
- __2.1.2__
622
- - [typo in docs](https://github.com/alfateam/a/pull/4)
623
-
624
- __2.1.0__
625
- - short hand syntax for returning promises (sync)
626
-
627
- __2.0.13__
628
- - a_mock 1.0.4, implements promise mock which is synchronous
629
-
630
- __2.0.12__
631
- - assertOk(falsy) would not throw (#21)
632
- - report suites that cannot be loaded as unrunnable (#20)
633
- - fix async test result reporting order
634
-
635
- __2.0.11__
636
- - README update
637
-
638
- __2.0.10__
639
- - disable runtime babel transpiling by default, set A_TEST_BABEL_REGISTER environment variable to enable it.
640
-
641
- __2.0.9__
642
- - replace dependency for coloured console output in test runner.
643
-
644
- __2.0.8__
645
- - update README
646
-
647
- __2.0.7__
648
- - test runner is able to load modules exporting a default function.
649
-
650
- __2.0.6__
651
- - test runner reports when a file with tests is not runnable
652
-
653
- __2.0.5__
654
- - test runner uses _node_ instead of _nodejs_
655
-
656
- __2.0.4__
657
- - correct reporting of aborted promise mocks
658
-
659
- __2.0.3__
660
- - abort Unfulfilled promise mocks after 10secs.
661
-
662
- __2.0.2__
663
- - forgotten dependency version
664
-
665
- __2.0.1__
666
- - Fix bin section in package.json.
667
- - Fix memory leak in the test runner
668
-
669
- __2.0.0__
670
- BREAKING CHANGE: Support async testing.
671
- - Tests relying on ability of [deferred][2] to resolve synchronously expected to cause problems.
672
- - Unfulfilled promises will prevent runner from exiting.
673
- - Runner uses babel-runtime which implies strict mode.
674
-
675
- __1.0.1__
676
- ExpectAnything() can be nested - for backwards compatability only.
677
- __1.0.0__
678
- ExpectAnything() no longer expects only one argument, but arbitary number of arguments. Use ignore() if you want to ignore a single argument.
679
- __0.4.8__
680
- Executable test runner "when" is deprecated. Use "a" instead.
681
- __0.4.7__
682
- Inconclusive tests are accounted as failed in exit code.
683
- __0.4.6__
684
- Fixed memory leak in test runner.
685
- __0.4.5__
686
- Display stack trace of inconclusive suites.
687
- Use dependency [deferred][2] instead of [promise][3].
688
- __0.4.4__
689
- Introduced promise mocks.
690
- Tests with failing setup are reported as inconclusive.
691
- Bugfix: Test names no longer converted to lowercase.
692
- __0.4.3__
693
- Can reset expectations on mocks by mock.reset.
694
- Renamed expectRequire.clear to expectRequire.reset. Same goes for for requireMock.
695
- __0.4.2__
696
- Can clear expectations on require by using expectRequire.clear.
697
- __0.4.1__
698
- "When" can accept function instead of separate act file. See example in [demo][1] repo.
699
- __0.4.0__
700
- Cleaner output on failed assertions.
701
- __0.3.9__
702
- Can inherit act by convention. See examples in [demo][0] repo.
703
- __0.3.8__
704
- Cleaner stack trace on mock errors.
705
- __0.3.7__
706
- Test path can be sent as argument to test runner.
707
- If no path is specified, the test runner will run from current directory.
708
- Example: ```when c:/devel/foo/testFolder```
709
- __0.3.6__
710
- Exit code is equal to number of failing tests.
711
- __0.3.5__
712
- Tests files are run in hierarchical order from top to bottom.
713
- __0.3.4__
714
- Cache was cleared at wrong time. This could lead to overflow when running large amount of tests.
715
- Make sure you update globally (npm update a -g) to get this fix, not only the local dependency.
716
- __0.3.3__
717
- Error in documentation about structs.
718
- __0.3.2__
719
- Mocks can be set up to throw.
720
- __0.3.1__
721
- "when" deletes all cached modules before executing. This ensures tests are isolated.
722
- ignore is alias for expectAnything.
723
- "When" can resolve act by camcelCase convention. If test class is named "whenFoo.js", it will assume "foo.js" is the act.
724
-
725
- __0.3.0__
726
- expectArray is deprecated, use expect instead.
727
- expect now handles structs - equality is acheived when same propertyNames and equal leaf properties.
728
-
729
- __0.2.9__
730
- "When" can resolve act by convention. If test class is named "when_foo.js", it will assume "foo.js" is the act.
731
- Example, given when_foo.js:
732
- ```js
733
- var c = {};
734
- var when = require('a').when;
735
-
736
- when(c). //equivalent to: when('./foo',c)....
737
- it('should have value equal to 1').
738
- assertEqual(1, c.sut.value);
739
-
740
- ```
741
- [0]:https://github.com/alfateam/a_demo
742
- [1]:https://github.com/alfateam/a_demo/blob/master/assert_specs/when_assertions.js
743
- [2]:https://www.npmjs.org/package/deferred
744
- [3]:https://www.npmjs.org/package/promise
615
+ ```
package/index.d.ts CHANGED
@@ -1,101 +1,138 @@
1
- type AnyFunction = (...args: any[]) => any;
2
- type Tail<T extends any[]> = T extends [any, ...infer R] ? R : [];
3
- type MockedFunction<T extends AnyFunction> = MockFunction<
4
- Parameters<T>,
5
- ReturnType<T>
6
- > &
7
- T;
8
-
9
- interface RepeatControl {
10
- repeat(times: number): RepeatControl;
11
- repeatAny(): RepeatControl;
12
- }
13
-
14
- interface ExpectationTerminal<R, TArgs extends any[]> {
15
- return(value?: R): RepeatControl;
16
- whenCalled(callback: (...args: TArgs) => void): ExpectationTerminal<R, TArgs>;
17
- repeat(times: number): RepeatControl;
18
- repeatAny(): RepeatControl;
19
- throw(error: unknown): ExpectationTerminal<R, TArgs>;
20
- resolve(value: any): RepeatControl;
21
- reject(value: any): RepeatControl;
22
- }
23
-
24
- interface ExpectationChain<TArgs extends any[], R>
25
- extends ExpectationTerminal<R, TArgs> {
26
- expect(arg: TArgs[0]): ExpectationChain<Tail<TArgs>, R>;
27
- expectAnything(): ExpectationChain<Tail<TArgs>, R>;
28
- ignore(): ExpectationChain<Tail<TArgs>, R>;
29
- expectArray(
30
- value: TArgs[0] extends any[] ? TArgs[0] : any[]
31
- ): ExpectationChain<Tail<TArgs>, R>;
32
- }
33
-
34
- interface MockFunction<TArgs extends any[], R> {
35
- (...args: TArgs): R;
36
- expect(): ExpectationTerminal<R, TArgs>;
37
- expect(...args: TArgs): ExpectationTerminal<R, TArgs>;
38
- expect(arg: TArgs[0]): ExpectationChain<Tail<TArgs>, R>;
39
- expectAnything(): ExpectationChain<Tail<TArgs>, R>;
40
- ignore(): ExpectationChain<Tail<TArgs>, R>;
41
- expectArray(
42
- value: TArgs[0] extends any[] ? TArgs[0] : any[]
43
- ): ExpectationChain<Tail<TArgs>, R>;
44
- verify(): true;
45
- reset(): void;
46
- }
47
-
48
- type Mocked<T> = T extends AnyFunction
49
- ? MockedFunction<T>
50
- : T extends object
51
- ? { [K in keyof T]: Mocked<T[K]> } & { verify: () => true }
52
- : T;
53
-
54
- interface RequireExpectation {
55
- return(value: any): RequireExpectation;
56
- repeat(times: number): RequireExpectation;
57
- repeatAny(): RequireExpectation;
58
- whenCalled(callback: (...args: any[]) => void): RequireExpectation;
59
- }
60
-
61
- interface ThenableMock<T = any> {
62
- (valueToResolveWith?: T | null, errorToFailWith?: any): ThenableMock<T>;
63
- then<U = T>(
64
- success?: (value: T) => U | ThenableMock<U> | PromiseLike<U>,
65
- fail?: (error: any) => U | ThenableMock<U> | PromiseLike<U>
66
- ): ThenableMock<U>;
67
- resolve(value?: T): ThenableMock<T>;
68
- reject(error: any): ThenableMock<never>;
69
- }
70
-
71
- declare function mock<TArgs extends any[] = any[], R = any>(): MockFunction<
72
- TArgs,
73
- R
74
- >;
75
- declare function mock<T extends AnyFunction>(
76
- original: T
77
- ): MockedFunction<T>;
78
- declare function mock<T extends object>(subject: T): Mocked<T>;
79
-
80
- declare function expectRequire(moduleName: string): RequireExpectation;
81
- declare namespace expectRequire {
82
- function reset(): void;
83
- }
84
-
85
- declare function requireMock<TArgs extends any[] = any[], R = any>(
86
- moduleName: string
87
- ): MockFunction<TArgs, R>;
88
- declare namespace requireMock {
89
- function reset(): void;
90
- }
91
-
92
- declare function promise<T = any>(): ThenableMock<T>;
93
-
94
- declare const aMock: {
95
- mock: typeof mock;
96
- expectRequire: typeof expectRequire;
97
- requireMock: typeof requireMock;
98
- promise: typeof promise;
99
- };
100
-
101
- export = aMock;
1
+ import type { AxiosInstance, AxiosResponse } from 'axios';
2
+
3
+ type AnyFunction = (...args: any[]) => any;
4
+ type Tail<T extends any[]> = T extends [any, ...infer R] ? R : [];
5
+ type OverloadedParameters<T> = T extends {
6
+ (...args: infer A): any;
7
+ (...args: infer B): any;
8
+ }
9
+ ? A | B
10
+ : T extends (...args: infer A) => any
11
+ ? A
12
+ : never;
13
+ type OverloadedReturnType<T> = T extends {
14
+ (...args: any[]): infer A;
15
+ (...args: any[]): infer B;
16
+ }
17
+ ? A | B
18
+ : T extends (...args: any[]) => infer A
19
+ ? A
20
+ : never;
21
+ type OptionalTuple<T extends any[]> = { [K in keyof T]?: T[K] };
22
+ type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
23
+ type CallbackFor<TArgs> = TArgs extends any[] ? (...args: TArgs) => void : never;
24
+ type FunctionProps<T> = T extends (...args: any[]) => any
25
+ ? { [K in Exclude<keyof T, keyof Function>]: Mocked<T[K]> }
26
+ : {};
27
+ type MockedFunction<T extends AnyFunction> = MockFunction<
28
+ OverloadedParameters<T>,
29
+ T extends AxiosInstance
30
+ ? Promise<AxiosResponse<any, any, {}>>
31
+ : OverloadedReturnType<T>
32
+ > &
33
+ T &
34
+ FunctionProps<T>;
35
+
36
+ interface RepeatControl {
37
+ repeat(times: number): RepeatControl;
38
+ repeatAny(): RepeatControl;
39
+ }
40
+
41
+ interface ExpectationTerminal<R, TArgs extends any[], TOrigArgs extends any[] = TArgs> {
42
+ return(value: R): RepeatControl;
43
+ returnAny(value?: any): RepeatControl;
44
+ whenCalled(callback: CallbackFor<TOrigArgs>): ExpectationTerminal<R, TArgs, TOrigArgs>;
45
+ repeat(times: number): RepeatControl;
46
+ repeatAny(): RepeatControl;
47
+ throw(error: any): ExpectationTerminal<R, TArgs, TOrigArgs>;
48
+ resolve(value: UnwrapPromise<R>): RepeatControl;
49
+ resolveLoose(value?: any): RepeatControl;
50
+ reject(error: Error): RepeatControl;
51
+ }
52
+
53
+ interface ExpectationChain<TArgs extends any[], R, TOrigArgs extends any[] = TArgs>
54
+ extends ExpectationTerminal<R, TArgs, TOrigArgs> {
55
+ expect(arg?: TArgs[0]): ExpectationChain<Tail<TArgs>, R, TOrigArgs>;
56
+ /** @deprecated Use ignoreAll() instead. */
57
+ expectAnything(): ExpectationChain<any[], R, TOrigArgs>;
58
+ ignoreAll(): ExpectationChain<any[], R, TOrigArgs>;
59
+ expectLoose(...args: any[]): ExpectationChain<any[], any, TOrigArgs>;
60
+ ignore(): ExpectationChain<Tail<TArgs>, R, TOrigArgs>;
61
+ /** @deprecated Use expect() instead. */
62
+ expectArray(
63
+ value?: TArgs[0] extends any[] ? TArgs[0] : any[]
64
+ ): ExpectationChain<Tail<TArgs>, R, TOrigArgs>;
65
+ }
66
+
67
+ interface MockFunction<TArgs extends any[], R> {
68
+ (...args: TArgs): R;
69
+ // Allow any-args calls in addition to the original signature.
70
+ (...args: any[]): any;
71
+ expect(...args: OptionalTuple<TArgs>): ExpectationChain<Tail<TArgs>, R, TArgs>;
72
+ ignore(): ExpectationChain<Tail<TArgs>, R, TArgs>;
73
+ /** @deprecated Use expect() instead. */
74
+ expectArray(
75
+ value?: TArgs[0] extends any[] ? TArgs[0] : any[]
76
+ ): ExpectationChain<Tail<TArgs>, R, TArgs>;
77
+ /** @deprecated Use ignoreAll() instead. */
78
+ expectAnything(): ExpectationChain<any[], R, TArgs>;
79
+ ignoreAll(): ExpectationChain<any[], R, TArgs>;
80
+ // Explicit loose expectations.
81
+ expectLoose(...args: any[]): ExpectationChain<any[], any, TArgs>;
82
+ verify(): true;
83
+ reset(): void;
84
+ }
85
+
86
+ type Mocked<T> = T extends AnyFunction
87
+ ? MockedFunction<T>
88
+ : T extends object
89
+ ? { [K in keyof T]: Mocked<T[K]> } & { verify: () => true }
90
+ : T;
91
+
92
+ interface RequireExpectation {
93
+ return(value: any): RequireExpectation;
94
+ repeat(times: number): RequireExpectation;
95
+ repeatAny(): RequireExpectation;
96
+ whenCalled(callback: (...args: any[]) => void): RequireExpectation;
97
+ }
98
+
99
+ interface ThenableMock<T = any> {
100
+ (valueToResolveWith?: T | null, errorToFailWith?: any): ThenableMock<T>;
101
+ then<U = T>(
102
+ success?: (value: T) => U | ThenableMock<U> | PromiseLike<U>,
103
+ fail?: (error: any) => U | ThenableMock<U> | PromiseLike<U>
104
+ ): ThenableMock<U>;
105
+ resolve(value?: T): ThenableMock<T>;
106
+ reject(error: any): ThenableMock<never>;
107
+ }
108
+
109
+ declare function mock<T = any[], R = any>(): T extends any[]
110
+ ? MockFunction<T, R>
111
+ : Mocked<T>;
112
+ declare function mock<T extends AnyFunction>(
113
+ original: T
114
+ ): MockedFunction<T>;
115
+ declare function mock<T extends object>(subject: T): Mocked<T>;
116
+
117
+ declare function expectRequire(moduleName: string): RequireExpectation;
118
+ declare namespace expectRequire {
119
+ function reset(): void;
120
+ }
121
+
122
+ declare function requireMock<TArgs extends any[] = any[], R = any>(
123
+ moduleName: string
124
+ ): MockFunction<TArgs, R>;
125
+ declare namespace requireMock {
126
+ function reset(): void;
127
+ }
128
+
129
+ declare function promise<T = any>(): ThenableMock<T>;
130
+
131
+ declare const aMock: {
132
+ mock: typeof mock;
133
+ expectRequire: typeof expectRequire;
134
+ requireMock: typeof requireMock;
135
+ promise: typeof promise;
136
+ };
137
+
138
+ export = aMock;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "a",
3
- "version": "4.0.3",
3
+ "version": "4.0.5",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "title": "a",