简体   繁体   English

为自定义 eslint 规则编写测试时保留 AssertionError 'import'

[英]AssertionError 'import' is reserved when writing tests for custom eslint rule

The custom eslint rule runs ok in our project, but I cannot figure out how to run tests for it.自定义 eslint 规则在我们的项目中运行正常,但我不知道如何为它运行测试。 My guess is that I am running wrong version of ecmascript and I need to use babel or adjust something in eslintrc.json to make this work with the mocha scripts.我的猜测是我运行了错误版本的 ecmascript,我需要使用 babel 或调整 eslintrc.json 中的某些内容以使其与 mocha 脚本一起使用。

Getting error message:收到错误信息:

AssertionError [ERR_ASSERTION]: A fatal parsing error occurred: Parsing error: The keyword 'import' is reserved

The test:考试:

/**
 * @fileoverview Prohibit import underscore to help tree
 * @author Jason Hocker
 */
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var rule = require("../../../lib/rules/no-full-lodash-import"),

RuleTester = require("eslint").RuleTester;


//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

var ruleTester = new RuleTester();
ruleTester.run("no-full-lodash-import", rule, {

valid: [
    {code: "import os from \"os\";\nimport fs from \"fs\";" },
    {code: "import { merge } from \"lodash-es\";"}
],

invalid: [
    {
        code: "import _ from 'lodash';",
            errors: [{
                message: "Fill me in.",
                type: "Me too"
            }]
        }
    ]
});

One of the.jslintrc.json files I tried:我试过的 .jslintrc.json 文件之一:

{
"env": {
    "browser": true,
    "es6": true,
    "mocha": true // add mocha as true to your ".eslintrc. *" file
},
"parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
        "jsx": true
    }
},
"rules": {
    "semi": "error"
}
}

ESLint docs suggest that you can pass a configuration object to the RuleTester constructor. ESLint 文档建议您可以将配置 object 传递给 RuleTester 构造函数。

In your case it should probably look like this:在您的情况下,它可能应该如下所示:

var config = {
  env: {
    browser: true,
    es6: true,
    mocha: true, // add mocha as true to your ".eslintrc. *" file
  },
  parserOptions: {
    ecmaVersion: 6,
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
    },
  },
}
var ruleTester = new RuleTester()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM