Nestjs "Reference Error: File is not defined" only in test
Nestjs "Reference Error: File is not defined" only in test
我的测试套件无法 运行 因为“文件”输入类型未定义。这仅在尝试 运行 测试时发生。该应用程序在开发中完美运行。下面是我的控制台输出。
$ nx run build-support-cases-api-core:test
> nx run build-support-cases-api-core:test
FAIL build-support-cases-api-core libs/build-support-cases/api/core/src/lib/__test__/buildSupportCase.service.spec.ts
● Test suite failed to run
ReferenceError: File is not defined
4 | export class CreateReceiptInput {
5 | @Field(() => String)
> 6 | public receipt: File;
| ^
7 |
8 | @Field(() => String)
9 | public vendorName: string;
at Object.<anonymous> (../../../core/database/receipt/src/lib/dto/createReceipt.input.ts:6:19)
at Object.<anonymous> (../../../core/database/receipt/src/lib/dto/createReceipt.dto.ts:1:1)
这里是完整的完整源文件
import { Field, ID, InputType } from "@nestjs/graphql";
@InputType()
export class CreateReceiptInput {
@Field(() => String)
public receipt: File;
@Field(() => String)
public vendorName: string;
@Field(() => Date)
public purchaseDate: Date;
@Field(() => Number)
public totalDollarAmount: number;
@Field(() => String)
public purchasedBy: string;
@Field(() => String)
public note: string;
@Field(() => ID)
public updateId?: number;
}
配置文件中是否有我遗漏的内容?我该如何解决这个问题,以便我的测试 运行?
问题是 File 存在于打字稿中,但不存在于 NodeJS(或扩展名 NestJS)中。但是,NestJS 中确实存在 Blob 类型,因此如果您只是将其复制到新文件并导入,则 typescript 定义的文件类型可以工作。
export interface File extends Blob {
readonly lastModified: number;
readonly name: string;
}
import { Field, ID, InputType } from "@nestjs/graphql";
import { File } from "./interfaces";
@InputType()
export class CreateReceiptInput {
@Field(() => String)
public receipt: File;
@Field(() => String)
public vendorName: string;
@Field(() => Date)
public purchaseDate: Date;
@Field(() => Number)
public totalDollarAmount: number;
@Field(() => String)
public purchasedBy: string;
@Field(() => String)
public note: string;
@Field(() => ID)
public updateId?: number;
}
我的测试套件无法 运行 因为“文件”输入类型未定义。这仅在尝试 运行 测试时发生。该应用程序在开发中完美运行。下面是我的控制台输出。
$ nx run build-support-cases-api-core:test
> nx run build-support-cases-api-core:test
FAIL build-support-cases-api-core libs/build-support-cases/api/core/src/lib/__test__/buildSupportCase.service.spec.ts
● Test suite failed to run
ReferenceError: File is not defined
4 | export class CreateReceiptInput {
5 | @Field(() => String)
> 6 | public receipt: File;
| ^
7 |
8 | @Field(() => String)
9 | public vendorName: string;
at Object.<anonymous> (../../../core/database/receipt/src/lib/dto/createReceipt.input.ts:6:19)
at Object.<anonymous> (../../../core/database/receipt/src/lib/dto/createReceipt.dto.ts:1:1)
这里是完整的完整源文件
import { Field, ID, InputType } from "@nestjs/graphql";
@InputType()
export class CreateReceiptInput {
@Field(() => String)
public receipt: File;
@Field(() => String)
public vendorName: string;
@Field(() => Date)
public purchaseDate: Date;
@Field(() => Number)
public totalDollarAmount: number;
@Field(() => String)
public purchasedBy: string;
@Field(() => String)
public note: string;
@Field(() => ID)
public updateId?: number;
}
配置文件中是否有我遗漏的内容?我该如何解决这个问题,以便我的测试 运行?
问题是 File 存在于打字稿中,但不存在于 NodeJS(或扩展名 NestJS)中。但是,NestJS 中确实存在 Blob 类型,因此如果您只是将其复制到新文件并导入,则 typescript 定义的文件类型可以工作。
export interface File extends Blob {
readonly lastModified: number;
readonly name: string;
}
import { Field, ID, InputType } from "@nestjs/graphql";
import { File } from "./interfaces";
@InputType()
export class CreateReceiptInput {
@Field(() => String)
public receipt: File;
@Field(() => String)
public vendorName: string;
@Field(() => Date)
public purchaseDate: Date;
@Field(() => Number)
public totalDollarAmount: number;
@Field(() => String)
public purchasedBy: string;
@Field(() => String)
public note: string;
@Field(() => ID)
public updateId?: number;
}