Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Because of registerEnumType, it cannot be tested in the jest

I'm using typegraphql, and i register DayOfWeek enum type using resgiterEnumType.

following is my enum and registerEnumType function, and they are in one file.

export enum DayOfWeek {
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY
}

registerEnumType(DayOfWeek, {
    name: "DayOfWeekType", // this one is mandatory
});

It works well when i run program. but when i run jest test I got an message like following

Looks like you've forgot to provide experimental metadata API polyfill.
  12 | }
  13 | 
> 14 | registerEnumType(DayOfWeek, {
     | ^
  15 |     name: "DayOfWeekType", // this one is mandatory
  16 | });
  17 | 

If i put enum and registerEnumType in the same file, run works well and test is failed.

and If i separate enum and registerEnumType, graphql not found DayOfWeek type....

how can i solved that?

like image 431
fuzes Avatar asked Apr 06 '20 04:04

fuzes


2 Answers

You need to add

import "reflect-metadata";

at the top of your file

like image 149
kardonv Avatar answered Nov 16 '22 05:11

kardonv


import { Test, TestingModule } from '@nestjs/testing';
import { GraphQLModule } from '@nestjs/graphql';

describe('your test suite', () => {
    beforeEach(async () => {
        const module: TestingModule = await Test.createTestingModule({
            imports: [
                GraphQLModule.forRoot({
                    ... 
                }),
            ],
        }).compile();

    });
});

You need to provision GraphQLModule in your spec.

P/S: the code-snippet above is under assumption that you're on NestJS framework.

like image 1
Roy Lee Avatar answered Nov 16 '22 05:11

Roy Lee