最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

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 },
  );
发布评论

评论列表(0)

  1. 暂无评论