找不到 e2e tsconfig 类型:错误 TS2304:找不到名称 'browser'

e2e tsconfig types not found: error TS2304: Cannot find name 'browser'

我正在尝试使用简单的 webdrioverio e2e 测试设置示例 angular 项目,但是 运行 我的 e2e 测试出现了一些编译错误。

tsconfig 设置

该项目主要使用以下文件设置:

e2e / test / [e2e test code]
e2e / tsconfig.e2e.json
e2e / wdio.conf.js
package.json
tsconfig.json

我的基地 tsconfig.json 看起来像:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2018",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

tsconfig.e2e.json 看起来像:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "module": "CommonJS",
    "outDir": "../dist/out-tsc/e2e",
    "baseUrl": "./",
    "target": "es2018",
    "types": [
      "node",
      "@wdio/sync",
      "@wdio/jasmine-framework",
      "jasminewd2"
    ]
  }
}

输出

运行 tsc 从我的 IDE 终端或尝试从 package.json 执行 e2e 脚本导致以下错误:

[0-0] 2021-02-09T15:47:53.019Z WARN @wdio/jasmine-framework: Unable to load spec files quite likely because they rely on `browser` object that is not fu
lly initialised.
`browser` object has only `capabilities` and some flags like `isMobile`.
Helper files that use other `browser` commands have to be moved to `before` hook.
Spec file(s): C:\dev\source\rme\angular-wdio6-builder-demo\e2e\test\specs\basic.spec.ts
 Error:  TSError: ⨯ Unable to compile TypeScript:
e2e/test/specs/basic.spec.ts(7,5): error TS2304: Cannot find name 'browser'.
e2e/test/specs/basic.spec.ts(8,19): error TS2304: Cannot find name 'browser'.
e2e/test/specs/basic.spec.ts(13,5): error TS2304: Cannot find name 'browser'.
e2e/test/specs/basic.spec.ts(14,21): error TS2581: Cannot find name '$'. Do you need to install type definitions for jQuery? Try `npm i @types/jquery`.

我的直觉和 MRE

我有一种感觉,当我以某种方式编译我的 e2e 代码时,`tsconfig.e2e.json` 内容被忽略了,因为它没有获取类型引用中指定的`browser`-object `@wdio/sync`。

完整的 MRE 可以找到 here on GitLab that was based on . It includes a CI script that shows the error output 我在本地 运行ning 时也遇到过。

另请注意,我的 intellij IDE 引用了 browser-object 并且 ctrl-clicking 它显示浏览器 var 位于 node_modules\@wdio\sync\webdriverio.d.ts 文件中。

edit/additional:package.json 中有一个 pree2e 脚本在 e2e 之前执行,它调用 e2e/e2e.cmd:

set TS_NODE_PROJECT=e2e/tsconfig.e2e.json

edit2:wdio.conf.js-配置文件:

包含(生成的)评论的完整文件也在最小可复制示例中:e2e/wdio.conf.js

exports.config = {
    runner: 'local',
    path: '/',
    specs: [
      `${__dirname}/test/specs/**/*.ts`
    ],
    exclude: [
    ],
    maxInstances: 10,
    capabilities: [{
        maxInstances: 5,
        browserName: 'chrome',
    }],
    logLevel: 'warn',
    bail: 0,
    baseUrl: `http://localhost:${process.env.PORT || 4200}`,
    waitforTimeout: 10000,
    connectionRetryTimeout: 90000,
    connectionRetryCount: 3,
    services: ['chromedriver'],
    framework: 'jasmine',
    jasmineNodeOpts: {
        requires: ['ts-node/register'],
        defaultTimeoutInterval: 60000,
        expectationResultHandler: function(passed, assertion) {
            // do something
        }
    },
}

您可以在 wdio.conf 文件中 运行 tsnode。 同样在你的茉莉花选项中,你应该需要 tsconfig-paths 来代替:

const tsNode = require('ts-node');
tsNode.register({
  files: true,
  project: './e2e/tsconfig.e2e.json'
});

exports.config = {
....
    jasmineNodeOpts: {
        // Required for Typescript
        requires: ['tsconfig-paths/register'],
...
};