-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate pactjs-cli from jest to vitest + msw
- Loading branch information
Showing
9 changed files
with
454 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 18 additions & 22 deletions
40
packages/tools/pactjs-cli/src/utils/tests/callLocal.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,31 @@ | ||
jest.mock('cross-fetch'); | ||
import { callLocal } from '../callLocal'; | ||
|
||
import fetch, { Response } from 'cross-fetch'; | ||
import { rest } from 'msw'; | ||
import { setupServer } from 'msw/node'; | ||
|
||
const mockedFetch = fetch as jest.MockedFunction<typeof fetch>; | ||
const restHandlers = [ | ||
rest.post('https://json-api.chainweb.com/api/v1/local', (req, res, ctx) => { | ||
return res(ctx.status(200), ctx.json({ json: 'some json' })); | ||
}), | ||
rest.post('https://text-api.chainweb.com/api/v1/local', (req, res, ctx) => { | ||
return res(ctx.status(200), ctx.text('some text')); | ||
}), | ||
]; | ||
|
||
describe('callLocal', () => { | ||
it('returns the correct jsonResponse on success', async () => { | ||
const response = new Response(); | ||
|
||
response.json = jest.fn().mockReturnValue({ json: 'some json' }); | ||
response.text = jest.fn().mockReturnValue('some text'); | ||
response.clone = jest.fn().mockImplementation(() => response); | ||
const server = setupServer(...restHandlers); | ||
|
||
mockedFetch.mockResolvedValue(response); | ||
|
||
const result = await callLocal('https://api.chainweb.com', 'body'); | ||
beforeAll(() => server.listen({ onUnhandledRequest: 'error' })); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
|
||
describe('callLocal', () => { | ||
it('returns the correct jsonResponse on success', async () => { | ||
const result = await callLocal('https://json-api.chainweb.com', 'body'); | ||
expect(result.jsonResponse).toEqual({ json: 'some json' }); | ||
}); | ||
|
||
it('returns the correct textResponse on success', async () => { | ||
const response = new Response(); | ||
|
||
response.text = jest.fn().mockReturnValue('some text'); | ||
response.clone = jest.fn().mockImplementation(() => response); | ||
|
||
mockedFetch.mockResolvedValue(response); | ||
|
||
const result = await callLocal('https://api.chainweb.com', 'body'); | ||
|
||
const result = await callLocal('https://text-api.chainweb.com', 'body'); | ||
expect(result.textResponse).toBe('some text'); | ||
}); | ||
}); |
33 changes: 19 additions & 14 deletions
33
packages/tools/pactjs-cli/src/utils/tests/retrieveContractFromChain.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { defineConfig } from 'vitest/config'; | ||
|
||
export default defineConfig({ | ||
test: { | ||
include: ['src/**/*.test.ts'], | ||
globals: true, | ||
}, | ||
}); |
Oops, something went wrong.