I have to create jest unit test for some services in nestJS in which sub e pub nats calls are used. I googled a lot but I didn't find a solution so far. Do you have any idea how to mock nats pub and sub calls? Which Is the best approach to achieve this?
Related
How can i create a microservice that contains all the shared/common login/classes between projects?
I have a gateway, and different microservices, let say auth and account.
How can I share the user.model for example, considering that both services will use that entity?
Also considering that some common methods can be located in this common microservices in order to not have the code duplicated.
Check out the library module of microservice in Nestjs
https://docs.nestjs.com/cli/libraries
Here you can create a library module with the command
nest g library my-library
Then in that module, you can put the shared module like constant, util, etc which are common in all microservices.
To use that module import like this
import { MyLibraryModule } from '#app/my-library';
I try to split a part of my serverless.yml template, events node, into multiple sub-files. I have a fairly large lamba api gateway. The “events” node is growing very long, its becoming difficult to manage.
I CANT split a function or service, its important that reamin only one api service.
Its possible to divide only “events” node of serverless template into sub several files, for example for each event of my api service (sections, roles, etc), such as:
sections-events.sls.yml, roles-events.sls.yml, and so on?
Ive tried more solutions but none worked..
This is my original serverless.yml template.
This is one of the tests I've done using template variables but it doesn't work ..
Looking at the sls documentation, I thought it was possible to structure the template as follows:
functions:
graphql:
handler: src/index.handler
...
events:
${file(./sections-events.serverless.yml):events}
${file(./roles-events.serverless.yml):events}
but the template validator returns an error as the events node must be an array and this syntax is not possible.
TEST 1:
TEST 2:
I hope someone can help me find a possible path, even though im convinced that isn't feasible!
Thanks a lot to the whole community!
Good Job.
I'm new to NestJS and would like to understand how I can unit test an endpoint with config dependencies and 3rd party library dependencies.
Code flow is along the lines of
Main -> Controller -> Service
Service uses config values from ConfigModule and passes these values to a third party library for authentication. The config values themselves are loaded in onModuleInit.
Please let me know if examples/code snippets are required. Thank you!
This repository has a lot of examples on unit testing with NestJS.
In general, with the class your testing, you should mock out the dependencies being injected into it using a custom provider. Then you can have complete control over what values are being returned from the dependencies
I met this clock-skew correction error(https://aws.amazon.com/blogs/developer/clock-skew-correction/) I'm getting from S3 getobject call from Lambda, and I have made a fix to the issue(this is in Nodejs)and right now is writing the unit test for the function.
Basically I'm trying to simulate the scenario where the client has a time difference of 15+ mins with S3, I have search for few S3 mock up library, but have no clue how to do so.
I'm using mocha as the test framework
I'm working migrating a bunch of unit tests from mockery to jest. When I jest a module that requires the new relic agent like so: require('newrelic'), I get downstream errors like :
- TypeError: Cannot convert undefined or null to object
at Object.<anonymous> (node_modules/newrelic/lib/config.js:165:33)
at Runtime._execModule (node_modules/jest-cli/src/Runtime/Runtime.js:261:17)
at Object.<anonymous> (node_modules/newrelic/lib/logger.js:18:14)
at Object.<anonymous> (node_modules/newrelic/index.js:3:14)
What is the best way to deal with modules like newrelic which jest has a hard time mocking? What have other people done when they have both jest and newrelic in their stack?
The route I ended up taking was to create a mock module for newrelic in my __mocks__ folder:
module.exports = {
addCustomParameter: jest.fn()
};
I will probably need to add more functions later, but for now this is enough. I still wonder if there is a way to get jest to auto mock the newrelic library without erroring.
I've seen this with a few modules were automocking fails for various reasons, although it seems to happen a lot less frequently in the newer versions of Jest.
As #linuxdan suggests you might be able to work around the issue by using the manual mocking functionality documented here.
To so this you'll probably want to just export an object with the expected methods generated using jest.fn().
The reason this will work is it will stop jest trying to determine the methods it needs to auto mock on the newrelic library. It will be during this process that it fails.