簡體   English   中英

為自定義 eslint 規則編寫測試時保留 AssertionError 'import'

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

自定義 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"
}
}

ESLint 文檔建議您可以將配置 object 傳遞給 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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM