0

自定义 eslint 规则在我们的项目中运行正常,但我不知道如何为它运行测试。我的猜测是我运行了错误版本的 ecmascript,我需要使用 babel 或调整 eslintrc.json 中的某些内容以使其与 mocha 脚本一起使用。

收到错误信息:

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

考试:

/**
 * @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"
            }]
        }
    ]
});

我试过的 .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"
}
}
4

1 回答 1

0

ESLint 文档建议您可以将配置对象传递给 RuleTester 构造函数。

在您的情况下,它可能应该如下所示:

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()
于 2021-01-02T19:02:27.433 回答