strong-mock 8.0.1 → 9.0.0-beta.1

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 (42) hide show
  1. package/README.md +143 -51
  2. package/dist/errors/api.d.ts +13 -0
  3. package/dist/errors/diff.d.ts +4 -0
  4. package/dist/errors/unexpected-access.d.ts +5 -0
  5. package/dist/errors/unexpected-call.d.ts +14 -0
  6. package/dist/errors/verify.d.ts +8 -0
  7. package/dist/expectation/expectation.d.ts +27 -30
  8. package/dist/expectation/repository/expectation-repository.d.ts +90 -90
  9. package/dist/expectation/repository/flexible-repository.d.ts +38 -38
  10. package/dist/expectation/repository/return-value.d.ts +13 -13
  11. package/dist/expectation/strong-expectation.d.ts +30 -30
  12. package/dist/index.d.ts +14 -9
  13. package/dist/index.js +827 -511
  14. package/dist/index.js.map +1 -1
  15. package/dist/matchers/deep-equals.d.ts +16 -0
  16. package/dist/matchers/is-any.d.ts +11 -0
  17. package/dist/matchers/is-array.d.ts +22 -0
  18. package/dist/matchers/is-number.d.ts +12 -0
  19. package/dist/matchers/is-partial.d.ts +27 -0
  20. package/dist/matchers/is-plain-object.d.ts +17 -0
  21. package/dist/matchers/is-string.d.ts +15 -0
  22. package/dist/matchers/is.d.ts +9 -0
  23. package/dist/matchers/it.d.ts +10 -0
  24. package/dist/matchers/matcher.d.ts +93 -0
  25. package/dist/matchers/will-capture.d.ts +21 -0
  26. package/dist/mock/defaults.d.ts +11 -11
  27. package/dist/mock/map.d.ts +16 -16
  28. package/dist/mock/mock.d.ts +29 -29
  29. package/dist/mock/options.d.ts +99 -99
  30. package/dist/mock/stub.d.ts +5 -5
  31. package/dist/print.d.ts +10 -10
  32. package/dist/proxy.d.ts +48 -48
  33. package/dist/return/invocation-count.d.ts +44 -44
  34. package/dist/return/returns.d.ts +61 -87
  35. package/dist/verify/reset.d.ts +20 -20
  36. package/dist/verify/verify.d.ts +27 -27
  37. package/dist/when/{pending-expectation.d.ts → expectation-builder.d.ts} +26 -31
  38. package/dist/when/when.d.ts +32 -32
  39. package/package.json +21 -17
  40. package/dist/errors.d.ts +0 -28
  41. package/dist/expectation/it.d.ts +0 -29
  42. package/dist/expectation/matcher.d.ts +0 -21
@@ -1,87 +1,61 @@
1
- import type { ExpectationRepository } from '../expectation/repository/expectation-repository';
2
- import type { PendingExpectation } from '../when/pending-expectation';
3
- import type { InvocationCount } from './invocation-count';
4
- export type PromiseStub<R, P> = {
5
- /**
6
- * Set the return value for the current call.
7
- *
8
- * @param value This needs to be of the same type as the value returned
9
- * by the call inside `when`.
10
- *
11
- * @example
12
- * when(() => fn()).thenReturn(Promise.resolve(23));
13
- *
14
- * @example
15
- * when(() => fn()).thenReturn(Promise.reject({ foo: 'bar' });
16
- */
17
- thenReturn(value: P): InvocationCount;
18
- /**
19
- * Set the return value for the current call.
20
- *
21
- * @param promiseValue This needs to be of the same type as the value inside
22
- * the promise returned by the `when` callback.
23
- *
24
- * @example
25
- * when(() => fn()).thenResolve('foo');
26
- */
27
- thenResolve(promiseValue: R): InvocationCount;
28
- /**
29
- * Make the current call reject with the given error.
30
- *
31
- * @param error An `Error` instance. You can pass just a message, and
32
- * it will be wrapped in an `Error` instance. If you want to reject with
33
- * a non error then use the {@link thenReturn} method.
34
- *
35
- * @example
36
- * when(() => fn()).thenReject(new Error('oops'));
37
- */
38
- thenReject(error: Error): InvocationCount;
39
- /**
40
- * Make the current call reject with an error with the given message.
41
- *
42
- * @param message Will be wrapped in `new Error()`. If you want to reject
43
- * with a custom error then pass it here instead of the message. If you
44
- * want to reject with a non error then use `thenReturn`.
45
- *
46
- * @example
47
- * when(() => fn()).thenReject('oops');
48
- */
49
- thenReject(message: string): InvocationCount;
50
- /**
51
- * Make the current call reject with `new Error()`.
52
- */
53
- thenReject(): InvocationCount;
54
- };
55
- export type NonPromiseStub<R> = {
56
- /**
57
- * Set the return value for the current call.
58
- *
59
- * @param returnValue This needs to be of the same type as the value returned
60
- * by the `when` callback.
61
- */
62
- thenReturn(returnValue: R): InvocationCount;
63
- /**
64
- * Make the current call throw the given error.
65
- *
66
- * @param error The error instance. If you want to throw a simple `Error`
67
- * you can pass just the message.
68
- */
69
- thenThrow(error: Error): InvocationCount;
70
- /**
71
- * Make the current call throw an error with the given message.
72
- *
73
- * @param message Will be wrapped in `new Error()`. If you want to throw
74
- * a custom error pass it here instead of the message.
75
- */
76
- thenThrow(message: string): InvocationCount;
77
- /**
78
- * Make the current call throw `new Error()`.
79
- */
80
- thenThrow(): InvocationCount;
81
- };
82
- export declare const createReturns: (pendingExpectation: PendingExpectation, repository: ExpectationRepository) => {
83
- thenReturn: (returnValue: any) => InvocationCount;
84
- thenThrow: (errorOrMessage?: Error | string) => InvocationCount;
85
- thenResolve: (promiseValue: any) => InvocationCount;
86
- thenReject: (errorOrMessage?: Error | string) => InvocationCount;
87
- };
1
+ import type { ExpectationRepository } from '../expectation/repository/expectation-repository';
2
+ import type { ExpectationBuilder } from '../when/expectation-builder';
3
+ import type { InvocationCount } from './invocation-count';
4
+ export type PromiseStub<R, P> = {
5
+ /**
6
+ * Set the return value for the current call.
7
+ *
8
+ * @param value This needs to be of the same type as the value returned
9
+ * by the call inside `when`.
10
+ *
11
+ * @example
12
+ * when(() => fn()).thenReturn(Promise.resolve(23));
13
+ *
14
+ * @example
15
+ * when(() => fn()).thenReturn(Promise.reject({ foo: 'bar' });
16
+ */
17
+ thenReturn: (value: P) => InvocationCount;
18
+ /**
19
+ * Set the return value for the current call.
20
+ *
21
+ * @param promiseValue This needs to be of the same type as the value inside
22
+ * the promise returned by the `when` callback.
23
+ *
24
+ * @example
25
+ * when(() => fn()).thenResolve('foo');
26
+ */
27
+ thenResolve: (promiseValue: R) => InvocationCount;
28
+ /**
29
+ * Make the current call reject with the given error.
30
+ *
31
+ * @param error An `Error` instance. You can pass just a message, and
32
+ * it will be wrapped in an `Error` instance. If you want to reject with
33
+ * a non error then use the {@link thenReturn} method.
34
+ *
35
+ * @example
36
+ * when(() => fn()).thenReject(new Error('oops'));
37
+ */
38
+ thenReject: ((error: Error) => InvocationCount) & ((message: string) => InvocationCount) & (() => InvocationCount);
39
+ };
40
+ export type NonPromiseStub<R> = {
41
+ /**
42
+ * Set the return value for the current call.
43
+ *
44
+ * @param returnValue This needs to be of the same type as the value returned
45
+ * by the `when` callback.
46
+ */
47
+ thenReturn: (returnValue: R) => InvocationCount;
48
+ /**
49
+ * Make the current call throw the given error.
50
+ *
51
+ * @param error The error instance. If you want to throw a simple `Error`
52
+ * you can pass just the message.
53
+ */
54
+ thenThrow: ((error: Error) => InvocationCount) & ((message: string) => InvocationCount) & (() => InvocationCount);
55
+ };
56
+ export declare const createReturns: (builder: ExpectationBuilder, repository: ExpectationRepository) => {
57
+ thenReturn: (returnValue: any) => InvocationCount;
58
+ thenThrow: (errorOrMessage?: Error | string) => InvocationCount;
59
+ thenResolve: (promiseValue: any) => InvocationCount;
60
+ thenReject: (errorOrMessage?: Error | string) => InvocationCount;
61
+ };
@@ -1,20 +1,20 @@
1
- import type { Mock } from '../mock/mock';
2
- /**
3
- * Remove any remaining expectations on the given mock.
4
- *
5
- * @example
6
- * const fn = mock<() => number>();
7
- *
8
- * when(() => fn()).thenReturn(23);
9
- *
10
- * reset(fn);
11
- *
12
- * fn(); // throws
13
- */
14
- export declare const reset: (mock: Mock<any>) => void;
15
- /**
16
- * Reset all existing mocks.
17
- *
18
- * @see reset
19
- */
20
- export declare const resetAll: () => void;
1
+ import type { Mock } from '../mock/mock';
2
+ /**
3
+ * Remove any remaining expectations on the given mock.
4
+ *
5
+ * @example
6
+ * const fn = mock<() => number>();
7
+ *
8
+ * when(() => fn()).thenReturn(23);
9
+ *
10
+ * reset(fn);
11
+ *
12
+ * fn(); // throws
13
+ */
14
+ export declare const reset: (mock: Mock<any>) => void;
15
+ /**
16
+ * Reset all existing mocks.
17
+ *
18
+ * @see reset
19
+ */
20
+ export declare const resetAll: () => void;
@@ -1,27 +1,27 @@
1
- import type { ExpectationRepository } from '../expectation/repository/expectation-repository';
2
- import type { Mock } from '../mock/mock';
3
- export declare const verifyRepo: (repository: ExpectationRepository) => void;
4
- /**
5
- * Verify that all expectations on the given mock have been met.
6
- *
7
- * @throws Will throw if there are remaining expectations that were set
8
- * using `when` and that weren't met.
9
- *
10
- * @throws Will throw if any unexpected calls happened. Normally those
11
- * calls throw on their own, but the error might be caught by the code
12
- * being tested.
13
- *
14
- * @example
15
- * const fn = mock<() => number>();
16
- *
17
- * when(() => fn()).thenReturn(23);
18
- *
19
- * verify(fn); // throws
20
- */
21
- export declare const verify: <T>(mock: T) => void;
22
- /**
23
- * Verify all existing mocks.
24
- *
25
- * @see verify
26
- */
27
- export declare const verifyAll: () => void;
1
+ import type { ExpectationRepository } from '../expectation/repository/expectation-repository';
2
+ import type { Mock } from '../mock/mock';
3
+ export declare const verifyRepo: (repository: ExpectationRepository) => void;
4
+ /**
5
+ * Verify that all expectations on the given mock have been met.
6
+ *
7
+ * @throws Will throw if there are remaining expectations that were set
8
+ * using `when` and that weren't met.
9
+ *
10
+ * @throws Will throw if any unexpected calls happened. Normally those
11
+ * calls throw on their own, but the error might be caught by the code
12
+ * being tested.
13
+ *
14
+ * @example
15
+ * const fn = mock<() => number>();
16
+ *
17
+ * when(() => fn()).thenReturn(23);
18
+ *
19
+ * verify(fn); // throws
20
+ */
21
+ export declare const verify: <T>(mock: Mock<T>) => void;
22
+ /**
23
+ * Verify all existing mocks.
24
+ *
25
+ * @see verify
26
+ */
27
+ export declare const verifyAll: () => void;
@@ -1,31 +1,26 @@
1
- import type { Expectation } from '../expectation/expectation';
2
- import type { ReturnValue } from '../expectation/repository/return-value';
3
- import type { ConcreteMatcher } from '../mock/options';
4
- import type { Property } from '../proxy';
5
- /**
6
- * An expectation has to be built incrementally, starting first with the property
7
- * being accessed inside {@link createStub}, then any arguments passed to it, and ending
8
- * it with the returned value from {@link createReturns}.
9
- */
10
- export interface PendingExpectation {
11
- setProperty(prop: Property): void;
12
- setArgs(args: any[] | undefined): void;
13
- finish(returnValue: ReturnValue): Expectation;
14
- /**
15
- * Used by `pretty-format`.
16
- */
17
- toJSON(): string;
18
- }
19
- export type ExpectationFactory = (property: Property, args: any[] | undefined, returnValue: ReturnValue, concreteMatcher: ConcreteMatcher, exactParams: boolean) => Expectation;
20
- export declare class PendingExpectationWithFactory implements PendingExpectation {
21
- private createExpectation;
22
- private concreteMatcher;
23
- private exactParams;
24
- private args;
25
- private property;
26
- constructor(createExpectation: ExpectationFactory, concreteMatcher: ConcreteMatcher, exactParams: boolean);
27
- setProperty(value: Property): void;
28
- setArgs(value: any[] | undefined): void;
29
- finish(returnValue: ReturnValue): Expectation;
30
- toJSON(): string;
31
- }
1
+ import type { Expectation } from '../expectation/expectation';
2
+ import type { ReturnValue } from '../expectation/repository/return-value';
3
+ import type { ConcreteMatcher } from '../mock/options';
4
+ import type { Property } from '../proxy';
5
+ /**
6
+ * An expectation has to be built incrementally, starting first with the property
7
+ * being accessed inside {@link createStub}, then any arguments passed to it, and ending
8
+ * it with the returned value from {@link createReturns}.
9
+ */
10
+ export interface ExpectationBuilder {
11
+ setProperty: (prop: Property) => void;
12
+ setArgs: (args: unknown[] | undefined) => void;
13
+ finish: (returnValue: ReturnValue) => Expectation;
14
+ }
15
+ export type ExpectationFactory = (property: Property, args: any[] | undefined, returnValue: ReturnValue, concreteMatcher: ConcreteMatcher, exactParams: boolean) => Expectation;
16
+ export declare class ExpectationBuilderWithFactory implements ExpectationBuilder {
17
+ private createExpectation;
18
+ private concreteMatcher;
19
+ private exactParams;
20
+ private args;
21
+ private property;
22
+ constructor(createExpectation: ExpectationFactory, concreteMatcher: ConcreteMatcher, exactParams: boolean);
23
+ setProperty(value: Property): void;
24
+ setArgs(value: unknown[] | undefined): void;
25
+ finish(returnValue: ReturnValue): Expectation;
26
+ }
@@ -1,32 +1,32 @@
1
- import type { NonPromiseStub, PromiseStub } from '../return/returns';
2
- interface When {
3
- <R>(expectation: () => Promise<R>): PromiseStub<R, Promise<R>>;
4
- <R>(expectation: () => R): NonPromiseStub<R>;
5
- }
6
- /**
7
- * Set an expectation on a mock.
8
- *
9
- * The expectation must be finished by setting a return value, even if the value
10
- * is `undefined`.
11
- *
12
- * If a call happens that was not expected then the mock will throw an error.
13
- * By default, the call is expected to only be made once. Use the invocation
14
- * count helpers to expect a call multiple times.
15
- *
16
- * @param expectation A callback to set the expectation on your mock. The
17
- * callback must return the value from the mock to properly infer types.
18
- *
19
- * @example
20
- * const fn = mock<() => void>();
21
- * when(() => fn()).thenReturn(undefined);
22
- *
23
- * @example
24
- * const fn = mock<() => number>();
25
- * when(() => fn()).thenReturn(42).atMost(3);
26
- *
27
- * @example
28
- * const fn = mock<(x: number) => Promise<number>();
29
- * when(() => fn(23)).thenResolve(42);
30
- */
31
- export declare const when: When;
32
- export {};
1
+ import type { NonPromiseStub, PromiseStub } from '../return/returns';
2
+ interface When {
3
+ <R>(expectation: () => Promise<R>): PromiseStub<R, Promise<R>>;
4
+ <R>(expectation: () => R): NonPromiseStub<R>;
5
+ }
6
+ /**
7
+ * Set an expectation on a mock.
8
+ *
9
+ * The expectation must be finished by setting a return value, even if the value
10
+ * is `undefined`.
11
+ *
12
+ * If a call happens that was not expected then the mock will throw an error.
13
+ * By default, the call is expected to only be made once. Use the invocation
14
+ * count helpers to expect a call multiple times.
15
+ *
16
+ * @param expectation A callback to set the expectation on your mock. The
17
+ * callback must return the value from the mock to properly infer types.
18
+ *
19
+ * @example
20
+ * const fn = mock<() => void>();
21
+ * when(() => fn()).thenReturn(undefined);
22
+ *
23
+ * @example
24
+ * const fn = mock<() => number>();
25
+ * when(() => fn()).thenReturn(42).atMost(3);
26
+ *
27
+ * @example
28
+ * const fn = mock<(x: number) => Promise<number>();
29
+ * when(() => fn(23)).thenResolve(42);
30
+ */
31
+ export declare const when: When;
32
+ export {};
package/package.json CHANGED
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "name": "strong-mock",
3
- "version": "8.0.1",
4
- "description": "Simple type safe mocking library",
3
+ "version": "9.0.0-beta.1",
4
+ "description": "Type safe mocking library for TypeScript",
5
5
  "keywords": [
6
6
  "tdd",
7
+ "test",
7
8
  "mock",
9
+ "expectation",
10
+ "stub",
8
11
  "interface",
9
- "typescript",
10
- "test"
12
+ "type",
13
+ "typescript"
11
14
  ],
12
15
  "repository": {
13
16
  "type": "git",
@@ -21,25 +24,26 @@
21
24
  "dist"
22
25
  ],
23
26
  "dependencies": {
24
- "jest-matcher-utils": "~29.3.0",
25
- "lodash": "~4.17.0"
27
+ "jest-diff": "~29.4.3",
28
+ "jest-matcher-utils": "~29.7.0",
29
+ "lodash": "~4.17.0",
30
+ "strip-ansi": "~6.0.0"
26
31
  },
27
32
  "devDependencies": {
28
- "@nighttrax/eslint-config-ts": "~11.0.0",
29
- "@tdd-buffet/jest-config": "~5.0.1",
30
- "@tdd-buffet/tsconfig": "~1.0.0",
31
- "@types/jest": "~29.2.0",
32
- "@types/node": "~18.11.0",
33
- "@types/lodash": "~4.14.0",
33
+ "@nighttrax/eslint-config-ts": "~12.0.0-alpha.0",
34
+ "@tdd-buffet/jest-config": "~6.0.0",
35
+ "@tdd-buffet/tsconfig": "~1.0.5",
36
+ "@types/jest": "~29.5.0",
37
+ "@types/node": "~20.12.0",
38
+ "@types/lodash": "~4.17.0",
34
39
  "doctoc": "~2.2.0",
35
- "eslint": "~8.32.0",
36
- "jest": "~29.3.0",
40
+ "eslint": "~8.57.0",
41
+ "jest": "~29.7.0",
37
42
  "microbundle": "~0.15.0",
38
43
  "standard-version": "~9.5.0",
39
- "strip-ansi": "~6.0.0",
40
44
  "strong-mock": "~6.0.0",
41
- "tslib": "~2.4.0",
42
- "typescript": "~4.9.0"
45
+ "tslib": "~2.6.0",
46
+ "typescript": "~5.4.0"
43
47
  },
44
48
  "scripts": {
45
49
  "docs": "doctoc --title '**Table of Contents**' README.md",
package/dist/errors.d.ts DELETED
@@ -1,28 +0,0 @@
1
- import type { Expectation } from './expectation/expectation';
2
- import type { CallMap } from './expectation/repository/expectation-repository';
3
- import type { Property } from './proxy';
4
- import type { PendingExpectation } from './when/pending-expectation';
5
- export declare class UnfinishedExpectation extends Error {
6
- constructor(pendingExpectation: PendingExpectation);
7
- }
8
- export declare class MissingWhen extends Error {
9
- constructor();
10
- }
11
- export declare class UnexpectedAccess extends Error {
12
- constructor(property: Property, expectations: Expectation[]);
13
- }
14
- export declare class UnexpectedCall extends Error {
15
- constructor(property: Property, args: any[], expectations: Expectation[]);
16
- }
17
- export declare class NotAMock extends Error {
18
- constructor();
19
- }
20
- export declare class UnmetExpectations extends Error {
21
- constructor(expectations: Expectation[]);
22
- }
23
- export declare class UnexpectedCalls extends Error {
24
- constructor(unexpectedCalls: CallMap, expectations: Expectation[]);
25
- }
26
- export declare class NestedWhen extends Error {
27
- constructor(parentProp: Property, childProp: Property);
28
- }
@@ -1,29 +0,0 @@
1
- import type { Matcher, TypeMatcher } from './matcher';
2
- type DeepPartial<T> = T extends object ? {
3
- [K in keyof T]?: DeepPartial<T[K]>;
4
- } : T;
5
- /**
6
- * Contains argument matchers that can be used to ignore arguments in an
7
- * expectation or to match complex arguments.
8
- */
9
- export declare const It: {
10
- matches: <T>(cb: (actual: T) => boolean, { toJSON }?: {
11
- toJSON?: (() => string) | undefined;
12
- }) => TypeMatcher<T>;
13
- deepEquals: <T_1>(expected: T_1, { strict }?: {
14
- strict?: boolean | undefined;
15
- }) => TypeMatcher<T_1>;
16
- is: <T_2 = unknown>(expected: T_2) => TypeMatcher<T_2>;
17
- isAny: () => TypeMatcher<any>;
18
- isObject: <T_3 extends object, K extends DeepPartial<T_3>>(partial?: K | undefined) => TypeMatcher<T_3>;
19
- isNumber: () => TypeMatcher<number>;
20
- isString: ({ matching, containing, }?: {
21
- matching?: RegExp | undefined;
22
- containing?: string | undefined;
23
- }) => TypeMatcher<string>;
24
- isArray: <T_4 extends any[]>(containing?: T_4 | undefined) => TypeMatcher<T_4>;
25
- willCapture: <T_5 = unknown>(name?: string) => T_5 & Matcher & {
26
- value: T_5 | undefined;
27
- };
28
- };
29
- export {};
@@ -1,21 +0,0 @@
1
- export declare const MATCHER_SYMBOL: unique symbol;
2
- export type Matcher = {
3
- /**
4
- * Will be called with a value to match against.
5
- */
6
- matches: (arg: any) => boolean;
7
- [MATCHER_SYMBOL]: boolean;
8
- /**
9
- * Used by `pretty-format`.
10
- */
11
- toJSON(): string;
12
- };
13
- /**
14
- * This takes the shape of T to satisfy call sites, but strong-mock will only
15
- * care about the matcher type.
16
- */
17
- export type TypeMatcher<T> = T & Matcher;
18
- /**
19
- * Used to test if an expectation on an argument is a custom matcher.
20
- */
21
- export declare function isMatcher(f: unknown): f is Matcher;