Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jasmine): add matcher transforms that have no direct equivalent in jest, but can be expressed in other ways #614

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/transformers/expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,14 @@ test('maps expect matchers', () => {
expect(stuff).toBeAn(Array);
expect(new Stuff).toBeAn(Stuff, 'Message');

expect([1, 2]).toHaveSize(2);

expect(stuff).toNotBeA(Number);
expect(stuff).toNotBeAn(Array);

expect(foo).toBePositiveInfinity();
expect(foo).toBeNegativeInfinity();

expect(stuff).toMatch({foo: 'bar'});
expect(stuff).toMatch({foo: 'bar'}, 'message');
expect(stuff).toMatch('a string');
Expand All @@ -113,6 +118,10 @@ test('maps expect matchers', () => {
expect(stuff).toNotHaveBeenCalled();
expect(stuff).toNotHaveBeenCalled('msg');
expect(stuff).toHaveBeenCalledWith('foo', 'bar');

expect(mySpy).toHaveBeenCalledOnceWith('foo', 'bar');
expect(mySpy).toHaveBeenCalledBefore(myOtherSpy);
expect(mySpyObj).toHaveSpyInteractions()
});
`,
`
Expand Down Expand Up @@ -152,9 +161,14 @@ test('maps expect matchers', () => {
expect(stuff).toBeInstanceOf(Array);
expect(new Stuff).toBeInstanceOf(Stuff);

expect([1, 2]).toHaveLength(2);

expect(stuff).not.toBeInstanceOf(Number);
expect(stuff).not.toBeInstanceOf(Array);

expect(foo).toBe(Infinity);
expect(foo).toBe(-Infinity);

expect(stuff).toMatchObject({foo: 'bar'});
expect(stuff).toMatchObject({foo: 'bar'});
expect(stuff).toMatch('a string');
Expand All @@ -169,6 +183,10 @@ test('maps expect matchers', () => {
expect(stuff).not.toHaveBeenCalled();
expect(stuff).not.toHaveBeenCalled();
expect(stuff).toHaveBeenCalledWith('foo', 'bar');

expect(mySpy.mock.calls).toEqual([['foo', 'bar']]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth double checking this one. I believe it should be mySpy.mock.calls[0], but we miss checking that it is only called once with those arguments.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the nested array implicitly asserts that mySpy.mock.calls.length === 1

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize the array was nested. All good then. 👍🏼

expect(Math.min(...mySpy.mock.invocationOrder)).toBeLessThan(Math.min(...myOtherSpy.mock.invocationOrder));
expect(Object.values(mySpyObj).some(spy => spy.mock?.calls?.length)).toBe(true)
});
`
)
Expand Down
77 changes: 77 additions & 0 deletions src/transformers/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,83 @@ ${keys}.forEach(e => {
matcher.name = 'toBe'
break
}

case 'toBePositiveInfinity': {
matcherArgs[0] = j.literal(Infinity)
matcher.name = 'toBe'
break
}

case 'toBeNegativeInfinity': {
matcherArgs[0] = j.literal(-Infinity)
matcher.name = 'toBe'
break
}

case 'toHaveSize': {
matcher.name = 'toHaveLength'
break
}

case 'toHaveBeenCalledOnceWith': {
expectArgs[0] = j.memberExpression(
j.memberExpression(expectArgs[0], j.identifier('mock')),
j.identifier('calls')
)
matcher.name = 'toEqual'

matcherNode.arguments = [j.arrayExpression([j.arrayExpression(matcherArgs)])]
break
}

case 'toHaveBeenCalledBefore': {
const getMinInvocationOrder = (spy) =>
j.callExpression(
j.memberExpression(j.identifier('Math'), j.identifier('min')),
[
j.spreadElement(
j.memberExpression(
j.memberExpression(spy, j.identifier('mock')),
j.identifier('invocationOrder')
)
),
]
)

expectArgs[0] = getMinInvocationOrder(expectArgs[0])
matcherArgs[0] = getMinInvocationOrder(matcherArgs[0])
matcher.name = 'toBeLessThan'
break
}

case 'toHaveSpyInteractions': {
expectArgs[0] = j.callExpression(
j.memberExpression(
j.callExpression(
j.memberExpression(j.identifier('Object'), j.identifier('values')),
[expectArgs[0]]
),
j.identifier('some')
),
[
j.arrowFunctionExpression(
[j.identifier('spy')],
j.optionalMemberExpression(
j.optionalMemberExpression(
j.memberExpression(j.identifier('spy'), j.identifier('mock')),
j.identifier('calls'),
false,
true
),
j.identifier('length')
)
),
]
)
matcherArgs[0] = j.literal(true)
matcher.name = 'toBe'
break
}
}

balanceMatcherNodeArguments(matcherNode, matcher, path)
Expand Down
Loading