
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
msw-introspection
Advanced tools
> Default mocked responses from a graphql query via msw and graphql introspection
msw-introspection
Default mocked responses from a graphql query via msw and graphql introspection
(introspection) -> (req,res,ctx) -> MockedResponse
Creates a msw handler for GraphQL operations. The handler returns mocked data that conforms to the schema types
Setup mocks
import { graphql } from "msw";
import { createIntrospectionHandler } from "msw-introspection";
import introspection from "../../graphql/introspection.json";
const introspectionHandler = createIntrospectionHandler(introspection);
const handlers = [graphql.operation(introspectionHandler)];
export const server = setupServer(...handlers);
Setup Jest
import "@testing-library/jest-dom/extend-expect";
import { client } from "./ApolloClient";
import { server } from "./mocks/server";
beforeAll(() => {
server.listen();
});
beforeEach(() => {
return client.clearStore();
});
afterEach(() => {
server.resetHandlers();
});
afterAll(() => {
server.close();
});
The testing for this library illustrates how to use MSW to mock a GQL API and get default responses from an introspected schema. The schema used for testing is provided by the SpaceX GraphQL API.
npm run download:schema
The /test
directory sets up an ApolloClient instance, a React component that calls useQuery
, and the utilities that set up the msw server.
├── test
│ ├── graphql
│ │ ├── introspection.json # Output of graphql-codegen
│ │ ├── schema.gql # Schema downloaded from SpaceX API
│ │ ├── types.d.ts # Output of graphql-codegen
│ ├── mocks # MSW code
│ ├── ApolloClient.ts # An instance of apollo client
│ ├── codegen.yml # Generate ts types and introspection from graphql schema
│ ├── index.test.tsx # Test suite
│ ├── rockets.tsx # Simple react component that makes a useQuery call.
│ ├── setupTests.ts # Enables mocking for unit tests via `beforeAll`/`afterAll` hooks.
FAQs
> Default mocked responses from a graphql query via msw and graphql introspection
The npm package msw-introspection receives a total of 2 weekly downloads. As such, msw-introspection popularity was classified as not popular.
We found that msw-introspection demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.