javascript - TypeError:无法读取 null 的属性“SystemError”
问题描述
我在 Typescript 中工作并使用 Jasmine 对其进行单元测试。我有这样的代码结构
- 项目
- 父包
- 儿童套餐
- 大童套餐
- fetchDetails.ts
- 大童套餐
- DataFetchinglError.ts
- 儿童套餐
- 另一个父包
- 错误.ts
- 父包
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 文件,但是在单击它时说文件在运行时不再存在。
fetchDetails - TypeError:无法读取 null 的属性“SystemError”
非规范失败 - 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;
解决方案
推荐阅读
- mql4 - MQL4:如何锁定由指标创建的对象?
- r - 报告字符串分隔向量的所有可能组合
- ios - 如何在 iOS 中使用 Vision Framework 从图像中提取表格数据?
- python - 使用 Inno Setup 自动安装 R
- arrays - MongoDB聚合在根中创建内部数组
- excel - Excel 数据查询 - 忽略丢失的表格/为每个查询分配特定的表格编号
- amazon-web-services - 当 Web GUI 显示更多时,Boto3(和 AWS CLI)仅返回 25 个 API Gateway 自定义域
- xamarin.forms - Xamarin 表单 resx 本地化始终返回默认语言
- android - 创建存储库对象时出现实例化错误“java.lang.InstantiationError:”
- javascript - javascript对象的转换和删除一些字段