Cannot spyOn on a primitive value; undefined given
Asked Answered
D

1

6

I am trying to write the unit test case using jest and nest but getting the below error: In the test case, I am trying to call create credentials method using spyon function but the spyon is itself giving me an error.

 DeviceSecretService › should call createCredentials method with expected parms

    Cannot spyOn on a primitive value; undefined given

      23 |   // });
      24 |   it('should call createCredentials method with expected parms', async () => {
    > 25 |     const createCredentialsSpy = jest.spyOn(service, 'createCredentials');
         |                                       ^
      26 |     const deviceId = 'deviceId';
      27 |     const dci = new DeviceCommunicationInterface();
      at Object.<anonymous> (device/services/device-secret/device-secret.service.spec.ts:25:39)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        20.61 s, estimated 76 s
Ran all test suites matching /device-secret.service.spec.ts/i.
npm ERR! Test failed.  See above for more details.

Below is the code for the spec.ts file


  it('should call createCredentials method with expected parms', async () => {
    const createCredentialsSpy = jest.spyOn(service, 'createCredentials');
    const deviceId = 'deviceId';
    const dci = new DeviceCommunicationInterface();
    service.createCredentials(deviceId,dci);
    expect(createCredentialsSpy).toHaveBeenCalledWith(deviceId,dci);
  });
});

I tried everything please give some suggestion

Danny answered 3/9, 2021 at 20:58 Comment(4)
That error means that service is undefined. Are you sure that module.get<DeviceSecretService>(DeviceSecretService); returns a value?Bog
Actually, I am not sure if it is returning a value or not. I have seen some videos where they have used spyon function to check if the method is getting called or not.Danny
Even if your code were working, it's not testing correctly. Your test code calls the service method and then expects it to have been called. It will always pass, because you just called it. Instead you need to test the side effects and results of the method, like checking that it calls secretManager.createSecret, and —wait, where does your function return anything, outside of the catch?Thinnish
Yes, My function is returning outside of the catch block. I have updated the .ts fileDanny
K
2

You're not providing a mock value for SecretManagerServiceClient, so Nest won't be able to create the DeviceSecretService, as a result of which you pass undefined to the jest.spyOn method.

To fix this,you need to provide some sort of custom provider as a mock for the injected service. Possibly something like

{
  provide: SecretManagerServiceClient,
  useValue: {
    getProjectId: jest.fn(),
    createSecret: jest.fn(),
  }
}

You'll obviously want to provide better definitions, but this should be a starting point to keep going.

Kanal answered 3/9, 2021 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.