generated from onebeyond/open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds MSW Adds Vitest Creates a test using fake api call
- Loading branch information
Alexis Reina
authored and
Alexis Reina
committed
Nov 29, 2022
1 parent
200c341
commit c804201
Showing
8 changed files
with
4,500 additions
and
171 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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 { expect, test } from 'vitest'; | ||
import { render, screen } from '@testing-library/react'; | ||
import App from '../App' | ||
|
||
test('<App />', async () => { | ||
render(<App />); | ||
expect(await screen.findByText('MSWUser')).toBeInTheDocument(); | ||
}) |
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,12 @@ | ||
import { rest } from "msw"; | ||
|
||
export const handlers = [ | ||
// Handles a GET /hello request | ||
rest.get("https://jsonplaceholder.typicode.com/users/1", (_req, res, ctx)=> { | ||
return res(ctx.status(200), ctx.json({ | ||
id: 1, | ||
name: "MSW mocked name", | ||
username: "MSWUser", | ||
})) | ||
}), | ||
]; |
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,6 @@ | ||
// src/mocks/server.js | ||
import { setupServer } from 'msw/node' | ||
import { handlers } from './handlers' | ||
|
||
// This configures a request mocking server with the given request handlers. | ||
export const server = setupServer(...handlers) |
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,19 @@ | ||
// src/setupTests.ts | ||
import { server } from './mocks/server.js' | ||
import { beforeAll, afterEach, afterAll } from 'vitest' | ||
|
||
// jest-dom adds custom jest matchers for asserting on DOM nodes. | ||
// allows you to do things like: | ||
// expect(element).toHaveTextContent(/react/i) | ||
// learn more: https://github.com/testing-library/jest-dom | ||
import '@testing-library/jest-dom'; | ||
|
||
// Establish API mocking before all tests. | ||
beforeAll(() => server.listen({ onUnhandledRequest: 'error' })) | ||
|
||
// Reset any request handlers that we may add during the tests, | ||
// so they don't affect other tests. | ||
afterEach(() => server.resetHandlers()) | ||
|
||
// Clean up after the tests are finished. | ||
afterAll(() => server.close()) |
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,7 +1,14 @@ | ||
/// <reference types="vitest" /> | ||
|
||
import { defineConfig } from 'vite' | ||
import react from '@vitejs/plugin-react' | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig({ | ||
plugins: [react()] | ||
}) | ||
plugins: [react()], | ||
test: { | ||
globals: true, | ||
environment: 'jsdom', | ||
setupFiles: ['./src/setupTests.ts'], | ||
}, | ||
}) |