解决linter错误-未定义“浅” no-undef

Nik*_*dia 5 babel reactjs eslint jestjs enzyme

我正在使用JEST和酶。在我的eslint文件中,我在环境下添加了jest作为true。但是由于在全球范围内将其包含在浅表中,因此我得到了一个皮棉错误。错误为-错误'浅'未定义no-undef

setupTests.js

//as we are accessing our application with a http://localhost prefix, we need to update our jest configuration

import { shallow, render, mount, configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
// React 16 Enzyme adapter
configure({ adapter: new Adapter() });
// Make Enzyme functions available in all test files without importing
global.shallow = shallow;
global.render = render;
global.mount = mount;
Run Code Online (Sandbox Code Playgroud)

.eslintrc

{
  parser: "babel-eslint",
  "extends": ["airbnb"],
  "env": {
    "browser": true,
    "jest": true
  },
  "rules": {
    "max-len": [1, 200, 2, {ignoreComments: true}],
    "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
    "no-underscore-dangle": [0, { "allow": [] }],
    "jsx-a11y/label-has-associated-control": [
      "error", {
        "required": {
          "every": [ "id" ]
        }
      }
    ],
    "jsx-a11y/label-has-for": [
      "error", {
        "required": {
          "every": [ "id" ]
        }
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

app.test.js

import React from 'react';

import { LoginFormComponent } from '../../components';

describe('LoginForm', () => {
  const loginform = shallow(<LoginFormComponent />);

  it('renders correctly', () => {
    expect(loginform).toMatchSnapshot();
  });
});
Run Code Online (Sandbox Code Playgroud)

package.json

 "scripts": {
    "dev": "webpack-dev-server --historyApiFallback true --port 8888 --content-base build/",
    "test": "jest",
    "lint": "eslint ./src",
    "lintfix": "eslint ./src --fix"
  },
  "jest": {
    "verbose": true,
    "testURL": "http://localhost/",
    "transform": {
      "^.+\\.js$": "babel-jest"
    },
    "setupFiles": [
      "./setupTests.js"
    ],
    "snapshotSerializers": [
      "enzyme-to-json/serializer"
    ]
  },
Run Code Online (Sandbox Code Playgroud)

错误出现在我尝试使用浅层的app.test.js中。我一定要添加的东西在我的eslint配置为我做了这样的玩笑为真?

Buk*_*ari 6

添加全局语句如何?eslint 无 undef 文档

/*global someFunction b:true*/
/*eslint no-undef: "error"*/

var a = someFunction();
b = 10;
Run Code Online (Sandbox Code Playgroud)

或设置 global on .eslintrc( eslint global )

{
    "globals": {
        "shallow": true,
        "render": true,
        "mount": true
    }
}
Run Code Online (Sandbox Code Playgroud)