Skip to content

Commit

Permalink
feat!: Control mock strictness through setDefaults
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The default strictness now avoids immediately throwing
on unexpected property access. This should lead to improved error
messages, and less breaking on common code patterns e.g. destructuring.

If you want the old behavior use:

```
import { setDefaults, Strictness } from 'strong-mock';

setDefaults({ strictness: Strictness.SUPER_STRICT });
```
  • Loading branch information
NiGhTTraX committed Aug 2, 2022
1 parent 71bb975 commit 6590713
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { reset, resetAll } from './verify/reset';
export { verify, verifyAll } from './verify/verify';
export { It } from './expectation/it';
export { setDefaults } from './mock/defaults';
export { Strictness } from './expectation/repository/strong-repository';

export type { Matcher } from './expectation/matcher';
export type { StrongMockDefaults } from './mock/defaults';
10 changes: 10 additions & 0 deletions src/mock/defaults.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { UnexpectedAccess } from '../errors';
import { Strictness } from '../expectation/repository/strong-repository';
import { It, when } from '../index';
import { setDefaults } from './defaults';
import { mock } from './mock';
Expand Down Expand Up @@ -31,6 +33,14 @@ describe('defaults', () => {
expect(() => fn(-1)).toThrow();
});

it('should override the strictness', () => {
setDefaults({ strictness: Strictness.SUPER_STRICT });

const fn = mock<{ foo: () => number }>();

expect(() => fn.foo()).toThrow(UnexpectedAccess);
});

it('should not stack', () => {
setDefaults({
matcher: () => It.matches(() => true),
Expand Down
8 changes: 8 additions & 0 deletions src/mock/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { It } from '../expectation/it';
import { Matcher } from '../expectation/matcher';
import { Strictness } from '../expectation/repository/strong-repository';

export type StrongMockDefaults = {
/**
Expand All @@ -17,10 +18,17 @@ export type StrongMockDefaults = {
* fn('not-value') === true;
*/
matcher: <T>(expected: T) => Matcher;

/**
* Controls what happens when a property is accessed, or a call is made,
* and there are no expectations set for it.
*/
strictness: Strictness;
};

const defaults: StrongMockDefaults = {
matcher: It.deepEquals,
strictness: Strictness.STRICT,
};

export let currentDefaults: StrongMockDefaults = defaults;
Expand Down
2 changes: 1 addition & 1 deletion src/mock/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const mock = <T>(): Mock<T> => {
strongExpectationFactory
);

const repository = new StrongRepository();
const repository = new StrongRepository(currentDefaults.strictness);
const stub = createStub<T>(repository, pendingExpectation, () => isRecording);

setMockState(stub, { repository, pendingExpectation });
Expand Down

0 comments on commit 6590713

Please sign in to comment.