Code Monkey home page Code Monkey logo

Comments (7)

c0m1t avatar c0m1t commented on May 13, 2024 2

@kettanaito It is the same jest warning you mentioned in the first post:

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

Can you try your test without supertest?

I do my best to do that.

Thank you

from msw.

c0m1t avatar c0m1t commented on May 13, 2024

We are having memory leak issue in integration tests. This is an integration test for nestjs which uses typescript. Here's how the code looks:

import { INestApplication, Query } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import { AppModule } from 'src/app.module';
import request from 'supertest';

const server = setupServer();

describe(<name>, () => {
  let app: INestApplication;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();
    app = module.createNestApplication();
    await app.init();
  });

  beforeAll(() => server.listen());
  afterEach(() => server.resetHandlers());
  afterAll(() => server.close());

  it(<name>, () => {
    server.use(
      rest.get(<endpoint>, (req, res, ctx) => {
        return res(ctx.status(500));
      }),
    );
    const query = <query>;
    return request(app.getHttpServer())
      .post('/graphql')
      .send({
        operationName: <operation-name>,
        query,
        variables: <variables>,
      })
      .expect(200)
      .expect({
        data: <data>,
      });
  });

  afterAll(async () => {
    await app.close();
  });
});

And after running the test we get the jest memory leak warning you mentioned earlier.

Running tests with --detectOpenHandles flag yields

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  ●  TCPSERVERWRAP

      52 |     `;
      53 |     return request(app.getHttpServer())
    > 54 |       .post('/graphql')
         |        ^

And this is the error stack:

      at Test.Object.<anonymous>.Test.serverAddress (../node_modules/supertest/lib/test.js:59:33)
      at new Test (../node_modules/supertest/lib/test.js:36:12)
      at Object.obj.<computed> [as post] (../node_modules/supertest/index.js:25:14)
      at Object.<anonymous> (path/to/test:54:8)

Running test not using msw does not leak memory and is fine.

from msw.

msutkowski avatar msutkowski commented on May 13, 2024

@c0m1t Is this just psuedo-code you pasted above? If not, there are a handful of issues with it from what I can see. Could you perhaps paste the real usage, as well as what your default server/handlers look like?

I don't know how nestjs builds requests, but msw requires an absolute URL in a node environment such as http://example.com/graphql.

Also, if you're using superagent, you should just superagent directly in the tests (instead of supertest), and let msw intercept the actual network request. I don't think providing the app.getHttpServer instance to a request mocking lib is what you want to be doing here - I would imagine no actual network request is being made for msw to even intercept, but I could be wrong.

from msw.

c0m1t avatar c0m1t commented on May 13, 2024

@msutkowski
This is the actual code but I removed some strings from it.
I should clarify that this is a GraphQL layered on top of a REST API. So things being mocked here are the REST endpoints and not the GraphQL itself. Services in GraphQL resolvers make requests to a REST server and we want to mock those. Given the mocked REST input, we test the result of GraphQL layer and see if they match expectations.

Also, if you're using superagent, you should just superagent directly in the tests (instead of supertest), and let msw intercept the actual network request.

This is the official NestJS documentation about End-to-end testing. supertest is for testing the graphql endpoint (which we do not want to mock with msw), and it works fine. We want to mock the REST endpoints which services inside GraphQL layer make requests to. Another point is tests are passing, using msw, and are not failing (presumably mocking is successful), but there is a memory leak warning.

from msw.

kettanaito avatar kettanaito commented on May 13, 2024

@c0m1t, may I please ask you to ensure you're using the latest version of msw and its dependencies?

In particular, [email protected] fixes 2 memory leaks, so ensure you have a version equal or higher than that;

$ npm ls node-request-interceptor

from msw.

c0m1t avatar c0m1t commented on May 13, 2024

@kettanaito [email protected] and [email protected] are installed.

  • The issue was kinda solved by adding async to test functions
  • We have a script which runs the E2E tests:
    "test:e2e": "NODE_PATH=. jest --config ./test/jest-e2e.json",

The thing is executing the yarn test:e2e command works fine and there is no memory leak warning. But running test for a specific file which uses msw (yarn test:e2e filename.e2e-spec.ts), still leads to that warning.

from msw.

kettanaito avatar kettanaito commented on May 13, 2024

Just to clarify: is there a memory leak warning or NodeJS heap stack trace? A pending handler in a test doesn't necessarily mean a memory leak. It can be an async task that never finished, for example.

Can you try your test without supertest? I understand that alters the program you're testing, but I'd be thankful if you tried and share the outcome here with us.

from msw.

Related Issues (20)

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.