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

618 views Asked by At

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"
}
}
1

There are 1 answers

0
hodbn On

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