Search code examples
angularts-jest

How to properly export/import enums and interfaces files so that Jest successfully completes tests?


I'm struggling with Jest tests that don't want to accept the import of a file that only exports the enums and interface themselves, for example:

export enum Test {
    TEST = 'test'
}

The problem is that Jest does not detect it as a module. I understand this error, however, I also know that it is possible to make such an import and for Jest tests to pass. My tests look like this:

import { MockBuilder, MockRender } from 'ng-mocks';
import { ServicesModule } from '../../services.module';
import { ImageComponent } from './image.component';

describe('ImageComponent ', () => {
  beforeEach(() => {
    return MockBuilder(ImageComponent, ServicesModule)
  });

  it('should create component', () => {
    const fixture = MockRender(ImageComponent);
    const component = fixture.point.componentInstance;
    expect(component).toBeTruthy();
  });
});

How to properly export/import enums and interfaces files so that Jest successfully completes tests?

I tried inserting the files into the modules and then exporting them via index.ts using export * from 'path_to_my_files/enums', but that doesn't work.


Solution

  • You need to update your tsconfig.spec.json to use it accordingly in your test cases. Find compilerOptions under paths in tsconfig.spec.json and specify:

    "@shared/*": ["path-to-your-enums/*"] 
    

    and then import in a relevant test like below:

    import { Test } from '@shared/path-to-your-enums/Test';