-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow overriding the default matcher
Relates to #252.
- Loading branch information
Showing
5 changed files
with
128 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,22 @@ | ||
/* istanbul ignore file */ | ||
import { instance } from './instance/instance'; | ||
import { It } from './expectation/matcher'; | ||
import { It, Matcher } from './expectation/matcher'; | ||
import { setDefaults, StrongMockDefaults } from './mock/defaults'; | ||
import { mock } from './mock/mock'; | ||
import { reset, resetAll } from './verify/reset'; | ||
import { verify, verifyAll } from './verify/verify'; | ||
import { when } from './when/when'; | ||
|
||
export { mock, when, instance, verify, verifyAll, reset, resetAll, It }; | ||
export { | ||
mock, | ||
when, | ||
instance, | ||
verify, | ||
verifyAll, | ||
reset, | ||
resetAll, | ||
It, | ||
setDefaults, | ||
}; | ||
|
||
export type { StrongMockDefaults, Matcher }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { describe, it, beforeEach } from 'tdd-buffet/suite/node'; | ||
import { expect } from 'tdd-buffet/expect/jest'; | ||
import { instance, It, when } from '../index'; | ||
import { setDefaults } from './defaults'; | ||
import { mock } from './mock'; | ||
|
||
describe('defaults', () => { | ||
beforeEach(() => { | ||
setDefaults({}); | ||
}); | ||
|
||
it('should override the matcher for non matcher values', () => { | ||
setDefaults({ | ||
matcher: () => It.matches(() => true), | ||
}); | ||
|
||
const fn = mock<(x: number) => boolean>(); | ||
|
||
when(fn(1)).thenReturn(true); | ||
|
||
expect(instance(fn)(-1)).toBeTruthy(); | ||
}); | ||
|
||
it('should not override the matcher for matcher values', () => { | ||
setDefaults({ | ||
matcher: () => It.matches(() => true), | ||
}); | ||
|
||
const fn = mock<(x: number) => boolean>(); | ||
|
||
when(fn(It.matches((x) => x === 1))).thenReturn(true); | ||
|
||
expect(() => instance(fn)(-1)).toThrow(); | ||
}); | ||
|
||
it('should not stack', () => { | ||
setDefaults({ | ||
matcher: () => It.matches(() => true), | ||
}); | ||
|
||
setDefaults({}); | ||
|
||
const fn = mock<(x: number) => boolean>(); | ||
|
||
when(fn(1)).thenReturn(true); | ||
|
||
expect(() => instance(fn)(-1)).toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { deepEquals, Matcher } from '../expectation/matcher'; | ||
|
||
export type StrongMockDefaults = { | ||
/** | ||
* The matcher that will be used when one isn't specified explicitly. | ||
* | ||
* @param expected The non matcher expected value. | ||
* | ||
* @example | ||
* StrongMock.setDefaults({ | ||
* matcher: () => It.matches(() => true) | ||
* }); | ||
* | ||
* when(fn('value')).thenReturn(true); | ||
* | ||
* instance(fn('not-value')) === true; | ||
*/ | ||
matcher: <T>(expected: T) => Matcher; | ||
}; | ||
|
||
const defaults: StrongMockDefaults = { | ||
matcher: deepEquals, | ||
}; | ||
|
||
export let currentDefaults: StrongMockDefaults = defaults; | ||
|
||
/** | ||
* Override strong-mock's defaults. | ||
* | ||
* @param newDefaults These will be applied to the library defaults. Multiple | ||
* calls don't stack e.g. calling this with `{}` will clear any previously | ||
* applied defaults. | ||
*/ | ||
export const setDefaults = (newDefaults: Partial<StrongMockDefaults>): void => { | ||
currentDefaults = { | ||
...defaults, | ||
...newDefaults, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters