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

Jest 28 failure: should return an object or a Promise resolving to an object #13

Open
granttchart opened this issue Jun 6, 2022 · 14 comments

Comments

@granttchart
Copy link

The following error is being thrown on Jest 28.1.0 due to Jest breaking changes. https://jestjs.io/docs/upgrading-to-jest28#transformer explains the necessary fix.

      `process()` or/and `processAsync()` method of code transformer found at 
      "node_modules/jest-transform-graphql/index.js" 
      should return an object or a Promise resolving to an object. The object 
      must have `code` property with a string of processed code.
      This error may be caused by a breaking change in Jest 28:
      https://jestjs.io/docs/upgrading-to-jest28#transformer
      Code Transformation Documentation:
      https://jestjs.io/docs/code-transformation
@ezmiller
Copy link

+1 also running into this error

@dep
Copy link

dep commented Jun 30, 2022

+1 also running into this error.

@dep
Copy link

dep commented Jul 8, 2022

@granttchart Have you found a workaround for this?

@dep
Copy link

dep commented Jul 8, 2022

Folks ending up here might have some luck switching to this:

https://www.npmjs.com/package/@graphql-tools/jest-transform

@dobesv
Copy link

dobesv commented Jul 13, 2022

@graphql-tools/jest-transform isn't quite compatible, and doesn't allow multiple operations per file.

@SimonSomlai
Copy link

SimonSomlai commented Jul 25, 2022

Worked for me!;

npm i -D @graphql-tools/jest-transform

**jest.config.js**
{
  "jest": {
    "transform": {
      "\\.(gql|graphql)$": "@graphql-tools/jest-transform"
    }
  }
}

@andre-muller
Copy link

I was trying to use @graphql-tools/jest-transform but it's throwing an error when file has multiple operations.

  ● Test suite failed to run

    TypeError: Cannot read properties of undefined (reading 'source')

      36 |  * Used to generate useUseUserProjectsProjectListQuery
      37 |  */
    > 38 | export const USER_PROJECTS_CONNECTION_QUERY = gql`
         |                                                  ^
      39 |   query useUserProjects_ProjectList(
      40 |     $projectFilters: ProjectFilters
      41 |     $listInputs: ListInput

      at ../node_modules/graphql-tag/src/index.ts:127:25
          at Array.forEach (<anonymous>)
      at gql (../node_modules/graphql-tag/src/index.ts:125:8)
      at Object.<anonymous> (src/views/hooks/useUserProjects.tsx:38:50)
      at Object.<anonymous> (src/views/components/UpdateAssigneesDialog/ProjectList.tsx:19:63)
      at Object.<anonymous> (src/views/components/UpdateAssigneesDialog/ProjectReassign.tsx:27:59)
      at Object.<anonymous> (src/views/components/UpdateAssigneesDialog/UpdateAssigneesDialog.tsx:18:63)
      at Object.<anonymous> (src/views/components/UpdateAssigneesDialog/index.ts:9:69)
      at Object.<anonymous> (src/views/components/EditUserDrawer/components/EditUserForm/EditUserForm.tsx:41:69)
      at Object.<anonymous> (src/views/components/EditUserDrawer/components/EditUserForm/EditUserForm.test.tsx:13:60)
      ```

@monigala
Copy link

The following error is being thrown on Jest 28.1.0 due to Jest breaking changes. https://jestjs.io/docs/upgrading-to-jest28#transformer explains the necessary fix.

      `process()` or/and `processAsync()` method of code transformer found at 
      "node_modules/jest-transform-graphql/index.js" 
      should return an object or a Promise resolving to an object. The object 
      must have `code` property with a string of processed code.
      This error may be caused by a breaking change in Jest 28:
      https://jestjs.io/docs/upgrading-to-jest28#transformer
      Code Transformation Documentation:
      https://jestjs.io/docs/code-transformation

The above link to the resolution/explanation results in 404.

@MasterOdin
Copy link

The first link should be https://jestjs.io/docs/28.x/upgrading-to-jest28#transformer.

@SethDavenport
Copy link

SethDavenport commented Dec 29, 2022

Should be able to do something like this then:

jest.config.js:

{
  "jest": {
    "transform": {
      "\\.(gql|graphql)$": "./transform-graphql-jest-28-shim.js"
    }
  }
}

transform-graphql-jest-28-shim.js:

const { process: upstreamProcess } = require('jest-transform-graphql');

const process = (...args) => {
  const code = upstreamProcess(...args);
  return { code };
};

module.exports = { process };

@dep
Copy link

dep commented Jan 19, 2023

Thank you @SethDavenport this is a great solution.

@yogeshgadge
Copy link

yogeshgadge commented Aug 28, 2023

Neither of these workarounds worked jest 29.6.4 in a Typescript project

It works fine in rollup build but I am unit testing my source.

@anewstead
Copy link

anewstead commented Sep 19, 2023

@yogeshgadge

Neither of these workarounds worked jest 29.6.4 in a Typescript project

It works fine in rollup build but I am unit testing my source.

thought i had this too, try running
jest --clearCache
and retry with @SethDavenport shim
worked for me

@jorgequintt
Copy link

Should be able to do something like this then:

jest.config.js:

{
  "jest": {
    "transform": {
      "\\.(gql|graphql)$": "./transform-graphql-jest-28-shim.js"
    }
  }
}

transform-graphql-jest-28-shim.js:

const { process: upstreamProcess } = require('jest-transform-graphql');

const process = (...args) => {
  const code = upstreamProcess(...args);
  return { code };
};

module.exports = { process };

This was a life saver. Thank you very much

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