Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vite plugin a la jest-transform for GraphQL? #5991

Open
jhalborg opened this issue Mar 18, 2024 · 0 comments
Open

Vite plugin a la jest-transform for GraphQL? #5991

jhalborg opened this issue Mar 18, 2024 · 0 comments

Comments

@jhalborg
Copy link

Is your feature request related to a problem? Please describe.

In Jest, you expose this transformer:

transform: {
    ....
    '\\.(gql|graphql)$': '@graphql-tools/jest-transform',
  },

Describe the solution you'd like
I am trying to move our tests from Jest to Vite, but am having trouble. It seems any test that includes GraphQL queries fail. We use

"@graphql-codegen/cli": "^5.0.0",
"@graphql-codegen/client-preset": "^4.1.0",

to generate types for our queries in integration tests. For example:

import { gql } from 'apollo-server-core';
import { GlobalUsersService } from '../__generated__/asset';
import { mockOperation } from './mockOperation';
import {
  UserNameQuery,
  UserNameQueryVariables,
} from '../__generated__/test-types/graphql';

const userNameQuery = gql(/* GraphQL */ `
  query UserName($emailAddress: String!) {
    user(emailAddress: $emailAddress) {
      ... on User {
        firstName
      }
    }
  }
`);

describe('verification test setup verification', () => {
  it('overrides context defaults', async () => {
    const firstNameMock = 'Svend';
    const emailToQuery = '[email protected]';

    const usersSpy = jest
      .spyOn(GlobalUsersService, 'getGlobalUserByEmailAddress')
      .mockResolvedValue({
        globalUsers: [
          {
            firstName: firstNameMock,
            b2CObjectId: 'someID',
          },
        ],
      });

    const result = await mockOperation<UserNameQuery, UserNameQueryVariables>(
      userNameQuery,
      {
        emailAddress: emailToQuery,
      },
      {
        // Override context.user with signInName to satisfy auth scope
        user: {
          signInName: emailToQuery,
        },
      }
    );

    expect(usersSpy).toHaveBeenCalledTimes(1);

    const { firstName } = result.data.user;

    expect(firstName).toEqual(firstNameMock);
  });
});

Describe alternatives you've considered

I hoped the graphql plugin for rollup would solve it, but it didn't :(

import { defineConfig } from 'vitest/config';
import graphql from '@rollup/plugin-graphql';

export default defineConfig(() => ({
  plugins: [graphql()],
  test: {
    globals: true,
    environment: 'node',
    globalSetup: './src/globalJestSetup.ts',
    reporters: ['verbose'],
    include: ['./src/**/*.{test,spec}.ts'],
  },
}));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant