Skip to content

Commit

Permalink
feat(mock): mock by args and calls (#119)
Browse files Browse the repository at this point in the history
<!-- CLICK "Preview" FOR INSTRUCTIONS IN A MORE READABLE FORMAT -->

## Proposed changes

Describe the big picture of your changes here to communicate to the 
maintainers why we should accept this pull request. If it fixes a bug 
or resolves a feature request, be sure to link to that issue.

## Types of changes

What types of changes does your code introduce?

_Put an `x` in the boxes that apply_

- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)

## Checklist

_Put an `x` in the boxes that apply. You can also fill these out after 
creating the PR. If you're unsure about any of them, don't hesitate to
ask.
We're here to help! This is simply a reminder of what we are going to
look
for before merging your code._

- [ ] I have read the
[CONTRIBUTING](https://github.com/AthennaIO/Test/blob/master/CONTRIBUTING.md)
documentation
- [ ] Lint and unit tests pass locally with my changes
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added necessary documentation (if appropriate)

## Further comments

If this is a relatively large or complex change, kick off the discussion
by
explaining why you chose the solution you did and what alternatives you 
considered, etc...

---------

Co-authored-by: snyk-bot <[email protected]>
  • Loading branch information
jlenon7 and snyk-bot authored Jan 9, 2024
1 parent 5981270 commit 041bec9
Show file tree
Hide file tree
Showing 13 changed files with 820 additions and 65 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/test",
"version": "4.17.0",
"version": "4.18.0",
"description": "The Athenna test runner. Built on top of Japa.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion src/exceptions/AfterAllHookException.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class AfterAllHookException extends Exception {
const hook = Color.green.bold('@AfterAll')
const classMethod = Color.yellow.bold(`${className}.${method}`)
const message = `${Color.gray.bold.bgYellow(' MESSAGE ')}\n\n${
error.message
error.message ? error.message : JSON.stringify(error, null, 2)
}`

error.message = `An exception has occurred while running the ${hook} hook in ${classMethod} method.\n\n${message}`
Expand Down
95 changes: 93 additions & 2 deletions src/globals/Assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,44 +122,87 @@ export {}
declare module '@japa/assert' {
export interface Assert {
throws(fn: () => any, errType: any, message?: string): void
doesNotThrows(fn: () => any, errType: any, message?: string): void
doesNotThrow(fn: () => any, errType: any, message?: string): void
rejects(
fn: () => any | Promise<any>,
errType: any,
message?: string
): Promise<any>
doesNotRejects(
doesNotReject(
fn: () => any | Promise<any>,
errType: any,
message?: string
): Promise<any>
/**
* Assert that the given mock was called.
*
* @example
* ```ts
* console.log('hello', 'world', '!')
* assert.called(console.log) // passes
* ```
*/
called(mock: any): void

/**
* Assert that the given mock was not called.
*
* @example
* ```ts
* assert.notCalled(console.log) // passes
* ```
*/
notCalled(mock: any): void

/**
* Assert that the given mock was called only once.
*
* @example
* ```ts
* console.log('hello', 'world', '!')
* assert.calledOnce(console.log) // passes
* ```
*/
calledOnce(mock: any): void

/**
* Assert that the given mock was called the
* determined number of times.
*
* @example
* ```ts
* console.log('hello', 'world', '!')
* console.log('hello', 'world', '!')
* console.log('hello', 'world', '!')
* assert.calledTimes(console.log, 3) // passes
* ```
*/
calledTimes(mock: any, times: number): void

/**
* Assert that the given mock was called with the
* determined arguments.
*
* @example
* ```ts
* console.log('hello', 'world', '!')
* assert.calledWith(console.log, 'hello', 'world', '!') // passes
* ```
*/
calledWith(mock: any, ...args: any[]): void

/**
* Assert that the given mock was not called with the
* determined arguments.
*
* @example
* ```ts
* console.log('hello', 'world', '!')
* assert.notCalledWith(console.log, 'hello', 'world') // passes
* ```
*/
notCalledWith(mock: any, ...args: any[]): void

/**
* Assert that the given mock was called with the
* arguments matching some of the given arguments.
Expand All @@ -173,6 +216,7 @@ declare module '@japa/assert' {
* ```
*/
calledWithMatch(mock: any, ...args: any[]): void

/**
* Assert that the given mock was not called with the
* arguments matching some of the given arguments.
Expand All @@ -186,35 +230,82 @@ declare module '@japa/assert' {
* ```
*/
notCalledWithMatch(mock: any, ...args: any[]): void

/**
* Assert that the given mock was called only once
* with the determined arguments.
*
* @example
* ```ts
* console.log('hello', 'world', '!')
* assert.calledOnceWith(console.log, 'hello', 'world', '!') // passes
* ```
*/
calledOnceWith(mock: any, ...args: any[]): void

/**
* Assert that the given mock was called the
* determined number of times with always the determined
* arguments.
*
* @example
* ```ts
* console.log('hello', 'world', '!')
* console.log('hello', 'world', '!')
* assert.calledTimesWith(console.log, 2, 'hello', 'world', '!') // passes
* ```
*/
calledTimesWith(mock: any, times: number, ...args: any[]): void

/**
* Assert that the given mock was called before
* an other determined mock.
*
* @example
* ```ts
* console.log('hello', 'world', '!')
* console.error('hello', 'world', '!')
* assert.calledBefore(console.log, console.error) // passes
* ```
*/
calledBefore(mock: any, beforeMock: any): void

/**
* Assert that the given mock was not called before
* an other determined mock.
*
* @example
* ```ts
* console.error('hello', 'world', '!')
* console.log('hello', 'world', '!')
* assert.notCalledBefore(console.log, console.error) // passes
* ```
*/
notCalledBefore(mock: any, beforeMock: any): void

/**
* Assert that the given mock was called after
* an other determined mock.
*
* @example
* ```ts
* console.error('hello', 'world', '!')
* console.log('hello', 'world', '!')
* assert.calledAfter(console.log, console.error) // passes
* ```
*/
calledAfter(mock: any, afterMock: any): void

/**
* Assert that the given mock was not called after
* an other determined mock.
*
* @example
* ```ts
* console.log('hello', 'world', '!')
* console.error('hello', 'world', '!')
* assert.notCalledAfter(console.log, console.error) // passes
* ```
*/
notCalledAfter(mock: any, afterMock: any): void
}
Expand Down
6 changes: 3 additions & 3 deletions src/mocks/Mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Mock {
* and a method of the object.
*/
public static when<T = any>(object: T, method: keyof T): MockBuilder {
return new MockBuilder(object, method, Mock.sandbox)
return new MockBuilder(Mock.sandbox, object, method)
}

/**
Expand Down Expand Up @@ -78,8 +78,8 @@ export class Mock {
* assert.isTrue({ hello: 'world', name: 'João' }, Mock.match({ hello: 'world' }))
* ```
*/
public static match(value: any): Match {
return Mock.sandbox.match(value)
public static get match(): Match {
return Mock.sandbox.match
}

/**
Expand Down
Loading

0 comments on commit 041bec9

Please sign in to comment.