generated from AthennaIO/Template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from AthennaIO/develop
feat(mock): add mock API
- Loading branch information
Showing
16 changed files
with
834 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* @athenna/test | ||
* | ||
* (c) João Lenon <lenon@athenna.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { createSandbox } from 'sinon' | ||
import { MockBuilder } from '#src/mocks/MockBuilder' | ||
import type { StubMethod } from '#src/types/StubMethod' | ||
import type { SpyMethod, SpyInstance } from '#src/types' | ||
import type { StubInstance } from '#src/types/StubInstance' | ||
|
||
export class Mock { | ||
/** | ||
* Sinon sandbox instance. | ||
*/ | ||
public static sandbox = createSandbox() | ||
|
||
/** | ||
* Create a mock builder instance for the given object | ||
* and a method of the object. | ||
*/ | ||
public static when<T = any>(object: T, method: keyof T): MockBuilder { | ||
return new MockBuilder(object, method, Mock.sandbox) | ||
} | ||
|
||
/** | ||
* Create a spy function for a given object. | ||
*/ | ||
public static spy<T = any>(object: T): SpyInstance<T> | ||
|
||
/** | ||
* Create a spy function for a given object method. | ||
*/ | ||
public static spy<T = any>(object: T, method: keyof T): SpyMethod<T[keyof T]> | ||
|
||
public static spy<T = any>(object: T, method?: keyof T) { | ||
return Mock.sandbox.spy(object, method) | ||
} | ||
|
||
/** | ||
* Create a stub function for a given object. | ||
*/ | ||
public static stub<T = any>(object: T): StubInstance<T> | ||
|
||
/** | ||
* Create a stub function for a given object method. | ||
*/ | ||
public static stub<T = any>( | ||
object: T, | ||
method: keyof T | ||
): StubMethod<T[keyof T]> | ||
|
||
public static stub<T = any>(object: T, method?: keyof T) { | ||
return Mock.sandbox.stub(object, method) | ||
} | ||
|
||
/** | ||
* Restore all mocks to default. | ||
*/ | ||
public static restoreAll(): void { | ||
Mock.sandbox.restore() | ||
Mock.sandbox.resetHistory() | ||
Mock.sandbox.resetBehavior() | ||
Mock.sandbox.reset() | ||
} | ||
} |
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,100 @@ | ||
/** | ||
* @athenna/test | ||
* | ||
* (c) João Lenon <lenon@athenna.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import type { Stub } from '#src/types' | ||
import type { SinonSandbox } from 'sinon' | ||
import type { Exception } from '@athenna/common' | ||
|
||
export class MockBuilder { | ||
public constructor( | ||
private object: any, | ||
private method: any, | ||
private sandbox: SinonSandbox | ||
) {} | ||
|
||
/** | ||
* Mock the method to return the given value. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Mock } from '@athenna/test' | ||
* | ||
* const mock = Mock.when(console, 'log').return('Hello World') | ||
* | ||
* mock.called // false | ||
* ``` | ||
*/ | ||
public return<T = any>(value: T): Stub { | ||
const stub = this.sandbox.stub(this.object, this.method) | ||
|
||
stub.returns(value) | ||
|
||
return stub | ||
} | ||
|
||
/** | ||
* Mock the method to throw the given value. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Mock } from '@athenna/test' | ||
* | ||
* const mock = Mock.when(console, 'log').throw('Hello World') | ||
* | ||
* mock.called // false | ||
* ``` | ||
*/ | ||
public throw(value: string | Error | Exception): Stub { | ||
const stub = this.sandbox.stub(this.object, this.method) | ||
|
||
stub.throws(value) | ||
|
||
return stub | ||
} | ||
|
||
/** | ||
* Mock the method to resolve returning the given value. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Mock } from '@athenna/test' | ||
* | ||
* const mock = Mock.when(console, 'log').resolve('Hello World') | ||
* | ||
* mock.called // false | ||
* ``` | ||
*/ | ||
public resolve<T = any>(value: T): Stub { | ||
const stub = this.sandbox.stub(this.object, this.method) | ||
|
||
stub.resolves(value) | ||
|
||
return stub | ||
} | ||
|
||
/** | ||
* Mock the method to reject returning the given value. | ||
* | ||
* @example | ||
* ```ts | ||
* import { Mock } from '@athenna/test' | ||
* | ||
* const mock = Mock.when(console, 'log').reject('Hello World') | ||
* | ||
* mock.called // false | ||
* ``` | ||
*/ | ||
public reject<T = any>(value: T): Stub { | ||
const stub = this.sandbox.stub(this.object, this.method) | ||
|
||
stub.rejects(value) | ||
|
||
return stub | ||
} | ||
} |
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,15 @@ | ||
/** | ||
* @athenna/test | ||
* | ||
* (c) João Lenon <lenon@athenna.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import type { SinonSpy } from 'sinon' | ||
|
||
export type Spy< | ||
TArgs extends readonly any[] = any[], | ||
TReturnValue = any | ||
> = SinonSpy<TArgs, TReturnValue> |
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,14 @@ | ||
/** | ||
* @athenna/test | ||
* | ||
* (c) João Lenon <lenon@athenna.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import type { SpyMethod } from '#src/types/SpyMethod' | ||
|
||
export type SpyInstance<T> = { | ||
[P in keyof T]: SpyMethod<T[P]> | ||
} |
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,12 @@ | ||
/** | ||
* @athenna/test | ||
* | ||
* (c) João Lenon <lenon@athenna.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import type { SinonSpiedMember } from 'sinon' | ||
|
||
export type SpyMethod<T> = SinonSpiedMember<T> |
Oops, something went wrong.