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

javascript - ESLint doesn't recognize node.js's 'global' object - Stack Overflow

programmeradmin2浏览0评论

The error:

3:5  error  'global' is not defined  no-undef

My current ESLint config:

module.exports = {
  parser: "babel-eslint",
  env: {
    browser: true,
    es6: true,
    "jest/globals": true,
    jest: true
  },
  extends: ["eslint:remended", "plugin:react/remended", "prettier", "prettier/react"],
  parserOptions: {
    ecmaFeatures: {
      experimentalObjectRestSpread: true,
      jsx: true
    },
    sourceType: "module"
  },
  globals: {
    testGlobal: true
  },
  plugins: ["react", "prettier", "jest"],
  rules: {
    "prettier/prettier": 1,
    "no-console": 0
  }
};

An simplified example test file that causes the ESLint error:

describe("Jest global:", () => {
  it("should not cause ESLint error", () => {
    global.testGlobal = {
      hasProp: true
    };
  });
});

I expected this Jest feature to be covered by having the env: { jest: true } in the eslint config. I can of course disable the rule or line in the file, but then I'd need to do that every time I use global.

The error:

3:5  error  'global' is not defined  no-undef

My current ESLint config:

module.exports = {
  parser: "babel-eslint",
  env: {
    browser: true,
    es6: true,
    "jest/globals": true,
    jest: true
  },
  extends: ["eslint:remended", "plugin:react/remended", "prettier", "prettier/react"],
  parserOptions: {
    ecmaFeatures: {
      experimentalObjectRestSpread: true,
      jsx: true
    },
    sourceType: "module"
  },
  globals: {
    testGlobal: true
  },
  plugins: ["react", "prettier", "jest"],
  rules: {
    "prettier/prettier": 1,
    "no-console": 0
  }
};

An simplified example test file that causes the ESLint error:

describe("Jest global:", () => {
  it("should not cause ESLint error", () => {
    global.testGlobal = {
      hasProp: true
    };
  });
});

I expected this Jest feature to be covered by having the env: { jest: true } in the eslint config. I can of course disable the rule or line in the file, but then I'd need to do that every time I use global.

Share Improve this question edited Jul 24, 2018 at 19:19 Aaron Brager 66.3k20 gold badges167 silver badges293 bronze badges asked Sep 25, 2017 at 20:48 bananabananabananabanana 4734 silver badges15 bronze badges 2
  • Tracked down ESLint's globals dependency github./sindresorhus/globals/blob/… Seems it doesn't have global in it... I'm guessing it should. – bananabanana Commented Sep 25, 2017 at 21:03
  • Filed an issue with the globals project: github./sindresorhus/globals/issues/118 Figured I'd file an issue with the ESLint team after – bananabanana Commented Sep 25, 2017 at 21:43
Add a ment  | 

1 Answer 1

Reset to default 9

The global object is part of Node.js. It is not specific to Jest and therefore it's not included in the jest environment. In fact, you are running your unit tests in Node and you happen to use the global object for your tests. In general the globals defined for specific libraries are the ones they provide to make it more convenient to use them without having to import them. A counterexample would be AVA, which requires you to import it instead of defining globals.

If you want to use ESLint for the tests as well, you would have to add the node environment.

env: {
  browser: true,
  es6: true,
  node: true,
  jest: true
},
发布评论

评论列表(0)

  1. 暂无评论