angular jest test fails when using js object from shared library

Discussão em 'Angular' iniciado por J King, Dezembro 31, 2024.

  1. J King

    J King Guest

    I have an angular 18 app in a NX workspace and I am trying to run a test in isolation. But I am getting an error:

    FAIL shared-ui libs/web/shared/ui/src/lib/shared-ui/form-controls/exercise-selector-input/data-access/exercise-input.service.spec.ts
    ● Test suite failed to run
    ReferenceError: fetch is not defined
    .....
    at Object.<anonymous> (src/lib/shared-ui/form-controls/exercise-selector-input/data-access/exercise-input.service.spec.ts:4:1)
    Test Suites: 1 failed, 1 total
    Tests: 0 total


    The line referenced is the import line FormatOptionData

    I am using the following command to run jus the single file in question:

    nx run shared-ui:test --testFile=exercise-input.service.spec.ts


    Here is my test file simplified:

    import { TestBed } from '@angular/core/testing';

    import { ExerciseInputService } from './exercise-input.service';
    import { Format, FormatOptionData } from '@t3a/shared-data-access';

    describe('ExerciseRecordService', () => {
    let service: ExerciseInputService;
    const format: Format[] = FormatOptionData;

    beforeEach(() => {
    TestBed.configureTestingModule({});
    service = TestBed.inject(ExerciseInputService);
    });

    // tests go here

    });


    I know the error occurs on the FormatDataOptions assignemnt because if that is the ONLY thing I remove then there is no error.

    FormatDataOptions is a js object that is imported from another shared lib inside the NX workspace. It looks like this:

    export const FormatOptionData: Format[] = [
    {
    dataType: DataTypeListEnum.Enum.format,
    name: "Sets x Reps",
    code: "NSETS_XREPS",
    description: "A number of sets of reps",
    useValue: true,
    valueType: FormatValueTypeEnum.enum.set_reps,
    valueLabel: "Sets",
    prefixValue: false,
    public: true,
    },
    {
    dataType: DataTypeListEnum.Enum.format,
    name: "Rep Max",
    code: "NRM",
    description: "Maximum weight for a specific number of reps",
    useValue: true,
    valueType: FormatValueTypeEnum.enum.number,
    valueLabel: "Reps",
    prefixValue: true,
    public: true,
    },
    ]


    I don't want to mock this because I need to do this with multiple objects like the one above and I don't want to maintain the data in two places

    Can someone help me import this JavaScript object into my angular test

    thanks

    Continue reading...

Compartilhe esta Página