【问题标题】:Expect-webdriverio doesn't work with TypeScriptExpect-webdriverio 不适用于 TypeScript
【发布时间】:2021-05-14 21:37:19
【问题描述】:

TypeScript 无法识别来自 expect-webdriverio 的expect 命令。
它给出了一个错误

TS2304: Cannot find name 'expect'.  

这是我的package.json

 "devDependencies": {
    "@cucumber/cucumber": "^7.1.0",
    "@percy/cli": "^1.0.0-beta.48",
    "@percy/webdriverio": "^2.0.0",
    "@types/chai": "^4.2.16",
    "@types/node": "^14.14.41",
    "@typescript-eslint/eslint-plugin": "^4.22.0",
    "@typescript-eslint/parser": "^4.22.0",
    "allure-commandline": "^2.13.8",
    "app-root-path": "^3.0.0",
    "chai": "^4.3.4",
    "eslint": "^7.24.0",
    "eslint-config-prettier": "^8.2.0",
    "eslint-plugin-prettier": "^3.4.0",
    "husky": "^6.0.0",
    "npm-run-all": "^4.1.5",
    "prettier": "^2.2.1",
    "ts-node": "^9.1.1",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^4.2.4",
    "webdriverio": "^7.5.2"
  },
  "dependencies": {
    "@wdio/allure-reporter": "^7.4.2",
    "@wdio/cli": "^7.4.6",
    "@wdio/cucumber-framework": "^7.4.6",
    "@wdio/local-runner": "^7.4.6",
    "@wdio/selenium-standalone-service": "^7.4.2",
    "@wdio/spec-reporter": "^7.4.3",
    "@wdio/sync": "^7.4.6",
    "@wdio/types": "^7.4.2",
    "expect": "^26.6.2",
    "expect-webdriverio": "^2.0.2"
  }

这是我的tsconfig.json 文件:

{
  "compilerOptions": {
    "outDir": "./.tsbuild/",
    "baseUrl": ".",
    "paths": {
      "*": [
        "./*"
      ],
      "src/*": [
        "./src/*"
      ],
      "test/*": [
        "src/step_definitions/*"
      ]
    },
    "target": "es2019",                       /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "removeComments": true,                /* Do not emit comments to output. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "noImplicitAny": false,                   /* Raise error on expressions and declarations with an implied 'any' type. */
     "strictNullChecks": true,              /* Enable strict null checks. */
     "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
    "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    "types": [
      "webdriverio/sync",
      "node",
      "@wdio/cucumber-framework",
      "@wdio/selenium-standalone-service"
    ], 
    // "allowSyntheticDefaultImports": true, "esModuleInterop": true are needed for percy 2 to work
    "allowSyntheticDefaultImports": true,     /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true,                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "sourceMap": false
  },
  "include": [
    "./src/**/*.ts",
    "./src/**/**/*.ts",
    "node_modules/expect-webdriverio/types/jest-global.d.ts" // added as suggested by IntelliJ IDEA, but seems fishy
  ]
}

IntelliJ IDEA 建议添加:

"node_modules/expect-webdriverio/types/jest-global.d.ts"

tsconfig.json 中的这一行。
必须将 node_modules 文件夹中的文件添加到 tsconfig.json 文件中对我来说似乎很奇怪。
我在这里做错了什么?
官方文档中的任何地方都没有提到这一点,我也找不到像这样设置的样板项目。
有任何想法吗? 谢谢!

【问题讨论】:

    标签: typescript webdriver-io


    【解决方案1】:

    我在仔细研究样板项目时发现了它。

    "types": [
      "webdriverio/sync",
      "node",
      "@wdio/cucumber-framework",
      "@wdio/selenium-standalone-service",
      "expect-webdriverio" // THIS line needed to be added
    ],
    

    我从 package.json 中删除了这些行,因为 expect-webdriverio 应该与更高版本的框架捆绑在一起(在本例中为版本 7)

     "expect": "^26.6.2",
     "expect-webdriverio": "^2.0.2"
    

    【讨论】:

      猜你喜欢
      • 2018-02-06
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 2021-11-15
      • 2019-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多