javascript - Jest: TypeError: Cannot destructure property 'print' of '_graphql.default' as it is

I'm new to using Jest and I'm trying to test my helper function.I'm not using any Types

I'm new to using Jest and I'm trying to test my helper function. I'm not using any Typescript at the moment.

This is the error I get when I run npm run test:

TypeError: Cannot destructure property 'print' of '_graphql.default' as it is undefined.

helper.test.js:

import getDuplicateError from '../../helpers/auth'

test('Check if email already exists', () => {
  error = "duplicate_email_key";
  expect(getDuplicateError(error)).toBe(true);
})

auth.js:

import graphql from "graphql";
const { print } = graphql;

export const getDuplicateError = (errors) => {
  /*...*/ // Actual implementation doesn't use print
};

jest.config.js

export default {
  preset: 'ts-jest/presets/js-with-babel',
  testEnvironment: "node",
  transform: {
    "^.+\\.(js|jsx)?$": "babel-jest",
    '^.+\\.(ts|tsx)?$': 'ts-jest',
  },
  transformIgnorePatterns: [
    "node_modules/(?!variables/.*)"
  ],
  coveragePathIgnorePatterns: [
    "/node_modules/"
  ]
};

babel.config.json

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": true
        }
      }
    ]
  ]
}

package.json

"devDependencies": {
    "@babel/preset-env": "^7.12.17",
    "babel-jest": "^26.6.3",
    "jest": "^26.6.3",
    "ts-jest": "^26.5.1",
    "ts-node": "^9.1.1"
  }

Even though print isn't being used for this function (it is used in other ones), the overall issue is that I can't test anything right now because of this issue, and will also need to be able to use it for other functions.

Is there a configuration that I need for destructuring to work? Am I not able to use destructuring on other libraries? Will definitely prefer to be able to destructure like this because I use a lot of destructuring in my code.

Let me know if I need to provide anything else.

I'm new to using Jest and I'm trying to test my helper function. I'm not using any Typescript at the moment.

This is the error I get when I run npm run test:

TypeError: Cannot destructure property 'print' of '_graphql.default' as it is undefined.

helper.test.js:

import getDuplicateError from '../../helpers/auth'

test('Check if email already exists', () => {
  error = "duplicate_email_key";
  expect(getDuplicateError(error)).toBe(true);
})

auth.js:

import graphql from "graphql";
const { print } = graphql;

export const getDuplicateError = (errors) => {
  /*...*/ // Actual implementation doesn't use print
};

jest.config.js

export default {
  preset: 'ts-jest/presets/js-with-babel',
  testEnvironment: "node",
  transform: {
    "^.+\\.(js|jsx)?$": "babel-jest",
    '^.+\\.(ts|tsx)?$': 'ts-jest',
  },
  transformIgnorePatterns: [
    "node_modules/(?!variables/.*)"
  ],
  coveragePathIgnorePatterns: [
    "/node_modules/"
  ]
};

babel.config.json

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": true
        }
      }
    ]
  ]
}

package.json

"devDependencies": {
    "@babel/preset-env": "^7.12.17",
    "babel-jest": "^26.6.3",
    "jest": "^26.6.3",
    "ts-jest": "^26.5.1",
    "ts-node": "^9.1.1"
  }

Even though print isn't being used for this function (it is used in other ones), the overall issue is that I can't test anything right now because of this issue, and will also need to be able to use it for other functions.

Is there a configuration that I need for destructuring to work? Am I not able to use destructuring on other libraries? Will definitely prefer to be able to destructure like this because I use a lot of destructuring in my code.

Let me know if I need to provide anything else.

Share Improve this question asked Feb 22, 2021 at 6:58 m1771vwm1771vw 7852 gold badges12 silver badges24 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

try to import like this

import * as graphql from "graphql";
const {print}=graphql;

graphql is not exported from graphql package and hence it is undefined. Again when you try to destructure print from it, It is throwing error.

I had to provide a mock for this one for it to ignore the destructure. I'm not sure if this is the best way to go about this but this is one solution to go past it. If someone knows of another way to go about it, I'd appreciate other responses!

 jest.mock(
    'graphql',
    () => {
      const mPrint = { t: jest.fn().mockReturnValue('test') };
      return {
        print: jest.fn(() => mPrint),
      };
    },
    { virtual: true },
  );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744407241a4572696.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信