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-globals): support async SpyStrategy methods #578

Merged
merged 3 commits into from
May 14, 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
15 changes: 15 additions & 0 deletions src/transformers/jasmine-globals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ test('spyOn', () => {
jest.spyOn();
spyOn(stuff);
jest.spyOn().mockImplementation();
jest.spyOn(stuff).and.resolveTo('lmao');
jest.spyOn(stuff).and.rejectWith('oh no');
const fetchSpy = spyOn(window, 'fetch').and.resolveTo({json: {}});
`,
`
jest.spyOn().mockReturnValue();
Expand All @@ -36,6 +39,9 @@ test('spyOn', () => {
jest.spyOn();
jest.spyOn(stuff).mockImplementation(() => {});
jest.spyOn().mockImplementation();
jest.spyOn(stuff).mockResolvedValue('lmao');
jest.spyOn(stuff).mockRejectedValue('oh no');
const fetchSpy = jest.spyOn(window, 'fetch').mockResolvedValue({json: {}});
`
)
})
Expand All @@ -49,6 +55,9 @@ test('jasmine.createSpy', () => {
jasmine.createSpy().and.callFake(arg => arg);
jasmine.createSpy().and.returnValue('lmao');
const spy2 = jasmine.createSpy().and.returnValue('lmao');
jasmine.createSpy().and.resolveTo('lmao');
jasmine.createSpy().and.rejectWith('oh no');
const spy3 = jasmine.createSpy().and.resolveTo('lmao');
`,
`
jest.fn();
Expand All @@ -57,8 +66,14 @@ test('jasmine.createSpy', () => {
jest.fn(arg => arg);
jest.fn(() => 'lmao');
const spy2 = jest.fn(() => 'lmao');
jest.fn().mockResolvedValue('lmao');
jest.fn().mockRejectedValue('oh no');
const spy3 = jest.fn().mockResolvedValue('lmao');
`
)

// Ensure we haven't missed any console warnings
expect(consoleWarnings).toEqual([])
})

test('not supported jasmine.createSpy().and.*', () => {
Expand Down
24 changes: 24 additions & 0 deletions src/transformers/jasmine-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ export default function jasmineGlobals(fileInfo, api, options) {
path.node.arguments = [j.arrowFunctionExpression([], path.node.arguments[0])]
break
}
// This is transformed by the *.and.*() expression handling below
case 'resolveTo': {
break
}
// This is transformed by the *.and.*() expression handling below
case 'rejectWith': {
break
}
default: {
logWarning(
`Unsupported Jasmine functionality "jasmine.createSpy().and.${spyType}".`,
Expand Down Expand Up @@ -137,6 +145,8 @@ export default function jasmineGlobals(fileInfo, api, options) {
// - `existingSpy.and.callFake(..)`
// - `spyOn().and.returnValue(..)`
// - `existingSpy.and.returnValue(..)`
// - `spyOn().and.resolveTo(..)`
// - `existingSpy.and.rejectWith(..)`
callee: {
type: 'MemberExpression',
object: {
Expand Down Expand Up @@ -171,6 +181,20 @@ export default function jasmineGlobals(fileInfo, api, options) {
path.node.callee.property.name = 'mockReturnValue'
break
}
// `*.and.resolveTo()` is equivalent of jest
// `*.mockResolvedValue()`
case 'resolveTo': {
path.node.callee.object = path.node.callee.object.object
path.node.callee.property.name = 'mockResolvedValue'
break
}
// `*.and.rejectWith()` is equivalent of jest
// `*.mockRejectedValue()`
case 'rejectWith': {
path.node.callee.object = path.node.callee.object.object
path.node.callee.property.name = 'mockRejectedValue'
break
}
}
})

Expand Down
Loading