Skip to content

Commit

Permalink
Merge pull request #110 from AthennaIO/develop
Browse files Browse the repository at this point in the history
Add way to mock properties and restore single mock
  • Loading branch information
jlenon7 committed Oct 18, 2023
2 parents e6fc6ed + d2c078b commit 059d4b7
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 8 deletions.
12 changes: 6 additions & 6 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.12.0",
"version": "4.13.0",
"description": "The Athenna test runner. Built on top of Japa.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -60,7 +60,7 @@
"@japa/run-failed-tests": "^1.1.0",
"@japa/runner": "^2.2.2",
"@japa/spec-reporter": "^1.3.3",
"@types/sinon": "^10.0.16",
"@types/sinon": "^10.0.17",
"c8": "^8.0.1",
"sinon": "^15.1.0"
},
Expand Down
21 changes: 21 additions & 0 deletions src/mocks/Mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ export class Mock {
return Mock.sandbox.match(value)
}

/**
* Restore a mock to default behavior.
*
* @example
* ```ts
* Mock.when(console, 'log').return(undefined)
*
* Mock.restore(console.log)
* ```
*/
public static restore(value: any): void {
if (!value.restore) {
return
}

value.restore()
value.resetHistory()
value.resetBehavior()
value.reset()
}

/**
* Restore all mocks to default.
*/
Expand Down
20 changes: 20 additions & 0 deletions src/mocks/MockBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@ export class MockBuilder {
private sandbox: SinonSandbox
) {}

/**
* Mock a property changing it value.
*
* @example
* ```ts
* import { Mock } from '@athenna/test'
*
* const mock = Mock.when(console, 'log').value(() => {})
*
* mock.called // false
* ```
*/
public value<T = any>(value: T): Stub {
const stub = this.sandbox.stub(this.object, this.method)

stub.value(value)

return stub
}

/**
* Mock the method to return the given value.
*
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/mocks/MockTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ export default class MockTest {
assert.calledWith(spy, 1)
}

@Test()
public async shouldBeAbleToMockObjectPropertyWithDifferentValues({ assert }: Context) {
const userService = new UserService()

Mock.when(userService, 'findById').value(() => ({ id: 2 }))

const value = userService.findById(1)

assert.deepEqual(value, { id: 2 })
}

@Test()
public async shouldBeAbleToMockObjectMethodsToReturnValue({ assert }: Context) {
const userService = new UserService()
Expand Down Expand Up @@ -121,4 +132,42 @@ export default class MockTest {
await assert.doesNotRejects(() => userService.find())
await assert.doesNotRejects(() => userService.findById(1))
}

@Test()
public async shouldBeAbleToRestoreASingleMockedMethod({ assert }: Context) {
const userService = new UserService()

Mock.when(userService, 'find').reject(new Error('ERROR_MOCK'))
Mock.when(userService, 'findById').reject(new Error('ERROR_MOCK'))

await assert.rejects(() => userService.find(), Error)
await assert.rejects(() => userService.findById(1), Error)

Mock.restore(userService.find)
Mock.restore(userService.findById)

await assert.doesNotRejects(() => userService.find())
await assert.doesNotRejects(() => userService.findById(1))
}

@Test()
public async shouldBeAbleToRestoreASingleMockedProperty({ assert }: Context) {
const userService = new UserService()

const mockFind = Mock.when(userService, 'find').value(() => {
throw new Error('ERROR_MOCK')
})
const mockFindById = Mock.when(userService, 'findById').value(() => {
throw new Error('ERROR_MOCK')
})

await assert.rejects(() => userService.find(), Error)
await assert.rejects(() => userService.findById(1), Error)

Mock.restore(mockFind)
Mock.restore(mockFindById)

await assert.doesNotRejects(() => userService.find())
await assert.doesNotRejects(() => userService.findById(1))
}
}

0 comments on commit 059d4b7

Please sign in to comment.