Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When testing express application with inject, payload is always empty #54

Open
2 tasks done
brechtdewinne opened this issue Oct 20, 2021 · 3 comments
Open
2 tasks done
Labels
bug Something isn't working

Comments

@brechtdewinne
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.22.1

Plugin version

0.3.3

Node.js version

16.4.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

11.2.1

Description

I'm having an express application which will transition completely to Fastify in the future. We want to write integration tests for this application and leverage fastify.inject but when executing a request, the payload is always empty.

To run our tests we are using Jest (27.2.5).

Steps to Reproduce

// server.ts

const router = express.Router();

const app = fastify();

router.get('/test', (req, res) => {
  res.status(201);
  res.json({ hello: 'world' });
});

await app.register(fastifyExpress).after(async () => {
  app.use(router);
});
// test.ts

test('No query parameters', async () => {
  app.inject().get('/test').end((err, response) => {
    expect(response.statusCode).toEqual(201);
    expect(response.json()).toEqual({ hello: 'world' });
  });
});

Expected Behavior

I would expect a body and not an empty string.

@mcollina
Copy link
Member

Unfortunately inject() does not work with Express :/. Express mutates the prototype of the Node.js request and response object and currently we have not figured out how to deal with it.

@mcollina mcollina added the bug Something isn't working label Oct 20, 2021
@billyvg
Copy link

billyvg commented Nov 15, 2021

I'm not sure what the differences are but https://github.com/fastify/middie works with inject()

@climba03003
Copy link
Member

climba03003 commented Nov 15, 2021

I'm not sure what the differences are but https://github.com/fastify/middie works with inject()

middle pass what the middleware needs and run it - which is just fastify
fastify-express use express directly - which is both fastify (request) and express (response)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants