tuan231195 / jest-aws-sdk-mock

MIT License
5 stars 1 forks source link

How to test with toBeCalledTimes/toBeCalledWith #1

Closed JLGouwy closed 4 years ago

JLGouwy commented 4 years ago

Hey,

Just found your repo. I would like to succeed to test something like :

expect(sns.publish().promise).toBeCalledTimes(1);
expect(sns.publish().promise).toBeCalledWith(data);

But I didn't get it how can I achieve that with your mocks. Any idea ?

tuan231195 commented 4 years ago

Hello @JLGouwy , I believe you can just test whether sns.publish has been called with

expect(sns.publish).toBeCalledTimes(1);
expect(sns.publish).toBeCalledWith(data);

sns.publish(...).promise is not a mock function so you cannot check whether it has been called or not

JLGouwy commented 4 years ago

i'm not sure to understand. I just put a basic example on a repo : https://github.com/JLGouwy/aws-sns-mock-test

I try two differents option : 1/ trying to test with basic method of jest (mock/mockimplementation). 2/ with your lib.

In both case, I don't understand how to valid it correctly.

tuan231195 commented 4 years ago

Hello @JLGouwy, sorry for this late reply.

I have put up a fix for your test here: https://github.com/tuan231195/aws-sns-mock-test.

Please note that in order to mock properly, AWSMock.mock statement must be called before you import the mocked AWS modules and the modules under test. That is a limitation of the library.

In my fix, you can see I have put the AWSMock calls on top of the files and change the other import statements to require calls. The reason is import statements are hoisted by default and they will be moved to the top of the file in the compiled output and break the mocks. Changing import calls to require will solve the problem.

Hope it helps.

jlgouwyapizr commented 4 years ago

Thanks a lot :)