【问题标题】:Node.js find all undefinded variable from projectNode.js 从项目中找到所有未定义的变量
【发布时间】:2021-07-01 18:46:47
【问题描述】:

我正在使用 Node.js 开发后端 API
有时,我无法进行单元测试,并且出现data is not defined 错误
使用 typescript,我们可以通过 lint 系统解决这个问题。
Angular 9 项目,编译系统检测到这个问题,这个错误会自动解决。

但是,如何配置 node.js 项目?

我检查了 esLint 工具,但是,它似乎可以使用 ts 文件

【问题讨论】:

  • 看到你用过 Typescript,我也只会用 Typescript 做节点。即使有一个好的 linter,JavaScript 的范围也是有限的。
  • 您没有为您尝试检测的情况显示实际代码,因此我们无法真正帮助您解决任何具体问题。 Javascript 的通用工具在严格模式或 lint 工具下运行,如果你想要更多,然后转移到 TypeScript。仅供参考,与此相关的任何内容都不会阻止您编写单元测试,因此您可能对其他内容感到困惑。请出示相关代码。

标签: javascript node.js unit-testing configuration syntax-error


【解决方案1】:

eslint 解决了我的问题
另外,我在 git commit 之前使用 git hook 来检查 eslint https://gist.github.com/linhmtran168/2286aeafe747e78f53bf

.eslintrc.json

{
  "env": {
    "es6": true,
    "node": true
  },
  "extends": "eslint:recommended",
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 8,
    "sourceType": "module"
  },
  "rules": {
    "indent": [
      "off",
      4,
      {
        "FunctionDeclaration": {
          "parameters": "first"
        },
        "FunctionExpression": {
          "parameters": "first"
        },
        "SwitchCase": 1
      }
    ],
    "max-len": [
      "error",
      {
        "code": 1000
      }
    ],
    "quotes": [
      "off",
      "single"
    ],
    "semi": [
      "warn",
      "always"
    ],
    "no-unused-vars": [
      "off",
      {
        "args": "after-used",
        "argsIgnorePattern": "^next$"
      }
    ],
    "comma-dangle": [
      "off",
      "always-multiline"
    ],
    "arrow-parens": [
      0,
      "as-needed",
      {
        "requireForBlockBody": true
      }
    ],
    "eol-last": [
      "off",
      "always"
    ],
    "no-multiple-empty-lines": [
      "off",
      {
        "max": 1,
        "maxEOF": 0
      }
    ],
    "prefer-const": "off",
    "no-var": "off",
    "prefer-arrow-callback": [
      "off"
    ],
    "object-curly-spacing": [
      "off",
      "always"
    ],
    "camelcase": "off",
    "eqeqeq": "off",
    "no-console": "off",
    "no-control-regex": 0,
    "no-useless-escape": 0,
    "no-constant-condition": "off",
    "no-extra-boolean-cast": "off",
    "no-empty": "off",
    "no-redeclare": "off",
    "no-self-assign": "off",
    "no-async-promise-executor": "off"
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多