【问题标题】:Why do I get a "error TS1005: ';' expected." error when running a simple Jasmine test?为什么我会收到“错误 TS1005:';'预期的。”运行简单的 Jasmine 测试时出错?
【发布时间】:2019-08-30 21:05:24
【问题描述】:

我使用的是 Angular 7。我写了一个真正简单的测试类,src/app/pastebin.ts,

export class Pastebin {
    id: number;
    title: string;
    language: string;
    paste: string;

    constructor(values: Object = {}) {
        Object.assign(this, values);
  }
}

export const Languages = ["Ruby","Java", "JavaScript", "C", "Cpp"];

然后是一个真正简单的 Jasmine 规范,src/app/pastebin.spec.ts,来测试它,

/* pastebin.spec.ts */

//import the Pastebin class
import { Pastebin } from './pastebin';

describe('Pastebin', () => {
    it('should create an instance of Pastebin',() => {
        expect(new Pastebin()).toBeTruthy();
    });

})

但是当我去运行我的测试时,我得到了一些看起来与我所做的事情相关的错误......

localhost:Pastebin davea$ ng test
 10% building modules 1/1 modules 0 active30 08 2019 15:53:13.954:WARN [karma]: No captured browser, open http://localhost:9876/
30 08 2019 15:53:13.960:INFO [karma]: Karma v2.0.5 server started at http://0.0.0.0:9876/
30 08 2019 15:53:13.960:INFO [launcher]: Launching browsers Chrome, ChromeNoSandboxHeadless with unlimited concurrency
30 08 2019 15:53:13.964:INFO [launcher]: Starting browser Chrome
30 08 2019 15:53:13.974:INFO [launcher]: Starting browser Chrome
[./src/test.ts] ./src/test.ts 0 bytes {main} [built]                               
   [0] multi ./src/polyfills.ts 28 bytes {polyfills} [built]
[./src/polyfills.ts] ./src/polyfills.ts 0 bytes {polyfills} [built]

ERROR in node_modules/@types/jasmine/index.d.ts(142,47): error TS1005: ';' expected.
node_modules/@types/jasmine/index.d.ts(142,90): error TS1005: '(' expected.
node_modules/@types/jasmine/index.d.ts(142,104): error TS1005: ']' expected.
node_modules/@types/jasmine/index.d.ts(142,112): error TS1005: ',' expected.
node_modules/@types/jasmine/index.d.ts(142,113): error TS1136: Property assignment expected.
node_modules/@types/jasmine/index.d.ts(142,121): error TS1005: ')' expected.
node_modules/@types/jasmine/index.d.ts(142,147): error TS1005: '(' expected.
node_modules/@types/jasmine/index.d.ts(142,162): error TS1005: ']' expected.
node_modules/@types/jasmine/index.d.ts(142,163): error TS1005: ',' expected.
node_modules/@types/jasmine/index.d.ts(142,164): error TS1136: Property assignment expected.
node_modules/@types/jasmine/index.d.ts(142,165): error TS1136: Property assignment expected.
node_modules/@types/jasmine/index.d.ts(142,179): error TS1005: ',' expected.
node_modules/@types/jasmine/index.d.ts(142,183): error TS1005: ':' expected.
node_modules/@types/jasmine/index.d.ts(142,208): error TS1005: '{' expected.
node_modules/@types/jasmine/index.d.ts(142,217): error TS1005: ':' expected.
node_modules/@types/jasmine/index.d.ts(142,222): error TS1005: ',' expected.
node_modules/@types/jasmine/index.d.ts(142,227): error TS1005: ':' expected.
node_modules/@types/jasmine/index.d.ts(142,228): error TS1109: Expression expected.
node_modules/@types/jasmine/index.d.ts(142,230): error TS1005: ')' expected.
node_modules/@types/jasmine/index.d.ts(635,30): error TS1005: ';' expected.
node_modules/@types/jasmine/index.d.ts(720,1): error TS1128: Declaration or statement expected.

有人知道我该如何解决这个问题吗?

编辑:响应给出的答案,这是新的输出...

localhost:Pastebin davea$ ng test
 10% building modules 1/1 modules 0 active01 09 2019 11:34:18.591:WARN [karma]: No captured browser, open http://localhost:9876/
01 09 2019 11:34:18.616:INFO [karma-server]: Karma v4.3.0 server started at http://0.0.0.0:9876/
01 09 2019 11:34:18.617:INFO [launcher]: Launching browsers Chrome, ChromeNoSandboxHeadless with concurrency unlimited
01 09 2019 11:34:18.622:INFO [launcher]: Starting browser Chrome
01 09 2019 11:34:18.797:INFO [launcher]: Starting browser Chrome
[./src/test.ts] ./src/test.ts 0 bytes {main} [built]                               
   [0] multi ./src/polyfills.ts 28 bytes {polyfills} [built]
[./src/polyfills.ts] ./src/polyfills.ts 0 bytes {polyfills} [built]

ERROR in node_modules/@types/jasmine/index.d.ts(191,11): error TS1005: ';' expected.
node_modules/@types/jasmine/index.d.ts(192,36): error TS1005: '(' expected.
node_modules/@types/jasmine/index.d.ts(192,50): error TS1005: ']' expected.
node_modules/@types/jasmine/index.d.ts(192,58): error TS1005: ',' expected.
node_modules/@types/jasmine/index.d.ts(192,59): error TS1136: Property assignment expected.
node_modules/@types/jasmine/index.d.ts(192,67): error TS1005: ')' expected.
node_modules/@types/jasmine/index.d.ts(193,37): error TS1005: '(' expected.
node_modules/@types/jasmine/index.d.ts(193,52): error TS1005: ']' expected.
node_modules/@types/jasmine/index.d.ts(193,53): error TS1005: ',' expected.
node_modules/@types/jasmine/index.d.ts(193,54): error TS1136: Property assignment expected.
node_modules/@types/jasmine/index.d.ts(193,55): error TS1136: Property assignment expected.
node_modules/@types/jasmine/index.d.ts(193,69): error TS1005: ',' expected.
node_modules/@types/jasmine/index.d.ts(193,73): error TS1005: ':' expected.
node_modules/@types/jasmine/index.d.ts(193,98): error TS1005: '{' expected.
node_modules/@types/jasmine/index.d.ts(193,107): error TS1005: ':' expected.
node_modules/@types/jasmine/index.d.ts(193,112): error TS1005: ',' expected.
node_modules/@types/jasmine/index.d.ts(193,117): error TS1005: ':' expected.
node_modules/@types/jasmine/index.d.ts(193,118): error TS1109: Expression expected.
node_modules/@types/jasmine/index.d.ts(193,120): error TS1005: ')' expected.
node_modules/@types/jasmine/index.d.ts(748,30): error TS1005: ';' expected.
node_modules/@types/jasmine/index.d.ts(854,1): error TS1128: Declaration or statement expected.

编辑 2:应用答案中的建议后,我得到了新错误

davea$ ng test
 10% building modules 1/1 modules 0 active02 09 2019 10:20:52.750:WARN [karma]: No captured browser, open http://localhost:9876/
02 09 2019 10:20:52.776:INFO [karma-server]: Karma v4.3.0 server started at http://0.0.0.0:9876/
02 09 2019 10:20:52.776:INFO [launcher]: Launching browsers Chrome, ChromeNoSandboxHeadless with concurrency unlimited
02 09 2019 10:20:52.780:INFO [launcher]: Starting browser Chrome
02 09 2019 10:20:52.787:INFO [launcher]: Starting browser Chrome
02 09 2019 10:20:56.030:WARN [karma]: No captured browser, open http://localhost:9876/  
02 09 2019 10:20:56.216:INFO [HeadlessChrome 76.0.3809 (Mac OS X 10.13.6)]: Connected on socket lcRdfhRmFhHi04mTAAAA with id 64655559
02 09 2019 10:20:56.232:INFO [Chrome 76.0.3809 (Mac OS X 10.13.6)]: Connected on socket IL97l6a-00J6s9H9AAAB with id 6205557
Browser {
  id: '6205557',
  fullName: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) ' +
    'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 ' +
    'Safari/537.36',
  name: 'Chrome 76.0.3809 (Mac OS X 10.13.6)',
  lastResult: [BrowserResult],
  disconnectsCount: 0,
  activeSockets: [Array],
  noActivityTimeout: 30000,
  collection: [BrowserCollection],
  emitter: [Server],
  socket: [Socket],
  timer: [Object],
  disconnectDelay: 2000,
  log: [Logger],
  noActivityTimeoutId: Timeout {
    _idleTimeout: 30000,
    _idlePrev: [Timeout],
    _idleNext: [Timeout],
    _idleStart: 10322,
    _onTimeout: [Function],
    _timerArgs: undefined,
    _repeat: null,
    _destroyed: false,
    [Symbol(refed)]: true,
    [Symbol(asyncId)]: 19087,
    [Symbol(triggerId)]: 19085
  },
  pendingDisconnect: null,
  state: 'EXECUTING'
} ERROR
  An error was thrown in afterAll
  TypeError: env.catchExceptions is not a function
      at node_modules/karma-jasmine-html-reporter/src/lib/adapter.js:24:9
      at node_modules/karma-jasmine-html-reporter/src/lib/adapter.js:58:3
Chrome 76.0.3809 (Mac OS X 10.13.6): Executed 4 of 4 ERROR (0 secs / 0.154 secs)
HeadlessChrome 76.0.3809 (Mac OS X 10.13.6): Executed 3 of 4 SUCCESS (0 secs / 0.132 secs)
Browser {
  id: '6205557',
  fullName: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) ' +
    'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 ' +
    'Safari/537.36',
  name: 'Chrome 76.0.3809 (Mac OS X 10.13.6)',
  lastResult: [BrowserResult],
  disconnectsCount: 0,
  activeSockets: [Array],
  noActivityTimeout: 30000,
  collection: [BrowserCollection],
  emitter: [Server],
  socket: [Socket],
  timer: [Object],
  disconnectDelay: 2000,
  log: [Logger],
  noActivityTimeoutId: Timeout {
    _idleTimeout: 30000,
    _idlePrev: [Timeout],
    _idleNext: [Timeout],
    _idleStart: 10322,
    _onTimeout: [Function],
    _timerArgs: undefined,
    _repeat: null,
    _destroyed: false,
    [Symbol(refed)]: true,
    [Symbol(asyncId)]: 19087,
    [Symbol(triggerId)]: 19085
  },
  pendingDisconnect: null,
  state: 'EXECUTING'
} ERROR
  An error was thrown in afterAll
  TypeError: env.catchExceptions is not a function

编辑 3 这是 package.json ...

localhost:Pastebin davea$ cat package.json 
{
  "name": "pastebin",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.2.0",
    "@angular/common": "^5.2.0",
    "@angular/compiler": "^5.2.0",
    "@angular/core": "^5.2.0",
    "@angular/forms": "^5.2.0",
    "@angular/http": "^5.2.0",
    "@angular/platform-browser": "^5.2.0",
    "@angular/platform-browser-dynamic": "^5.2.0",
    "@angular/router": "^5.2.0",
    "core-js": "^2.4.1",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.19"
  },
  "devDependencies": {
    "@angular/cli": "~1.7.4",
    "@angular/compiler-cli": "^5.2.0",
    "@angular/language-service": "^5.2.0",
    "@types/jasmine": "^2.8.8",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "^2.99.1",
    "jasmine-spec-reporter": "^3.2.0",
    "karma": "^4.3.0",
    "karma-chrome-launcher": "^3.1.0",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "^2.0.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~4.1.0",
    "tslint": "~5.9.1",
    "typescript": "^2.7.2"
  }
}

这是我在运行测试时遇到的错误...

localhost:Pastebin davea$ ng test
 10% building modules 1/1 modules 0 active07 09 2019 12:27:20.412:WARN [karma]: No captured browser, open http://localhost:9876/
07 09 2019 12:27:20.429:INFO [karma-server]: Karma v4.3.0 server started at http://0.0.0.0:9876/
07 09 2019 12:27:20.429:INFO [launcher]: Launching browsers Chrome, ChromeNoSandboxHeadless with concurrency unlimited
07 09 2019 12:27:20.433:INFO [launcher]: Starting browser Chrome
07 09 2019 12:27:20.441:INFO [launcher]: Starting browser Chrome
07 09 2019 12:27:23.398:WARN [karma]: No captured browser, open http://localhost:9876/  
07 09 2019 12:27:23.600:INFO [HeadlessChrome 76.0.3809 (Mac OS X 10.13.6)]: Connected on socket BQzsVF64PPIY9jUsAAAA with id 32854347
07 09 2019 12:27:23.624:INFO [Chrome 76.0.3809 (Mac OS X 10.13.6)]: Connected on socket edznAyypyhtvngwrAAAB with id 87775700
HeadlessChrome 76.0.3809 (Mac OS X 10.13.6) ERROR
  An error was thrown in afterAll
  TypeError: env.catchExceptions is not a function
      at node_modules/karma-jasmine-html-reporter/src/lib/adapter.js:24:9
      at node_modules/karma-jasmine-html-reporter/src/lib/adapter.js:58:3
HeadlessChrome 76.0.3809 (Mac OS X 10.13.6): Executed 4 of 4 ERROR (0 secs / 0.144 secs)
Chrome 76.0.3809 (Mac OS X 10.13.6): Executed 2 of 4 SUCCESS (0 secs / 0.117 secs)
HeadlessChrome 76.0.3809 (Mac OS X 10.13.6) ERROR
  An error was thrown in afterAll
  TypeError: env.catchExceptions is not a function
Chrome 76.0.3809 (Mac OS X 10.13.6) ERROR
  An error was thrown in afterAll
  TypeError: env.catchExceptions is not a function
      at node_modules/karma-jasmine-html-reporter/src/lib/adapter.js:24:9
      at node_modules/karma-jasmine-html-reporter/src/lib/adapter.js:58:3
HeadlessChrome 76.0.3809 (Mac OS X 10.13.6): Executed 4 of 4 ERROR (0.146 secs / 0.144 secs)
Chrome 76.0.3809 (Mac OS X 10.13.6): Executed 4 of 4 ERROR (0 secs / 0.142 secs)
Chrome 76.0.3809 (Mac OS X 10.13.6) ERROR
  An error was thrown in afterAll
  TypeError: env.catchExceptions is not a function
HeadlessChrome 76.0.3809 (Mac OS X 10.13.6): Executed 4 of 4 ERROR (0.146 secs / 0.144 secs)
Chrome 76.0.3809 (Mac OS X 10.13.6): Executed 4 of 4 ERROR (0.157 secs / 0.142 secs)

【问题讨论】:

  • 与您的问题无关,但您应该阅读typescriptlang.org/docs/handbook/declaration-files/…
  • 我尝试在我的 Angular 项目上运行您的代码,它运行良好。问题似乎是别的。你能先xdescribe 然后fdescribe 让我们知道输出有什么不同吗
  • 也许您的节点模块安装损坏。您可以尝试重新安装它们吗?
  • @ShashankVivek,我尝试运行“xdescribe”,但收到错误“-bash: xdescribe: command not found”。
  • @lealceldeiro,您建议的重新安装命令是否与答案中给出的不同?这些是我刚刚运行并在问题编辑中发布的错误。

标签: angular jasmine karma-jasmine angular7


【解决方案1】:

在运行 typescript @ 2.7.2 和 @types/jasmine 大于 2.8.8 或 jasmine >= 3.0 时,我看到了与此非常相似的问题。

对我来说已知的安全工作版本是

"dependencies": {
    "@angular/animations": "7.2.0",
    "@angular/common": "7.2.0",
    "@angular/compiler": "7.2.0",
    "@angular/core": "7.2.0",
    "@angular/forms": "7.2.0",
    "@angular/platform-browser": "7.2.0",
    "@angular/platform-browser-dynamic": "7.2.0",
    "@angular/router": "7.2.0",
    "core-js": "2.5.4",
    "rxjs": "6.3.3",
    "tslib": "1.9.0",
    "zone.js": "0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "0.13.0",
    "@angular/cli": "7.3.8",
    "@angular/compiler-cli": "7.2.0",
    "@angular/language-service": "7.2.0",
    "@types/node": "8.9.4",
    "@types/jasmine": "2.8.8",
    "@types/jasminewd2": "2.0.3",
    "codelyzer": "5.1.0",
    "jasmine-core": "2.99.1",
    "jasmine-spec-reporter": "4.2.1",
    "karma": "4.1.0",
    "karma-chrome-launcher": "2.2.0",
    "karma-coverage-istanbul-reporter": "2.0.5",
    "karma-jasmine": "2.0.1",
    "karma-jasmine-html-reporter": "1.4.2",
    "protractor": "5.4.2",
    "ts-node": "7.0.0",
    "tslint": "5.11.0",
    "typescript": "3.2.2"
  }

如果不是,请发布您的 package.json。一些更新的库需要 typescript 3.0,如果没有,则必须指向旧版本。

我还看到https://github.com/dfederm/karma-jasmine-html-reporter 说 对于 Jasmine

"karma-jasmine-html-reporter":"0.2.2"

【讨论】:

  • 嗨,我将版本更新为您建议的版本(在我的问题的 edit-3 中包含我的 package.json)。我还包括了我得到的错误——“TypeError: env.catchExceptions is not a function”
  • 嗯,这很有趣,因为 package.json 表明当你说你在 Angular 7 上时你正在使用 Angular 5。我正在更新我的答案以获得我的 Angular 7 的完整依赖项列表我知道的应用程序有效。它也有更高版本的打字稿,我认为这将解决您所看到的问题。
  • 另外请注意,我不使用~^,因为这些依赖关系可能很奇怪。我喜欢精确的版本以保持一致性。
  • 谢谢。我用你所拥有的替换了我的 package.json 的“依赖项”和“devDependencies”。在运行“ng test”之前,我需要做些什么来安装它们吗?我问是因为在我替换了 package.json 中的那些并运行“ng test”之后,我仍然得到这些“HeadlessChrome 76.0.3809 (Mac OS X 10.13.6) 错误,毕竟抛出了一个错误,TypeError: env.catchExceptions 不是一个函数”错误。
  • 哦,是的,设置完这些后,只需运行 npm inpm install。有时需要先删除 package-lock.json
【解决方案2】:

您似乎正在运行 Karma v2.0.5,它是 Karma 的一个非常过时的版本。

30 08 2019 15:53:13.960:INFO [karma]: Karma v2.0.5 server started at http://0.0.0.0:9876/

如果我没记错的话,我之前遇到过这个错误,因为我正在运行一个过时的 Karma 和更新的打字稿,还是相反。

无论如何,最新的 Karma 测试运行器是 V4.3.0

您可以通过执行以下命令来升级您的 Karma 和 Jasmine:


npm install karma@latest --save-dev
npm install karma-jasmine@latest karma-chrome-launcher@latest jasmine-core@latest --save-dev
npm install @types/jasmine@latest --save-dev

如果您安装了额外的 karma 插件,您可能也想更新它们。看看你的 package.json。例如, 你可能有 karma-firefox-launcher 而不是 chrome 的版本。


我在浏览器历史上做了一个简单的搜索,发现了这个 github 问题:https://github.com/DefinitelyTyped/DefinitelyTyped/issues/30310

这个问题突出了他们如何通过将 typescript 降级/升级到支持 jasmine/karma 版本来解决问题。

通过反复试验和一些研究,您很可能会在 Angular 7 中找到相互兼容的 karma、jasmine 和 typescript 版本的合适组合。

但是,我建议您只需将 @angular/cli 从 Angular 7 升级到 Angular 8 并更新所有 karma 和 jasmine 包。

Angular 8 包含许多重大更改,导致许多 Angular 3rd 方库更新其所有库。因此,如果您将来要执行 npm i some-lib,它可能不适用于您的 Angular 7 项目。而且你必须找到最后一个与 Angular 7 配合得很好的工作版本。升级到 Angular 8 将为你省去很多“降级”的麻烦。最新的 karma 和 jasmine 包可与 Angular 8 一起使用,您只需将所有内容升级到最新的工作版本就更容易了。

Official Angular Update Guide

【讨论】:

  • 嗨@Riv,我运行了命令并将输出添加为我的问题的编辑。现在你可以看到我有 karma 4.3.0 但仍然出现上述错误。
  • 我已经用与我在搜索历史中找到的错误代码相关的 github 问题编辑了我的答案。我希望它能让你朝着正确的方向前进......
  • 谢谢。解决方案有点麻烦,链接中推荐“npm install typescript/2.8.4 --save-dev”(获取主机'github.com(192.30.253.113)'的真实性无法建立.0d71 ) 但这是一个很好的起点。
  • 请注意,github 问题已经有将近一年的历史了。 Angular 7(您提到的是您在问题中使用的版本)支持 typescript 3.1,我不确定 typescript 2.8.4。这就是为什么我建议您将所有内容都升级到最新版本 =)
  • 嘿@Dave,看起来你已经开始了赏金=0...你更新到Angular 8,但它仍然没有为你解决吗?另外,看看this,如果你在jasmine包之前删除前面的符号可能会有所帮助?
【解决方案3】:

看起来你不像在 beforeEach 周围包裹你的测试来设置 Angular 测试模块。你可以试试下面的代码:

    describe('MyComponent', () => {
      beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [ 
            // All the components referenced in the test
          ],    
          providers: [
            // All injected services
          ]
        })
        .compileComponents();
      }));


    it('should create an instance of Pastebin',() => {
      expect(new Pastebin()).toBeTruthy();
    });

  })

【讨论】:

  • 嗨@Paul,我在运行您的代码时遇到两个错误。它们是“src/app/pastebin.spec.ts(7,14) 中的错误:错误 TS2304:找不到名称‘异步’。src/app/pastebin.spec.ts(8,5):错误 TS2304:找不到名称'TestBed'"
  • 你可以从@angular/core/testing 导入这个。希望这会有所帮助!
  • 解决了 TestBed 错误,但是异步错误呢?仍然得到错误,“错误 TS2304:找不到名称'async'。”
  • 你是否也从 @angular/core/testing 导入了异步?
  • 解决了编译错误,但是现在运行测试本身时出现的错误是,“HeadlessChrome 76.0.3809 (Mac OS X 10.13.6) ERROR, An error was throw in afterAll, TypeError : env.catchExceptions 不是函数”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-08
  • 1970-01-01
  • 2017-10-20
  • 2012-05-31
  • 2021-11-16
  • 1970-01-01
  • 2018-05-14
相关资源
最近更新 更多