|
| 1 | +describe('Jest v29 Deprecated Matchers Demo', () => { |
| 2 | + describe('Mock function matchers that will break in Jest v30', () => { |
| 3 | + it('uses toBeCalled instead of toHaveBeenCalled', () => { |
| 4 | + const mockFn = jest.fn(); |
| 5 | + mockFn('test'); |
| 6 | + |
| 7 | + expect(mockFn).toBeCalled(); |
| 8 | + }); |
| 9 | + |
| 10 | + it('uses toBeCalledTimes instead of toHaveBeenCalledTimes', () => { |
| 11 | + const mockFn = jest.fn(); |
| 12 | + mockFn('first'); |
| 13 | + mockFn('second'); |
| 14 | + |
| 15 | + expect(mockFn).toBeCalledTimes(2); |
| 16 | + }); |
| 17 | + |
| 18 | + it('uses toBeCalledWith instead of toHaveBeenCalledWith', () => { |
| 19 | + const mockFn = jest.fn(); |
| 20 | + mockFn('test-arg'); |
| 21 | + |
| 22 | + expect(mockFn).toBeCalledWith('test-arg'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('uses lastCalledWith instead of toHaveBeenLastCalledWith', () => { |
| 26 | + const mockFn = jest.fn(); |
| 27 | + mockFn('first'); |
| 28 | + mockFn('last'); |
| 29 | + |
| 30 | + expect(mockFn).lastCalledWith('last'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('uses nthCalledWith instead of toHaveBeenNthCalledWith', () => { |
| 34 | + const mockFn = jest.fn(); |
| 35 | + mockFn('first'); |
| 36 | + mockFn('second'); |
| 37 | + |
| 38 | + expect(mockFn).nthCalledWith(1, 'first'); |
| 39 | + expect(mockFn).nthCalledWith(2, 'second'); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('Return value matchers that will break in Jest v30', () => { |
| 44 | + it('uses toReturn instead of toHaveReturned', () => { |
| 45 | + const mockFn = jest.fn().mockReturnValue('result'); |
| 46 | + mockFn(); |
| 47 | + |
| 48 | + expect(mockFn).toReturn(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('uses toReturnTimes instead of toHaveReturnedTimes', () => { |
| 52 | + const mockFn = jest.fn().mockReturnValue('result'); |
| 53 | + mockFn(); |
| 54 | + mockFn(); |
| 55 | + |
| 56 | + expect(mockFn).toReturnTimes(2); |
| 57 | + }); |
| 58 | + |
| 59 | + it('uses toReturnWith instead of toHaveReturnedWith', () => { |
| 60 | + const mockFn = jest.fn().mockReturnValue('specific-result'); |
| 61 | + mockFn(); |
| 62 | + |
| 63 | + expect(mockFn).toReturnWith('specific-result'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('uses lastReturnedWith instead of toHaveLastReturnedWith', () => { |
| 67 | + const mockFn = jest.fn(); |
| 68 | + mockFn.mockReturnValueOnce('first'); |
| 69 | + mockFn.mockReturnValueOnce('last'); |
| 70 | + mockFn(); |
| 71 | + mockFn(); |
| 72 | + |
| 73 | + expect(mockFn).lastReturnedWith('last'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('uses nthReturnedWith instead of toHaveNthReturnedWith', () => { |
| 77 | + const mockFn = jest.fn(); |
| 78 | + mockFn.mockReturnValueOnce('first'); |
| 79 | + mockFn.mockReturnValueOnce('second'); |
| 80 | + mockFn(); |
| 81 | + mockFn(); |
| 82 | + |
| 83 | + expect(mockFn).nthReturnedWith(1, 'first'); |
| 84 | + expect(mockFn).nthReturnedWith(2, 'second'); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('Error matchers that will break in Jest v30', () => { |
| 89 | + it('uses toThrowError instead of toThrow', () => { |
| 90 | + const errorFn = () => { |
| 91 | + throw new Error('Test error'); |
| 92 | + }; |
| 93 | + |
| 94 | + expect(errorFn).toThrowError('Test error'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('uses toThrowError with no message instead of toThrow', () => { |
| 98 | + const errorFn = () => { |
| 99 | + throw new Error('Any error'); |
| 100 | + }; |
| 101 | + |
| 102 | + expect(errorFn).toThrowError(); |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments