简体   繁体   中英

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. 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.

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:

{
"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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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