Skip to content

Commit

Permalink
fix(jasmine): negated jasmine matchers should be handled (#621)
Browse files Browse the repository at this point in the history
  • Loading branch information
jase88 authored Sep 17, 2024
1 parent d885321 commit 34e7f02
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
60 changes: 60 additions & 0 deletions src/transformers/jasmine-globals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,110 @@ describe('jasmine matchers', () => {
test('toBeTrue', () =>
expectTransformation('expect(stuff).toBeTrue()', 'expect(stuff).toBe(true)'))

test('not.toBeTrue', () =>
expectTransformation('expect(stuff).not.toBeTrue()', 'expect(stuff).not.toBe(true)'))

test('toBeFalse', () =>
expectTransformation('expect(stuff).toBeFalse()', 'expect(stuff).toBe(false)'))

test('not.toBeFalse', () =>
expectTransformation(
'expect(stuff).not.toBeFalse()',
'expect(stuff).not.toBe(false)'
))

test('toBePositiveInfinity', () =>
expectTransformation(
'expect(foo).toBePositiveInfinity()',
'expect(foo).toBe(Infinity)'
))

test('not.toBePositiveInfinity', () =>
expectTransformation(
'expect(foo).not.toBePositiveInfinity()',
'expect(foo).not.toBe(Infinity)'
))

test('toBeNegativeInfinity', () =>
expectTransformation(
'expect(foo).toBeNegativeInfinity()',
'expect(foo).toBe(-Infinity)'
))

test('not.toBeNegativeInfinity', () =>
expectTransformation(
'expect(foo).not.toBeNegativeInfinity()',
'expect(foo).not.toBe(-Infinity)'
))

test('toHaveSize', () =>
expectTransformation(
'expect([1, 2]).toHaveSize(2)',
'expect([1, 2]).toHaveLength(2)'
))

test('not.toHaveSize', () =>
expectTransformation(
'expect([1]).not.toHaveSize(2)',
'expect([1]).not.toHaveLength(2)'
))

test('toHaveClass', () =>
expectTransformation(
`expect(element).toHaveClass('active')`,
`expect(element.classList.contains('active')).toBe(true)`
))

test('not.toHaveClass', () =>
expectTransformation(
`expect(element).not.toHaveClass('danger')`,
`expect(element.classList.contains('danger')).not.toBe(true)`
))

test('toHaveBeenCalledOnceWith', () =>
expectTransformation(
`expect(mySpy).toHaveBeenCalledOnceWith('foo', 'bar')`,
`expect(mySpy.mock.calls).toEqual([['foo', 'bar']])`
))

test('not.toHaveBeenCalledOnceWith', () =>
expectTransformation(
`expect(mySpy).not.toHaveBeenCalledOnceWith('foo', 'bar')`,
`expect(mySpy.mock.calls).not.toEqual([['foo', 'bar']])`
))

test('toHaveBeenCalledBefore', () =>
expectTransformation(
'expect(mySpy).toHaveBeenCalledBefore(myOtherSpy)',
'expect(Math.min(...mySpy.mock.invocationOrder)).toBeLessThan(Math.min(...myOtherSpy.mock.invocationOrder))'
))

test('not.toHaveBeenCalledBefore', () =>
expectTransformation(
'expect(mySpy).not.toHaveBeenCalledBefore(myOtherSpy)',
'expect(Math.min(...mySpy.mock.invocationOrder)).not.toBeLessThan(Math.min(...myOtherSpy.mock.invocationOrder))'
))

test('toHaveSpyInteractions', () =>
expectTransformation(
'expect(mySpyObj).toHaveSpyInteractions()',
'expect(Object.values(mySpyObj).some(spy => spy.mock?.calls?.length)).toBe(true)'
))

test('not.toHaveSpyInteractions', () =>
expectTransformation(
'expect(mySpyObj).not.toHaveSpyInteractions()',
'expect(Object.values(mySpyObj).some(spy => spy.mock?.calls?.length)).not.toBe(true)'
))

test.each([
['toHaveBeenCalled', 'expect(spy).not.toHaveBeenCalled()'],
['toBeNull', 'expect(result).not.toBeNull()'],
['toBe', `expect(foo).not.toBe('')`],
['toEqual', 'expect(foo).not.toEqual(expectedState)'],
])('not.%s', (_name, defaultMatcherStatement) => {
expectTransformation(defaultMatcherStatement, defaultMatcherStatement)
})
})

test('spyOn', () => {
Expand Down
18 changes: 12 additions & 6 deletions src/transformers/jasmine-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@ export default function jasmineGlobals(fileInfo, api, options) {
property: { type: 'Identifier' },
})
.forEach((path) => {
const matcher = path.node.property
const matcherName = matcher.name
const matcherNode = path.parentPath.node
const matcherArgs = matcherNode.arguments
const expectArgs = path.node.object.arguments
const isNegatedMatcher = path.node.property.name === 'not'
const matcher = isNegatedMatcher ? path.parent.node.property : path.node.property

switch (matcherName) {
const expectArgs = isNegatedMatcher
? path.parent.node.object.object.arguments
: path.node.object.arguments

const matcherNode = isNegatedMatcher
? path.parent.parentPath.node
: path.parentPath.node
const matcherArgs = matcherNode.arguments ?? []

switch (matcher.name) {
case 'toBeTrue': {
matcherArgs[0] = j.literal(true)
matcher.name = 'toBe'
Expand Down

0 comments on commit 34e7f02

Please sign in to comment.