carnubak / jest-mock-req-res Goto Github PK
View Code? Open in Web Editor NEWJest mocks for Express Request and Response objects. Inspired by mock-req-res.
License: MIT License
Jest mocks for Express Request and Response objects. Inspired by mock-req-res.
License: MIT License
I'm trying to use this module with my Javascript project (not TypeScript) but I'm getting a dependency problem even though I have Jest installed. My NodeJs version is 10. Am I doing something wrong or is this only compatible with TypeScript?
ReferenceError: jest is not defined
at Object.exports.mockRequest (.../myproject/node_modules/jest-mock-req-res/dist/index.js:1:246)
"devDependencies": {
"jest": "^26.4.2",
"jest-mock-req-res": "^1.0.2",
...
● AuthService › getAuthorization › should redirect for authentication when user is not logged
TypeError: res.setHeader is not a function
31 | getAuthorization(@Request() req, @Res() res: Response) {
32 | if (!req.user) {
> 33 | res.setHeader(
| ^
34 | 'Set-Cookie',
35 | `login_callback=./get_authorization; HttpOnly; Path=/; `,
36 | );
at AuthService.getAuthorization (auth/services/auth.service.ts:33:11)
at Object.<anonymous> (auth/__tests__/auth.service.spec.ts:52:35)
it('should redirect for authentication when user is not logged', async () => {
const redirectUrl = service.getAuthorization(
mockRequest({ body: {} }),
mockResponse({}),
);
console.log(redirectUrl);
});
Is there anything I'm losing sight of?
In my situation, I'm testing a controller as part of an ExpressJS-based server.
The call in my test looks like this: await RegistrantControllerModule.register(req1, res1);
Above that I have:
const req1 = mockRequest({body: testRegistration});
const res1 = mockResponse({});
The mockRequest
works as I expect; body
is used in the controller code for the test.
But if my controller succeeds, it should res.json
an object, or if there are errors, res.status(x).json(errors)
.
Should I build out the responses I'm desire to see back in my test file from the res.json
call in the controller?
For example in a test I expect to be successful:
const res1 = mockResponse({
json: {
auth_token: "MyToken",
info: {
name: Tim,
email: "[email protected]",
websiteURL: "localhost",
}
}
});
The idea being that when I hit res.json
in the controller, that mockResponse is returned to the test?
When I try that, I end up with TypeErrors for those values.
Examples showing how I might use this function in a test file would be most helpful.
Or, am I misunderstanding what this function is designed to accomplish?
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.