From f0a8b374a6bb79ac1bf38c7c0a1d99d3f21ebb9b Mon Sep 17 00:00:00 2001 From: Charley Date: Tue, 14 May 2024 09:49:57 -0500 Subject: [PATCH 1/3] async spies --- src/transformers/jasmine-globals.test.ts | 8 ++++++++ src/transformers/jasmine-globals.ts | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts index 1d74c48d..67899a98 100644 --- a/src/transformers/jasmine-globals.test.ts +++ b/src/transformers/jasmine-globals.test.ts @@ -25,6 +25,8 @@ test('spyOn', () => { jest.spyOn(); spyOn(stuff); jest.spyOn().mockImplementation(); + jest.spyOn(stuff).and.resolveTo('lmao'); + jest.spyOn(stuff).and.rejectWith('oh no'); `, ` jest.spyOn().mockReturnValue(); @@ -36,6 +38,8 @@ test('spyOn', () => { jest.spyOn(); jest.spyOn(stuff).mockImplementation(() => {}); jest.spyOn().mockImplementation(); + jest.spyOn(stuff).mockResolvedValue('lmao'); + jest.spyOn(stuff).mockRejectedValue('oh no'); ` ) }) @@ -49,6 +53,8 @@ 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'); `, ` jest.fn(); @@ -57,6 +63,8 @@ test('jasmine.createSpy', () => { jest.fn(arg => arg); jest.fn(() => 'lmao'); const spy2 = jest.fn(() => 'lmao'); + jest.fn().mockResolvedValue('lmao'); + jest.fn().mockRejectedValue('oh no'); ` ) }) diff --git a/src/transformers/jasmine-globals.ts b/src/transformers/jasmine-globals.ts index 06f97259..e255eb5d 100644 --- a/src/transformers/jasmine-globals.ts +++ b/src/transformers/jasmine-globals.ts @@ -137,6 +137,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: { @@ -171,6 +173,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 + } } }) From 4ea3367a567fe3d0a8150eb73dff3c073401051c Mon Sep 17 00:00:00 2001 From: Charley Date: Tue, 14 May 2024 10:12:22 -0500 Subject: [PATCH 2/3] avoid warnings --- src/transformers/jasmine-globals.test.ts | 3 +++ src/transformers/jasmine-globals.ts | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts index 67899a98..eaeae7d2 100644 --- a/src/transformers/jasmine-globals.test.ts +++ b/src/transformers/jasmine-globals.test.ts @@ -67,6 +67,9 @@ test('jasmine.createSpy', () => { jest.fn().mockRejectedValue('oh no'); ` ) + + // Ensure we haven't missed any console warnings + expect(consoleWarnings).toEqual([]) }) test('not supported jasmine.createSpy().and.*', () => { diff --git a/src/transformers/jasmine-globals.ts b/src/transformers/jasmine-globals.ts index e255eb5d..69471932 100644 --- a/src/transformers/jasmine-globals.ts +++ b/src/transformers/jasmine-globals.ts @@ -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}".`, From 544bb6486d302b287b15e44ee5ff989e2b5d4f3e Mon Sep 17 00:00:00 2001 From: Charley Date: Tue, 14 May 2024 10:23:58 -0500 Subject: [PATCH 3/3] couple more test cases --- src/transformers/jasmine-globals.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/transformers/jasmine-globals.test.ts b/src/transformers/jasmine-globals.test.ts index eaeae7d2..d2da1824 100644 --- a/src/transformers/jasmine-globals.test.ts +++ b/src/transformers/jasmine-globals.test.ts @@ -27,6 +27,7 @@ test('spyOn', () => { 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(); @@ -40,6 +41,7 @@ test('spyOn', () => { jest.spyOn().mockImplementation(); jest.spyOn(stuff).mockResolvedValue('lmao'); jest.spyOn(stuff).mockRejectedValue('oh no'); + const fetchSpy = jest.spyOn(window, 'fetch').mockResolvedValue({json: {}}); ` ) }) @@ -55,6 +57,7 @@ test('jasmine.createSpy', () => { 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(); @@ -65,6 +68,7 @@ test('jasmine.createSpy', () => { const spy2 = jest.fn(() => 'lmao'); jest.fn().mockResolvedValue('lmao'); jest.fn().mockRejectedValue('oh no'); + const spy3 = jest.fn().mockResolvedValue('lmao'); ` )