Skip to content

Commit

Permalink
sinon: add missing global resets
Browse files Browse the repository at this point in the history
  • Loading branch information
skirsdeda committed Nov 5, 2024
1 parent cdb82ed commit 76c2ab3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/transformers/sinon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,9 @@ describe.each([
Api.get.restore()
Api.get.reset()
sinon.restore()
sinon.reset()
sinon.resetBehavior()
sinon.resetHistory()
stub.resetBehavior()
stub.resetHistory()
`,
Expand All @@ -721,6 +724,9 @@ describe.each([
Api.get.mockRestore()
Api.get.mockReset()
jest.restoreAllMocks()
jest.resetAllMocks()
jest.resetAllMocks()
jest.resetAllMocks()
stub.mockReset()
stub.mockReset()
`
Expand Down
24 changes: 17 additions & 7 deletions src/transformers/sinon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,24 @@ const CHAI_CHAIN_MATCHERS = new Set(
'toBeFalsy',
].map((a) => a.toLowerCase())
)
const SINON_CALLED_WITH_METHODS = ['calledWith', 'notCalledWith', 'neverCalledWith']
const SINON_SPY_METHODS = ['spy', 'stub']
const SINON_CALLED_WITH_METHODS = [
'calledWith',
'notCalledWith',
'neverCalledWith',
] as const
const SINON_SPY_METHODS = ['spy', 'stub'] as const
const SINON_MOCK_RESETS = {
reset: 'mockReset',
resetBehavior: 'mockReset',
resetHistory: 'mockReset',
restore: 'mockRestore',
}
} as const
const SINON_GLOBAL_MOCK_RESETS = {
reset: 'resetAllMocks',
resetBehavior: 'resetAllMocks',
resetHistory: 'resetAllMocks',
restore: 'restoreAllMocks',
} as const
const _sinonMockImpls = [
'returns',
'returnsArg',
Expand Down Expand Up @@ -701,13 +711,13 @@ function transformMockResets(j, ast) {
},
property: {
type: 'Identifier',
name: 'restore',
name: (name) => Object.hasOwn(SINON_GLOBAL_MOCK_RESETS, name),
},
},
})
.forEach((np) => {
np.node.callee.object.name = 'jest'
np.node.callee.property.name = 'restoreAllMocks'
.forEach(({ node }) => {
node.callee.object.name = 'jest'
node.callee.property.name = SINON_GLOBAL_MOCK_RESETS[node.callee.property.name]
})

ast
Expand Down

0 comments on commit 76c2ab3

Please sign in to comment.