Node.js modules

Suppose I have this situation (architecture)

layer1 -> layer2 -> layer3 

- these are just normal node.js modules (there are some exported functions)

Layer1 requires layer2 and calls its functions, and layer2 requires layer3 and calls its functions.

I want to test the functions in layer1, but also mock layer3 (my function call in layer1 extends to layer3 and this one that I want to make fun of).

What is the best way to do this? I looked at this module: https://github.com/thlorenz/proxyquire , but I don’t think it supports mockery when things go 2 or more levels in depth, as I have an example.

Thanks for any suggestions!

+6
source share
2 answers

I used mockery with great success, although it can become really tedious depending on what you want to mock.

However, your setup seems curious. If you want unit test level 1, you only need to break layer 2, and there should be no (direct) connection between layers 1 and 3.

+1
source

Actually, I was wrong with proxyquire. Yes, you can simulate a module 2 or more depths below the source module that you are testing, and it works fine, as they showed in their example. Just put the stub with the path to the module you are mocking at. If you mimic layer3, the stub path must match the path to layer3 written to layer2 (therefore it refers to layer2, not layer1 or some root).

We are doing integration testing, and this is difficult because we use the mongoDB database and there is no built-in database for mongo. There are some attempts and alternatives, but, as I saw, they are not good enough. So there was a root in my problems, we had to make fun of the entire data layer.

Before that, we had a real database on some machines, and integration tests on a CI server (Jenkins) used this real database, but this is not very good, because you cannot, for example, run tests on your laptop.

Thus, prototyping the entire data layer of the application is also a very bad solution, but, as I see it, there is no alternative. If someone had the same or similar situation, feel free to write your solution here.

+1
source

Source: https://habr.com/ru/post/948770/


All Articles