Comments (7)
@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.
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.
@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.
@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.
@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.
@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.
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)
- "InvalidStateError: The object is in invalid state" when mocking rest api
- Unable to use msw/node for testing solid-js due to `resolve.conditions` set to `browser` HOT 5
- drop CommonJS support HOT 2
- quiet: true should supress RESPONSE LISTENER logs HOT 4
- Narrowing the response body type in `HttpResponse.json` HOT 16
- Error: No known conditions for "./browser" specifier in "msw" package HOT 5
- "Cannot read properties of undefined (reading 'get')" when using generators/async generators as resolvers HOT 2
- `TypeError: crypto.randomUUID is not a function` with msw 2.2.12 HOT 3
- When running Cypress tests in Electron, MSW activation occurs after test completion, causing tests to fail
- Chrome unregisters MSW after 5 mins of inactivity / idle HOT 13
- First-class Server-Sent Events (SSE) API
- The currently registered Service Worker has been generated by a different version of MSW (undefined) HOT 1
- disable prettier as well HOT 1
- Issue with response resolver types after recent upgrade HOT 2
- "TypeError: response.body.getReader is not a function" for HTTPResponse of JSON type HOT 5
- Preserve the original request URL in "onUnhandledRequest" warnings HOT 3
- The cookies object recevied in the handlers varies depending on the placement of the handler within the `setupServer` function. HOT 5
- Calls to worker.use() remove handlers to same endpoint with different method HOT 1
- Unable to use HttpResponse.error() with TypeScript HOT 3
- How do I implement MSW in a NextJS backend?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msw.