@apollo/graphql-testing-library

中文文档
0.3.0发布于 2024/08/30MITTypeScriptESM + CJS构建来源已验证近 30 天下载 20.6万

Apollo GraphQL Logo

GraphQL Testi

原始使用文档
文档目录3 节
  1. Installation
  2. Usage
  3. createHandler

Apollo GraphQL Logo

GraphQL Testing Library

Testing utilities that encourage good practices for TypeScript apps built with GraphQL.

npm version badge workflow Join our Discord server


GraphQL Testing Library provides utilities that make it easy to generate Mock Service Worker handlers for any GraphQL API.

MSW is the Testing Library-recommended way to declaratively mock API communication in your tests without stubbing window.fetch.

This library currently supports incremental delivery features @defer and @stream out of the box, with plans to support subscriptions over multipart HTTP as well as other transports such as WebSockets, currently in beta in MSW.

This project is not affiliated with the "Testing Library" ecosystem that inspired it. We're just fans :)

Installation

This library has peerDependencies listings for msw at ^2.0.0 and graphql at ^15.0.0 || ^16.0.0. Install them along with this library using your preferred package manager:

npm install --save-dev @apollo/graphql-testing-library msw graphql
pnpm add --save-dev @apollo/graphql-testing-library msw graphql
yarn add --dev @apollo/graphql-testing-library msw graphql
bun add --dev @apollo/graphql-testing-library msw graphql

Usage

createHandler

import { createHandler } from "@apollo/graphql-testing-library";

// We suggest using @graphql-tools/mock and @graphql-tools/schema
// to create a schema with mock resolvers.
// See https://the-guild.dev/graphql/tools/docs/mocking for more info.
import { addMocksToSchema } from "@graphql-tools/mock";
import { makeExecutableSchema } from "@graphql-tools/schema";
import typeDefs from "./schema.graphql";

// Create an executable schema
const schema = makeExecutableSchema({ typeDefs });

// Add mock resolvers
const schemaWithMocks = addMocksToSchema({
  schema,
  resolvers: {
    Query: {
      products: () =>
        Array.from({ length: 5 }, (_element, id) => ({
          id: `product-${id}`,
        })),
    },
  },
});

// `createHandler` returns an object with a `handler` and `replaceSchema`
// function: `handler` is a MSW handler that will intercept all GraphQL
// operations, and `replaceSchema` allows you to replace the mock schema
// the `handler` use to resolve requests against.
const { handler, replaceSchema } = createHandler(schemaWithMocks, {
  // It accepts a config object as the second argument where you can specify a
  // delay duration, which uses MSW's delay API:
  // https://mswjs.io/docs/api/delay
  // Default: "real" (100-400ms in browsers, 20ms in Node-like processes)
  delay: number | "infinite" | "real",
});

版本与发布记录

@apollo/graphql-testing-library 共发布 11 个版本,首次发布于 2024/06/15;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.3.0latest2024/08/302 年前
0.0.0-pr-93-20240814151011pr2024/08/142 年前
0.0.0-pr-83-202408132042142024/08/142 年前
0.0.0-pr-83-202408051911022024/08/062 年前
0.0.0-pr-83-202408012129132024/08/022 年前
0.0.0-pr-73-202407242033482024/07/252 年前

关于 @apollo/graphql-testing-library 的常见问题

@apollo/graphql-testing-library 是什么?

GraphQL Testi

如何安装 @apollo/graphql-testing-library?

在项目目录运行 npm install @apollo/graphql-testing-library,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:msw(^2.0.0)、graphql(^15.0.0 || ^16.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@apollo/graphql-testing-library 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@apollo/graphql-testing-library 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@apollo/graphql-testing-library 可以商用吗?

@apollo/graphql-testing-library 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@apollo/graphql-testing-library 的下载量有多少?

根据 npm 官方下载统计,@apollo/graphql-testing-library 在 2026/10/07 – 2026/11/05 这 30 天内被下载 205,948 次,日均约 6,865 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@apollo/graphql-testing-library 还在维护吗?

最新版本 0.3.0 发布于 2024/08/30(2 年前);至今共发布 11 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/06/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。