Skip to content

Commit

Permalink
feat: set up integration tests with msw
Browse files Browse the repository at this point in the history
  • Loading branch information
alessbell committed Apr 5, 2024
1 parent 7c2c9ed commit 3ef06a3
Show file tree
Hide file tree
Showing 11 changed files with 15,959 additions and 8,069 deletions.
23,828 changes: 15,827 additions & 8,001 deletions integration-tests/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"next",
"vite",
"vite-swc",
"vite-jest-msw",
"node-standard",
"node-esm",
"shared"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import type { Config } from "jest";

const config: Config = {
const config = {
globals: {
"globalThis.__DEV__": JSON.stringify(true),
},
testEnvironment: "jsdom",
setupFiles: ["./tests/jest.polyfills.js"],
setupFilesAfterEnv: ["./tests/setupTests.ts"],
setupFilesAfterEnv: ["./tests/setupTests.js"],
transform: {
"\\.(gql|graphql)$": "@graphql-tools/jest-transform",
".*": "babel-jest",
Expand Down
86 changes: 85 additions & 1 deletion integration-tests/vite-jest-msw/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion integration-tests/vite-jest-msw/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"@babel/preset-react": "^7.24.1",
"@babel/preset-typescript": "^7.24.1",
"@graphql-tools/jest-transform": "^2.0.0",
"@playwright/test": "*",
"@graphql-tools/schema": "^10.0.3",
"@graphql-tools/utils": "^10.1.2",
"@testing-library/jest-dom": "^6.4.2",
"@testing-library/react": "^14.2.2",
"@testing-library/user-event": "^14.5.2",
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/vite-jest-msw/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const httpLink = new HttpLink({
uri: "https://main--hack-the-e-commerce.apollographos.net/graphql",
});

const client = new ApolloClient({
export const client = new ApolloClient({
cache: new InMemoryCache(),
link: ApolloLink.from([delayLink, httpLink]),
});
Expand Down Expand Up @@ -54,7 +54,7 @@ export default function App() {

function Main() {
const { data } = useQuery(QUERY);
console.log({ data });

return data ?
<ul>
{data?.products.map(({ id, title }) => <li key={id}>{title}</li>)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@ import { render as rtlRender, screen } from "@testing-library/react";
import {
ApolloClient,
ApolloProvider,
InMemoryCache,
NormalizedCacheObject,
} from "@apollo/client";
import App from "../../src/App";
import App, { client } from "../../src/App";
import { schemaProxy } from "../mocks/handlers";

const client = new ApolloClient({
cache: new InMemoryCache(),
uri: "http://localhost:3000/graphql",
});

const render = (client: ApolloClient<NormalizedCacheObject>) =>
const render = (renderedClient: ApolloClient<NormalizedCacheObject>) =>
rtlRender(
<ApolloProvider client={client}>
<ApolloProvider client={renderedClient}>
<App />
</ApolloProvider>
);

beforeEach(() => {
// since all our tests now use our production Apollo Client instance
// since all our tests now use our "real" production Apollo Client instance,
// we need to reset the client cache before each test
return client.cache.reset();
});
Expand All @@ -32,36 +26,36 @@ describe("IndexRoute", () => {
test("renders", async () => {
render(client);

screen.debug();
expect(await screen.findByText(/product/i)).toBeInTheDocument();
// expect(await screen.findByText(/this is my playlist/i)).toBeInTheDocument();
// expect(await screen.findByText(/description/i)).toBeInTheDocument();
expect(await screen.findByText(/blue jays hat/i)).toBeInTheDocument();
});

// test("allows resolvers to be updated via schemaProxy", async () => {
// // using _restore = replaceSchema(
// // schemaProxy.fork({
// // resolvers: {
// // FeaturedPlaylistConnection: {
// // message: () => "purple seahorse",
// // edges: () => [{}],
// // },
// // },
// // })
// // );
test("allows resolvers to be updated via schemaProxy", async () => {
schemaProxy.add({
resolvers: {
Query: {
products: () => {
return [
{
id: "2",
title: "Mets Hat",
},
];
},
},
},
});

// render(client);
render(client);

// // the resolver has been updated
// // expect(await screen.findByText(/afternoon delight/i)).toBeInTheDocument();
// expect(await screen.findByText(/purple seahorse/i)).toBeInTheDocument();
// });
// the resolver has been updated
expect(await screen.findByText(/mets hat/i)).toBeInTheDocument();
});

// test("reset method works", async () => {
// schemaProxy.reset();
test("reset method works", async () => {
schemaProxy.reset();

// render(client);
render(client);

// expect(await screen.findByText(/afternoon delight/i)).toBeInTheDocument();
// });
expect(await screen.findByText(/blue jays hat/i)).toBeInTheDocument();
});
});
29 changes: 7 additions & 22 deletions integration-tests/vite-jest-msw/tests/mocks/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
import { graphql, HttpResponse } from "msw";
import { execute, GraphQLSchema } from "graphql";
import { execute } from "graphql";
import { gql } from "@apollo/client";
import { createMockSchema, createProxiedSchema } from "@apollo/client/testing";
import { makeExecutableSchema } from "@graphql-tools/schema";
import Schema from "../schema.graphql";

export const resolvers = {
Query: {
products: () => {
console.log("product resolver");
return [
{
id: "1",
title: "product",
description: "description",
mediaUrl: "http://example.com",
weight: 1.0,
price: {
amount: 1.0,
currency: "USD",
},
reviews: [],
averageRating: 5.0,
},
];
},
products: () => [
{
id: "1",
title: "Blue Jays Hat",
},
],
},
};

Expand Down Expand Up @@ -62,7 +50,6 @@ export let schemaProxy = createProxiedSchema(schema, resolvers);
export const handlers = [
graphql.operation(async ({ query, variables, operationName }) => {
const document = gql(query);
console.log({ execute });

const result = await execute({
document,
Expand All @@ -71,8 +58,6 @@ export const handlers = [
variableValues: variables,
});

console.log({ result });
console.log("AFTER");
return HttpResponse.json(result);
}),
];
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "@testing-library/jest-dom";
import { beforeAll, afterAll, afterEach, jest } from "@jest/globals";

import { beforeAll, afterAll, afterEach } from "@jest/globals";
import { server } from "./mocks/server";
import { gql } from "@apollo/client";

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/vite-jest-msw/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
"noFallthroughCasesInSwitch": true
},
"files": ["@types/graphql.d.ts"],
"include": ["src", "./tests/setupTests.ts"],
"include": ["src", "tests", "./tests/setupTests.ts"],
"references": [{ "path": "./tsconfig.node.json" }]
}
2 changes: 1 addition & 1 deletion integration-tests/vite-jest-msw/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"allowSyntheticDefaultImports": true
},
"files": ["@types/graphql.d.ts"],
"include": ["vite.config.ts", "./tests/setupTests.ts"]
"include": ["vite.config.ts", "tests/setupTests.js"]
}

0 comments on commit 3ef06a3

Please sign in to comment.