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

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.

Related

Which function in "nestjs/swagger" converts DTOs to Swagger model definitions?

I have DTOs specified with Class-Validator and I am looking for a library that can be used to generate Swagger specification from it. I am not using it for a REST API, the code is addressing an IoT/MQTT scenario - I simply use Class-Validator to manage JSON.
NestJS/Swagger is the best maintained library. I would like to use it's capability to produce Swagger definitions without a NestJS Server. Ideally I would like to pass in a DTO definition and get it's Swagger schema.
I have been reading the source, but am struggling to understand which function in the framework actually does that. At best, I have been able to track it down to modelsDefinitions property in swagger-explorer class.
As best I can tell, from there, api-parameters.explorer and api-produces.explorer. The way they work is not clear to me. I was wondering of someone might help me out?
I'd like to add that I am aware of class-validator-jsonschema, but it is not maintained and no longer seems to work properly.
nestjs/swagger does not expose what you need as its public API which you cannot access it. The class you're looking for is SchemaObjectFactory and the method is exploreModelSchema.
Reference:
SwaggerObjectFactory
Test

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.

Node.js or Express.js REST API document generator

I'm working on a restAPI using Express.js and I'm wondering if there is a way for me to generate API documents that allow a user to view API definitions or possibly even try out the API call?
What you are looking for is a good JavaScript documentation generator. I found a decent one here http://apidocjs.com/example/.
This will allow you to use just Express as OP asked.
Swagger isn't for generating docs, it's for making APIs. So you are going to need to learn a full system to get that feature.
I found a couple more here that I haven't fully looked into yet which seem promising:
Docco here JSDoc here and an article on Documentor for Node
Swagger is an amazing project for auto generating API documentation. It includes an Express module.
http://swagger.io/
https://github.com/wordnik/swagger-node-express
I found this library to be very useful when it comes to keeping your documentation and route declaration logic close together:
https://www.npmjs.com/package/swagger-jsdoc
If you use Postman while developing you might already have a Postman collection containing the most relevant calls.
If that is the case Postman has a docs feature https://learning.postman.com/docs/publishing-your-api/documenting-your-api/

Node unit test - mocking calls to solr

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.

Resources