使用绝对导入路径确实会破坏 Testcafe 中 Typescript 的编译器

发布于 2025-01-11 07:53:02 字数 3138 浏览 0 评论 0原文

Intellij 确实可以正确解析,但在我的测试文件中使用绝对导入路径时,Testcafe 编译器会抛出错误。 在应用程序源文件中使用绝对路径不会引发任何错误。

这里可能有什么问题?

这些是我的版本

"typescript": "~3.9.10",
"testcafe": "^1.16.0",
"@angular/core": "~10.2.5"

这是我的项目结构:

angular
├── e2e/
│   ├── tsconfig.json
│   └── src
│       ├── noResults.ts
│       └── test.e2e-spec.ts
├── node_modules/
├── src/
│   └── <project files>
├── tsconfig.json
├── testcaferc.json

这是我的 tsconfig:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "downlevelIteration": true,
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2020",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "typeRoots": ["node_modules/@types"],
    "lib": ["es2018", "es2015", "dom"],
    "noImplicitAny": true,
    "strictNullChecks": true,
    "charset": "utf8",
    "newLine": "lf",
    "allowSyntheticDefaultImports": true
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "preserveWhitespaces": true,
    "strictTemplates": true
  }
}

这是我的 e2e/tsconfig.json:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../dist/out-tsc/e2e",
    "target": "ESNext",
    "sourceMap": false,
    "resolveJsonModule": true,
    "esModuleInterop": true
  }
}

这是我的 testcaferc.json:

{
  "compilerOptions": {
    "typescript": {
      "configPath": "./e2e/tsconfig.json"
    }
  }
}

这是我在 e2e/src/test.e2e-spec.ts 中的导入:

import { noResults } from 'e2e/src/no-results';

这是我在 e2e/src/noResults.ts 中导出

export const noResults = () => {...}

这是我的错误:

- /home/IdeaProjects/angular/node_modules/testcafe/lib/cli/index.js
at Object.<anonymous> (/home/IdeaProjects/angular/e2e/src/test.e2e-spec.ts:7:1) {
  code: 'E1035',
    data: [
    "Error: Cannot find module 'e2e/src/noResults'\n" +
    'Require stack:\n' +
    '- /home/IdeaProjects/angular/e2e/test.e2e-spec.ts\n' +
    '- /home/deaProjects/angular/node_modules/testcafe/lib/compiler/test-file/formats/es-next/compiler.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/compiler/compilers.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/compiler/index.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/runner/bootstrapper.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/runner/index.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/live/test-runner.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/testcafe.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/index.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/cli/cli.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/cli/index.js'
  ]
}

Intellij does resolve correctly but the Testcafe compiler throws an error when using absolute import paths in my test files.
Using absolute paths within the application source files does not throw any errors.

What could be the problem here?

These are my versions

"typescript": "~3.9.10",
"testcafe": "^1.16.0",
"@angular/core": "~10.2.5"

This is my project structure:

angular
├── e2e/
│   ├── tsconfig.json
│   └── src
│       ├── noResults.ts
│       └── test.e2e-spec.ts
├── node_modules/
├── src/
│   └── <project files>
├── tsconfig.json
├── testcaferc.json

This is my tsconfig:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "downlevelIteration": true,
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "es2020",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "typeRoots": ["node_modules/@types"],
    "lib": ["es2018", "es2015", "dom"],
    "noImplicitAny": true,
    "strictNullChecks": true,
    "charset": "utf8",
    "newLine": "lf",
    "allowSyntheticDefaultImports": true
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "preserveWhitespaces": true,
    "strictTemplates": true
  }
}

This is my e2e/tsconfig.json:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../dist/out-tsc/e2e",
    "target": "ESNext",
    "sourceMap": false,
    "resolveJsonModule": true,
    "esModuleInterop": true
  }
}

This is my testcaferc.json:

{
  "compilerOptions": {
    "typescript": {
      "configPath": "./e2e/tsconfig.json"
    }
  }
}

This is my import in the e2e/src/test.e2e-spec.ts:

import { noResults } from 'e2e/src/no-results';

This my export in e2e/src/noResults.ts

export const noResults = () => {...}

This is my error:

- /home/IdeaProjects/angular/node_modules/testcafe/lib/cli/index.js
at Object.<anonymous> (/home/IdeaProjects/angular/e2e/src/test.e2e-spec.ts:7:1) {
  code: 'E1035',
    data: [
    "Error: Cannot find module 'e2e/src/noResults'\n" +
    'Require stack:\n' +
    '- /home/IdeaProjects/angular/e2e/test.e2e-spec.ts\n' +
    '- /home/deaProjects/angular/node_modules/testcafe/lib/compiler/test-file/formats/es-next/compiler.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/compiler/compilers.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/compiler/index.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/runner/bootstrapper.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/runner/index.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/live/test-runner.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/testcafe.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/index.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/cli/cli.js\n' +
    '- /home/IdeaProjects/angular/node_modules/testcafe/lib/cli/index.js'
  ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的背包 2025-01-18 07:53:02

您的模块和装置文件位于同一目录中。
因此导入语句可以更改如下。

import { noResults } from 'no-results';

Your module and fixture files resides in the same directory.
Hence import statement can be changes as following.

import { noResults } from 'no-results';

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文