Node unit test - mocking calls to solr - node.js

For my application i have used solr-node client
How can i write unit tests by mocking the calls to the solr module?
Can someone pls help me out of this?

Try using JsMockito - it's a JavaScript based mocking framework that allows you to mock up objects, function calls, and returns in your project.

Related

How do i mock "chrome.storage" API for testing?

How do I mock "chrome.storage" (local/session) API for the testing in node.js environment? To be more detailed i'd prefer to have any implementation that actually works in node.js, probably some in-memory implementation?
I've found some mock like this, but it's just no-op implementation and i actually need to be able to get a result with according "get" call after a "set" call.
Ended up using mem-storage-area package.

How to create tests for serverless testing using Jest for these scenarios?

I am new to serverless and NodeJS.Could you please guide me how can I create a automated test cases for
lambda to lambda invoke
API Gateway to Lambda Invoke
DynamoDB insertion test
Please help. Thanks in advance.
If you want full end-to-end test of a lambda function, you will have to handle that outside the function itself.
If you use unit testing tools you will be able to run them locally or even inside the function, but you won't have the ability to actually query the function and go through the whole process.
I'd create a second lambda function with any unit test library, like mocha, and write functional tests that invoke the first lambda function, through API gateway, with a simple http-request package (like request).
EDIT:
Here's more clarification on each one of your points:
1) Lambda to lambda invoke
If by lambda-to-lambda you mean you want to call another function WITHOUT using API GW, then I guess you're planning to use the AWS SDK to trigger a function.
If that is the case, it's like any other test. You will create a test function which will get the SDK to trigger the second lambda, and then check the result of the SDK function. It will probably indicate if it's a success or not, or even give you the result.
2) API gateway to lambda invoke
If you are looking to test if the connection between API GW and lambda works, I'd say, why bother? It's a setup-once-and-use kind of deal.. But if you still want to test this, it will be similar to item 1), with the exception that instead of using the SDK, you'd use an API gateway URL.
So you can use an npm package such as axios or request to make a request to such URL and see if the content is the expected.
I'd even say you can run the test in the lambda function and call the very same lambda function, no need to create separate lambdas.
3) Dynamo insertion
This one is the easiest, just create a unit test that writes something into dynamo. Then, in order to know if the test passes or not, just read the DB trying to find what you wrote.
If you're in the fence between testing libraries, I'd suggest going for mocha and chai.
If I can help you answering something more specific, let me know.

An Example of Unit Test for Time Triggered Azure function

I am a newbie in Azure Functions.
I have implemented a time triggered azure function and wish to write unit test cases for it.
I am using specflow and nunit for writing my testcases.
However, I am unable to find a proper example of how to stub time trigger function.
Can someone point me to the correct example?
Thanks.
I wouldn't call it a unit test anymore but you can trigger non-HTTP functions by calling the following admin endpoint of the function app:
POST <ROOT_URL>/admin/functions/<FUNCTION_NAME>
Note that you need to specify the system key in the x-functions-key header when making a request to a deployed function app.
More info in the docs.
Alternative
What I usually try to do is put as much of the business logic in a seperate class which is easily testable and call this class from a function.
Personally, I don't think you should test if the trigger works, that's the responsibility of the Azure Functions Runtime. Fine to test this in a larger scoped integration test but not as a fast and frequently executed unit test.
Get the business logic out of the function itself, and instead have the function call libraries.
Add tests for those libraries.
You don't need to do anything that's specific to azure functions in order to test your code.
If you are attempting to do integration testing, then follow Marc's advice.

How can I make unit test on a nodejs app?

I want to make some unit tests on my nodejs app in order to test the insertion of a user on a database or the read of a user from the database.
I'm using Objection.js such an ORM and I searched through the net how to do these Test, thus I found Sinon to mock a database or a model.Concerning this issue I found also this question Mocking database in node.js?.
For example if I have a table Users contains 10 users, is there any solutions to get all the rows of such table for example of fetch if a user exists in the table? I don't really understand the concept of mocking and if it is the suitable solution to make such tests?
There are lots of libs for unit testing of NodeJS such as Moca, Chai, Jasmine.
here is some tutorial link:
https://www.codementor.io/davidtang/unit-testing-and-tdd-in-node-js-part-1-8t714s877
https://blog.risingstack.com/node-hero-node-js-unit-testing-tutorial/

Testing locomotive.js with Mocha

I am building an app with locomotive.js, and I am looking to build my test suite using the Mocha test framework. I am also new to TDD/BDD in general, so please take that into consideration. I am curious if anyone can point me in a good direction to start testing a locomotive based app.
My biggest question would be:
How do I test a controller's actions?
Can I test an initializer?
Are there best practices around creating test request objects?
Testing controller actions
It depends on what exactly you want to test. Controller actions usually either return content, or pass (perhaps in case of errors) the request along the server stack, so testing them means using something like supertest to check for the correct responses (the supertest page also mentions how to use it together with Mocha)
Can I test an initializer?
Testing an initializer by itself is difficult, because they require to be run within the context of a LocomotiveJS application. However, you could create a test just booting up the application, which during the booting process will also run all initializers. I just added a simple Mocha-based testing framework to my Locomotive + Sequelize boilerplate project which shows how to boot the Locomotive app from within Mocha.
Are there best practices around creating test request objects?
If you mean how you can check responses to requests, look at the aforementioned supertest or perhaps mocha-headless.

Resources