首页 > 解决方案 > TypeError:无法读取 null 的属性“SystemError”

问题描述

我在 Typescript 中工作并使用 Jasmine 对其进行单元测试。我有这样的代码结构

fetchDetails.ts

import {ErrorType, ErrorameN, SystemError} from '../../AnotherParentPackage/system_error';

import {DataFetchinglError} from '../data_fetching_error';

  it('throws error if file not found', async () => {
    .
    .
    .
    await expectAsync(result).toBeRejectedWith(new DataFetchinglError(
        ErrorName.NO_SUCH_FILE_OR_DIRECTORY,
        ErrorType.GENERIC,
        `No file found at '${Location}'.`,
        ));
  });

DataFetchinglError.ts


import {ErrorType, SystemError} from '../../AnotherParentPackage/system_error';

export class DataFetchinglError extends SystemError {
  constructor(
      readonly name: number, readonly type: ErrorType,
      message: string) {
    // This call breaks the prototype chain, ...
    super(name, type, message);
    // ... so it has to be restored.
    // This requires ES6 transpilation, otherwise the compilation will fail.
    Object.setPrototypeOf(this, new.target.prototype);
  }

  toString() {
    return `${this.type}:${this.name} (${
        this.message || 'Undefined error.'})`;
  }
}

错误类

export class ErrorType {
    .
    .
    ,
    constructor() {}
}

export const enum ErrorName{
    NONE,
    GENERIC
}

export class SystemError extends Error {
 constructor(
     readonly name: number, readonly type: ErrorType,
     message: string) {
    // This call breaks the prototype chain, ...
   super(message);
    // ... so it has to be restored.
    // This requires ES6 transpilation, otherwise the compilation will fail.
    Object.setPrototypeOf(this, new.target.prototype);
  }

在运行这个测试用例时,我收到了这些错误。它指向生成的 .js 文件,但是在单击它时说文件在运行时不再存在。

  1. fetchDetails - TypeError:无法读取 null 的属性“SystemError”

  2. 非规范失败 - afterAll TypeError 中引发错误:无法读取 null 的属性“SystemError”

我也在日志中收到错误,但在某处读到它可以被忽略

错误'goog.require 找不到:Anotherparenr.DataFetchinglError

错误接口

interface Error {
    name: string;
    message: string;
    stack?: string;
}

interface ErrorConstructor {
    new(message?: string): Error;
    (message?: string): Error;
    readonly prototype: Error;
}

declare var Error: ErrorConstructor;

标签: javascriptjquerytypescriptjasminetypeerror

解决方案


推荐阅读