Skip to content

Commit

Permalink
Merge pull request #104 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat(mock): add new match methods
  • Loading branch information
jlenon7 committed Sep 29, 2023
2 parents cf743fc + 38117d9 commit 7026efb
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 13 deletions.
153 changes: 144 additions & 9 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/test",
"version": "4.8.0",
"version": "4.9.0",
"description": "The Athenna test runner. Built on top of Japa.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -58,7 +58,7 @@
"sinon": "^15.1.0"
},
"devDependencies": {
"@athenna/common": "^4.14.0",
"@athenna/common": "^4.15.5",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
"c8": "^8.0.0",
Expand Down
46 changes: 46 additions & 0 deletions src/globals/Assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ Assert.macro(
}
)

Assert.macro('calledWithMatch', function (mock: SinonSpy, ...args: any[]) {
const hasCalledWithMatch = mock.calledWithMatch(...args)

if (!hasCalledWithMatch) {
return this.deepEqual(mock.args[0], args)
}

return this.isTrue(hasCalledWithMatch)
})

Assert.macro('calledBefore', function (mock: SinonSpy, beforeMock: SinonSpy) {
return this.isTrue(mock.calledBefore(beforeMock))
})
Expand All @@ -66,6 +76,16 @@ Assert.macro('notCalledWith', function (mock: SinonSpy, ...args: any[]) {
return this.isFalse(hasCalledWith)
})

Assert.macro('notCalledWithMatch', function (mock: SinonSpy, ...args: any[]) {
const hasCalledWithMatch = mock.calledWithMatch(...args)

if (hasCalledWithMatch) {
return this.notDeepEqual(mock.args[0], args)
}

return this.isFalse(hasCalledWithMatch)
})

Assert.macro('calledOnceWith', function (mock: SinonSpy, ...args: any[]) {
return this.isTrue(mock.calledOnceWith(...args))
})
Expand Down Expand Up @@ -140,6 +160,32 @@ declare module '@japa/assert' {
* determined arguments.
*/
notCalledWith(mock: SinonSpy, ...args: any[]): void
/**
* Assert that the given mock was called with the
* arguments matching some of the given arguments.
* This is the same of doing:
* `assert.calledWith(mock, Mock.match(arg1), Mock.match(arg2))`
*
* @example
* ```ts
* console.log('hello', 'world', '!')
* assert.calledWithMatch(console.log, 'hello', 'world') // passes
* ```
*/
calledWithMatch(mock: SinonSpy, ...args: any[]): void
/**
* Assert that the given mock was not called with the
* arguments matching some of the given arguments.
* This is the same of doing:
* `assert.notCalledWith(mock, Mock.match(arg1), Mock.match(arg2))`
*
* @example
* ```ts
* console.log('hello', 'world', '!')
* assert.notCalledWithMatch(console.log, 'hello', 'world') // fails
* ```
*/
notCalledWithMatch(mock: SinonSpy, ...args: any[]): void
/**
* Assert that the given mock was called only once
* with the determined arguments.
Expand Down
17 changes: 15 additions & 2 deletions src/mocks/Mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
* file that was distributed with this source code.
*/

import { createSandbox } from 'sinon'
import { MockBuilder } from '#src/mocks/MockBuilder'
import type {
Spy,
Match,
SpyMethod,
StubMethod,
SpyInstance,
StubInstance
} from '#src'
import { createSandbox } from 'sinon'
import { MockBuilder } from '#src/mocks/MockBuilder'

export class Mock {
/**
Expand Down Expand Up @@ -69,6 +70,18 @@ export class Mock {
return Mock.sandbox.fake()
}

/**
* Create a matcher for the given value.
*
* @example
* ```ts
* assert.isTrue({ hello: 'world', name: 'João' }, Mock.match({ hello: 'world' }))
* ```
*/
public static match(value: any): Match {
return Mock.sandbox.match(value)
}

/**
* Restore all mocks to default.
*/
Expand Down
12 changes: 12 additions & 0 deletions src/types/Match.ts
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 { SinonMatcher } from 'sinon'

export type Match = SinonMatcher
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
export type { Config, PluginFn } from '@japa/runner'

export * from './Spy.js'
export * from './Match.js'
export * from './SpyMethod.js'
export * from './SpyInstance.js'
export * from './Stub.js'
Expand Down
Loading

0 comments on commit 7026efb

Please sign in to comment.