Git Product home page Git Product logo

jest-mock-req-res's People

Contributors

carnubak avatar dependabot[bot] avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

jest-mock-req-res's Issues

ReferenceError: jest is not defined

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",
...

Having trouble visualizing how to use `mockResponse`

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!

TypeError: res.setHeader is not a function

  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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.