Nestjs "Reference Error: File is not defined" only in test

Viewed 1559

My test suite is failing to run because the "File" input type is undefined. This only happens when trying to run tests. The app works perfectly in development. Below is my console output.

$ 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)

Here is the full source file for completeness

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;
}

Is there something in a config file somewhere that I am missing? How do I get past this issue so that my tests will run?

1 Answers

The issue was that File exists in typescript but not within NodeJS, (or NestJS by extension). However, the Blob type does exist in NestJS so the File type defined by typescript works if you just copy it over to a new file and import it.

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;
}
Related