Skip to content

Commit

Permalink
chai-should: don't update member expressions inside expect calls (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristerkari authored Mar 22, 2023
1 parent 5c01c62 commit 1ae676f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/transformers/chai-should.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,13 @@ test('converts "called"', () => {
expectTransformation(
`
expect(sinonSpy).to.be.called;
expect(sinonSpy).to.be.called();
expect(sinonSpy).not.to.be.called;
expect(sinonSpy).to.not.be.called;
expect(sinonSpy).to.be.not.called;
`,
`
expect(sinonSpy).toHaveBeenCalled();
expect(sinonSpy).toHaveBeenCalled();
expect(sinonSpy).not.toHaveBeenCalled();
expect(sinonSpy).not.toHaveBeenCalled();
Expand All @@ -315,6 +317,19 @@ test('converts "called"', () => {
)
})

test('does not convert "fetchMock.called()"', () => {
expectTransformation(
`
expect(fetchMock.called("/url")).to.equal(true);
expect(fetchMock.called()).to.equal(true);
`,
`
expect(fetchMock.called("/url")).toBe(true);
expect(fetchMock.called()).toBe(true);
`
)
})

test('converts "called.exactly(n)"', () => {
expectTransformation(
`
Expand Down
5 changes: 5 additions & 0 deletions src/transformers/chai-should.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ export default function transformer(fileInfo, api, options) {

const isExpectCall = (node) => isExpectCallUtil(j, node)

const isInsideExpectCall = (path) =>
findParentOfType(path.parentPath.parentPath, 'CallExpression')?.value.callee.name ===
'expect'

const typeOf = (path, value, args, containsNot) => {
switch (args[0].value) {
case 'null':
Expand Down Expand Up @@ -398,6 +402,7 @@ export default function transformer(fileInfo, api, options) {
},
})
.filter((p) => findParentOfType(p, 'ExpressionStatement'))
.filter((p) => !isInsideExpectCall(p))
.filter((p) => {
const { value } = p
const propertyName = value.property.name.toLowerCase()
Expand Down

0 comments on commit 1ae676f

Please sign in to comment.