Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jest tests - Please add 'import "reflect-metadata"' to the top of your entry point

I'm developing an application using dependency injection with tsyringe. That's the example of a service that receives the repository as a dependency:

import { injectable, inject } from 'tsyringe'

import IAuthorsRepository from '@domains/authors/interfaces/IAuthorsRepository'

@injectable()
export default class ListAuthorsService {
  constructor (
    @inject('AuthorsRepository')
    private authorsRepository: IAuthorsRepository
  ) {}

And the dependencies container:

import { container } from 'tsyringe'

import IAuthorsRepository from '@domains/authors/interfaces/IAuthorsRepository'
import AuthorsRepository from '@domains/authors/infra/typeorm/repositories/AuthorsRepository'

container.registerSingleton<IAuthorsRepository>(
  'AuthorsRepository',
  AuthorsRepository
)

export default container

In the tests, I don't want to use the dependencies registered on the container, but instead, to pass a mock instance via parameter.

let authorsRepository: AuthorsRepositoryMock
let listAuthorsService: ListAuthorsService

describe('List Authors', () => {
  beforeEach(() => {
    authorsRepository = new AuthorsRepositoryMock()
    listAuthorsService = new ListAuthorsService(authorsRepository)
  })

But I'm receiving the following error:

tsyringe requires a reflect polyfill. Please add 'import "reflect-metadata"' to the top of your entry point.

What I thought was - "I may need to import the reflect-metadata package before executing the tests". So I created a jest.setup.ts which imports the reflect-metadata package. But another error occurs:

Error

The instance of the repository is somehow undefined.

I would like to run my tests in peace.

like image 500
Laura Beatris Avatar asked Dec 10 '20 10:12

Laura Beatris


People also ask

What is reflect-metadata in TypeORM?

With the reflect-metadata package you can do runtime reflection on types. Since TypeORM mostly works with decorators (like @Entity or @Column), this package is used to parse these decorators and use it for building sql queries.

What is use of reflect-metadata?

reflect-metadata Allows you to do runtime reflection on types. The native (non reflect-metadata) version of type inference is much poorer than reflect-metadata and consists only of typeof and instanceof .

Do I need to import Jest?

In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or import anything to use them.

Can you test TypeScript with Jest?

Jest is a simple, lightweight testing framework that provides a variety of testing capabilities to JavaScript and TypeScript projects. It provides functionality like assertions, mocking, spies, running tests in parallel, prioritizing failed tests, and coverage analysis.


1 Answers

First create in root of your project an jest.setup.ts.

In your jest.config.js, search for this line:

// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],

uncomment, and add your jest.setup.ts file path.

// A list of paths to modules that run some code to configure or set up the testing framework before each test
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],

Now import the reflect-metadata in jest.setup.ts:

import 'reflect-metadata';

And run tests again.

like image 112
Ebert Mota Avatar answered Oct 21 '22 03:10

Ebert Mota