Skip to content

Commit 5199f2e

Browse files
authored
feat(jasmine-globals): support async SpyStrategy methods (#578)
* async spies * avoid warnings * couple more test cases
1 parent 907545d commit 5199f2e

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/transformers/jasmine-globals.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ test('spyOn', () => {
2525
jest.spyOn();
2626
spyOn(stuff);
2727
jest.spyOn().mockImplementation();
28+
jest.spyOn(stuff).and.resolveTo('lmao');
29+
jest.spyOn(stuff).and.rejectWith('oh no');
30+
const fetchSpy = spyOn(window, 'fetch').and.resolveTo({json: {}});
2831
`,
2932
`
3033
jest.spyOn().mockReturnValue();
@@ -36,6 +39,9 @@ test('spyOn', () => {
3639
jest.spyOn();
3740
jest.spyOn(stuff).mockImplementation(() => {});
3841
jest.spyOn().mockImplementation();
42+
jest.spyOn(stuff).mockResolvedValue('lmao');
43+
jest.spyOn(stuff).mockRejectedValue('oh no');
44+
const fetchSpy = jest.spyOn(window, 'fetch').mockResolvedValue({json: {}});
3945
`
4046
)
4147
})
@@ -49,6 +55,9 @@ test('jasmine.createSpy', () => {
4955
jasmine.createSpy().and.callFake(arg => arg);
5056
jasmine.createSpy().and.returnValue('lmao');
5157
const spy2 = jasmine.createSpy().and.returnValue('lmao');
58+
jasmine.createSpy().and.resolveTo('lmao');
59+
jasmine.createSpy().and.rejectWith('oh no');
60+
const spy3 = jasmine.createSpy().and.resolveTo('lmao');
5261
`,
5362
`
5463
jest.fn();
@@ -57,8 +66,14 @@ test('jasmine.createSpy', () => {
5766
jest.fn(arg => arg);
5867
jest.fn(() => 'lmao');
5968
const spy2 = jest.fn(() => 'lmao');
69+
jest.fn().mockResolvedValue('lmao');
70+
jest.fn().mockRejectedValue('oh no');
71+
const spy3 = jest.fn().mockResolvedValue('lmao');
6072
`
6173
)
74+
75+
// Ensure we haven't missed any console warnings
76+
expect(consoleWarnings).toEqual([])
6277
})
6378

6479
test('not supported jasmine.createSpy().and.*', () => {

src/transformers/jasmine-globals.ts

+24
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ export default function jasmineGlobals(fileInfo, api, options) {
5353
path.node.arguments = [j.arrowFunctionExpression([], path.node.arguments[0])]
5454
break
5555
}
56+
// This is transformed by the *.and.*() expression handling below
57+
case 'resolveTo': {
58+
break
59+
}
60+
// This is transformed by the *.and.*() expression handling below
61+
case 'rejectWith': {
62+
break
63+
}
5664
default: {
5765
logWarning(
5866
`Unsupported Jasmine functionality "jasmine.createSpy().and.${spyType}".`,
@@ -137,6 +145,8 @@ export default function jasmineGlobals(fileInfo, api, options) {
137145
// - `existingSpy.and.callFake(..)`
138146
// - `spyOn().and.returnValue(..)`
139147
// - `existingSpy.and.returnValue(..)`
148+
// - `spyOn().and.resolveTo(..)`
149+
// - `existingSpy.and.rejectWith(..)`
140150
callee: {
141151
type: 'MemberExpression',
142152
object: {
@@ -171,6 +181,20 @@ export default function jasmineGlobals(fileInfo, api, options) {
171181
path.node.callee.property.name = 'mockReturnValue'
172182
break
173183
}
184+
// `*.and.resolveTo()` is equivalent of jest
185+
// `*.mockResolvedValue()`
186+
case 'resolveTo': {
187+
path.node.callee.object = path.node.callee.object.object
188+
path.node.callee.property.name = 'mockResolvedValue'
189+
break
190+
}
191+
// `*.and.rejectWith()` is equivalent of jest
192+
// `*.mockRejectedValue()`
193+
case 'rejectWith': {
194+
path.node.callee.object = path.node.callee.object.object
195+
path.node.callee.property.name = 'mockRejectedValue'
196+
break
197+
}
174198
}
175199
})
176200

0 commit comments

Comments
 (0)