【问题标题】:Typescript not compiling external class - protractor test打字稿不编译外部类 - 量角器测试
【发布时间】:2023-03-23 14:56:02
【问题描述】:

Typescript 未编译外部类 - 量角器测试。

我正在尝试将我作为 npm 包发布的外部 typescript 类用于我的量角器测试,该测试也在 typescript 中。我用 gulp 编译打字稿。当我在与测试相同的项目中拥有该类时,一切工作正常,但是现在我尝试通过 node_modules 作为包使用它,但出现错误。

在运行时,我得到Unexpected token error。 看起来打字稿没有编译。是打字错误吗?我需要为我的包生成类型吗?


import { Helper } from './src/helper';
       ^
SyntaxError: Unexpected token {

打包文件系统

  • e2e/src/helper

  • index.ts

  • package.json

  • tsconfig.json


index.ts

import { Helper } from './src/helper';
export { Helper };

package.json

  "name": "my-project",
  "main": "index.ts",
  "types": "index.d.ts",
  "version": "0.0.1",

tsconfig.json

{
    "compilerOptions": {
        "outDir": "lib",
        "rootDir": ".",
        "target": "es5",
        "module": "commonjs",
        "declaration": true,
        "types": [
        ]
    }
}

测试

Protractor 测试文件,我在其中尝试使用外部类,已导入node_modules

test.ts

import { Helper } from 'my-project';

describe(' Test Description' , () => {

    let helper: Helper;

before(async () => {
    helper = new Helper();
});

running this test gives me error - SyntaxError: Unexpected token {

【问题讨论】:

标签: typescript protractor typescript-typings


【解决方案1】:
import { Helper } from 'my-project';

describe(' Test Description' , () => {

    let helper: Helper;

    before(async () => {
        helper = new Helper();
    });
}); //- сlosing tag

【讨论】:

  • 嗨!虽然这可能会为 OP 的问题提供解决方案,但 StackOverflow 不鼓励仅使用代码回答。对于 OP 来说,了解为什么这是一个解决方案很重要,因为从长远来看,它将对他们有更多帮助,并且对网站的未来访问者更有利。谢谢!
猜你喜欢
  • 2020-05-17
  • 2019-08-28
  • 2012-11-09
  • 1970-01-01
  • 2021-04-06
  • 2013-08-19
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
相关资源
最近更新 更多