
Security News
TC39 Advances 11 Proposals for Math Precision, Binary APIs, and More
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
@golevelup/nestjs-testing
Advanced tools
Utilities for making testing NestJS applications easier. Currently supports Jest.
You ever just come across a type that you want to mock, but mocking the entire object seems daunting, and who knows how many sub properties the object has and if those sub properties have sub properties. The list goes on with possible problems. Enter @golevelup/nestjs-testing
's createMock
utility function. This function will create a mock object for you with all sub properties mocked as jest.fn()
unless otherwise provided, to allow for easy mocking later on, but more on that later.
This package and utility function was derived out of a want to help those unit testing things like Guards, Interceptors, and Filters, in NestJS; however, given the dynamic nature of the package, the createMock
utility function can handle so much more than just what's inside of NestJS. Essentially, if it has an interface, @golevelup/nestjs-testing
can mock it!
For examples we'll show how well it works with NestJS' ExecutionContext which extends NestJS' ArgumentHost.
Pretty standard installation, nothing too crazy
npm i @golevelup/nestjs-testing
or
yarn @golevelup/nestjs-testing
Here is where the fun begins. As a heads up, this function does require you to be using Typescript, as it takes advantage of some advanced Typescript features under the hood, like Proxies
.
createMock
function into your test classcreateMock
function plus its generic type inputExample:
import { createMock } from '@golevelup/nestjs-testing';
import { ExecutionContext } from '@nestjs/common';
describe('Mocked Execution Context', () => {
it('should have a fully mocked Execution Context', () => {
const mockExecutionContext = createMock<ExecutionContext>();
expect(mockExecutionContext.switchToHttp()).toBeDefined();
});
});
And just like that, the simplest mock can quickly and easily be made! "Big deal" you may say? It's easy to create a mock with a single property like that? Well, just watch this:
import { createMock } from '@golevelup/nestjs-testing';
import { ExecutionContext } from '@nestjs/common';
describe('Mocked Execution Context', () => {
it('should have a fully mocked Execution Context', () => {
const mockExecutionContext = createMock<ExecutionContext>();
expect(mockExecutionContext.switchToHttp().getRequest()).toBeDefined();
expect(mockExecutionContext.switchToRPC().getContext()).toBeDefined();
expect(mockExecutionContext.switchToWs().getClient()).toBeDefined();
});
});
How's that for ya? No, still not impressed? All right, time to bring out the big guns.
import { createMock } from '@golevelup/nestjs-testing';
import { ExecutionContext } from '@nestjs/common';
describe('Mocked Execution Context', () => {
it('should have a fully mocked Execution Context', () => {
const mockExecutionContext = createMock<ExecutionContext>({
switchToHttp: () => ({
getRequest: () =({
headers: {
authorization: 'auth'
}
})
})
});
mockExecutionContext.switchToHttp().getResponse.mockReturnValue({data: 'res return data'});
expect(mockExecutionContext.switchToHttp().getRequest()).toEqual({
headers: {
authorization: 'auth'
}
});
expect(mockExecutionContext.switchToHttp().getResponse()).toEqual({
data: 'res return data'
});
expect(mockExecutionContext.switchToHttp).toBeCalledTimes(3);
expect(mockExecutionContext.switchToRPC().getContext()).toBeDefined();
expect(mockExecutionContext.switchToWs().getClient()).toBeDefined();
});
});
Note: Be aware that when providing your own mocks, if you asserting how many times you called a parent mock function, the number will be equal to the number of times the function was called in your
expects
plus the number of times the function had to be called to set your mocks. In the above case, we had to callswitchToHttp()
once to set the mock forgetResponse()
and twice for theexpect
calls, so it was called in a total of three times.
The above case shows how well the createMock
utility can take in user provided values as well as returning type safe mocks that can easily be chained and modified as needed.
For a few more examples on what can be done the mock.spec file has some really cool examples that show pretty well just what is doable with this utility.
FAQs
Reusable utilties to help level up NestJS Testing
We found that @golevelup/nestjs-testing demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.