2017-09-25 91 views
1

錯誤:ESLint不承認玩笑的 'Global' 對象

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

我目前ESLint配置:

module.exports = { 
    parser: "babel-eslint", 
    env: { 
    browser: true, 
    es6: true, 
    "jest/globals": true, 
    jest: true 
    }, 
    extends: ["eslint:recommended", "plugin:react/recommended", "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 
    } 
}; 

引起ESLint錯誤的一個簡單的例子測試文件:

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

我期望通過在eslint配置中使用env: { jest: true }來涵蓋此Jest功能。我當然可以禁用文件中的規則或行,但是每次我使用global時都需要這樣做。

+0

追查ESLint的全局依賴https://github.com/sindresorhus/globals/blob/7c53146f46ba4633fda9479d5ecfa6a4d62193c6/globals.json#L925似乎它並沒有在它的全球...我猜它應該。 – bananabanana

+0

提交了一個全局項目的問題:https://github.com/sindresorhus/globals/issues/118 想象我會向ESLint團隊提交一個問題 – bananabanana

回答

2

global object是Node.js的一部分。它並不特定於Jest,因此它不包含在jest環境中。實際上,您正在Node中運行單元測試,並且恰好使用global對象進行測試。一般來說,爲特定庫定義的全局變量是它們提供的全局變量,以便使用它們時更方便,而無需導入它們。一個反例是AVA, which requires you to import it而不是定義全局變量。

如果您想要使用ESLint進行測試,則必須添加node環境。

env: { 
    browser: true, 
    es6: true, 
    node: true, 
    jest: true 
},