-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
node-app nestjs e2e test module #1098
Comments
+1 |
any update/plan about this? |
@FrozenPandaz @vsavkin any chance to fix this for Nx 8 release? |
Thought for feedback:
|
@xmlking I prefer that too, is a cleaner solution... In fact, both angular and NestJS have e2e test in some folder inside the main project folder when are created with their official cli... NestJS directory structure (created using
Angular directory structure (created using
|
merged api, api-e2e projects and wepapp and webapp-e2e projects nrwl/nx#1098
in the above commit, I merged e2e projects with corresponding parent projects (both for angular/cypress and nestjs) they are working fine with following commands ng e2e api
ng e2e webapp
|
@xmlking can you send a PR to this repo? 🙏 |
I prefer the existing approach. In a mono repo, wouldn't it be better to split these out of the common application because companies tend to do smoke screen tests now and then on the live system periodically? |
One of the reason I am motivated to merge e2e and main src in single project is , direct e2e dependency on main app code, https://github.com/xmlking/ngx-starter-kit/blob/develop/apps/api/e2e/src/app.e2e-spec.ts#L4 |
@xmlking that's because of how the tests are done using jest and are only an issue with the node applications. In the example, the "end2end" testing is actually done more on a unit test/integration perspective rather than the real "end2end" I think they some confusion over the testing. This to me doesn't feel like end2end and it should be done using jest personally. When it comes to the frontend you use protector or cypress that files to a URL. rather than what happening in the example which is you building a local version of the site to test. the node API. |
For starters, I don't think this is in scope for My main motivation for having an e2e project for a node-app is for repositories that contain only backend code. Such an e2e project would likely use something like As for having the |
@FrozenPandaz well, I think that the node-app should be able to be tested no matter which approach is decided... The nest template provides a good structure for e2e tests using |
I'm with @FrozenPandaz can test e2e on node application is excellent. But my main reservation is it going to the main application root. I like splitting the responsibility of my backend code, for example, having a microservice that deals with database connections and another for authentication etc. |
as Nx 8 was released, how can we deal with nestjs e2e test? this will be added in the future? |
ping @FrozenPandaz @vsavkin |
As Jason mentioned the Run:
Next, rename the test target in the api-e2e project into e2e, and write tests using say supertest. Having said that, I think it is a good idea to generate an e2e test project for both express and nest apps. Could anyone write a proposal of what it will look like for both express and nest projects/ |
@vsavkin what do you mean with |
@vsavkin omit my last question... I understand 👍 I delete the ...
"api-e2e": {
"root": "apps/api-e2e",
"sourceRoot": "apps/api-e2e/src",
"projectType": "application",
"prefix": "api-e2e",
"schematics": {},
"architect": {
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"apps/api-e2e/tsconfig.app.json",
"apps/api-e2e/tsconfig.spec.json"
],
"exclude": ["**/node_modules/**", "!apps/api-e2e/**"]
}
},
"e2e": {
"builder": "@nrwl/jest:jest",
"options": {
"jestConfig": "apps/api-e2e/jest.config.js",
"tsConfig": "apps/api-e2e/tsconfig.spec.json"
}
}
}
} Basic test: // some-test-file.spec.ts
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule } from '../../api/src/app/app.module'; // <----- fails
describe('AppController (e2e)', () => {
let app;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule]
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect({ message: 'Welcome to api!' });
});
}); running Also glad to see this will be improved and supported in Nx 9 |
I followed @vsavkin advice, and as @gperdomor, I created a new app Then, in the spirit of Nx modularization, I moved code of my NestJs
It's clear, that I have a very specific Finally, I imported theses 2 libs into both
Really interested to receive your feedback/comments. Thank you guys for your sharing, and all your great job ! |
@tfalvo can you send us the link of the repo? |
the solution described by @vsavkin and @gperdomor works perfectly.. However, a more "clean" approach would be to bootstrap it like @tfalvo described in his post.. will dive into this later, i guess :) cheers to all of you for making nrwl such an awesome experience! |
Hi, sorry about this. This was mislabeled as stale. We are testing ways to mark not reproducible issues as stale so that we can focus on actionable items but our initial experiment was too broad and unintentionally labeled this issue as stale. |
@tfalvo do you think having e2e for nestjs is not tricky and stable? |
Hi @luqezman, could you precise your question please. e2e with NestJS or e2e with NestJS + NX, or just e2e vs simple unit test??... |
Hey @vsavkin , @DominikPieper and I took some time to go over this, and we came up with the proposal below. A (manual) implementation of this structure with a Nest API can be found here. The only downside to this proposal so far is that Which makes WebStorm complain: It would be great if we could have a way to address this, without having to change too much (if anything at all) on the side of the application. Looking forward to your feedback :) Proposal: Create e2e projects for Node apps in Nx WorkspaceBackend applications in an Nx Workspace (generated with the The reasons behind this is that when e2e tests that are added to a frontend project, they will generally also test the backend. However, in some cases it makes sense to add an e2e test for Node-based projects, for instance if there is only backend code in your workspace. SolutionExisting projectsCreate a new schematic to @nrwl/node plugin called
Running this schematic creates a new project in the workspace file called The structure of the generated app will be very similar to the Having this schematic being standalone allows people to opt in to having an e2e test for a backend project, for instance in an existing Nx Workspace. New projectsAdditionally, when generating a new project using For instance, using this command:
Will result in a structure like this:
|
Hi all, I don't think we need a e2e for api projects. For me its only integrations tests that should live inside the api projects. |
I agree with @khalilou88, // example configuration
// ... node-e2e
"e2e": {
"builder": "@nrwl/jest:jest", // <--- update/create new builder to accept `devServerTarget` option
"options": {
"jestConfig": "apps/node/jest.config.js",
"devServerTarget": "backend:serve" // <--- this is what we need, run another node application here (backend)
},
"configurations": {
"production": {
"devServerTarget": "backend:serve:production"
}
}
}, From e2e files, we can use the API URL which |
I am questioning if making a separate e2e project makes sense for the type of tests that @beeman and @gperdomor and the nestjs example https://docs.nestjs.com/fundamentals/testing#end-to-end-testing. The way that these tests are run are fundamentally different than a cypress test. In cypress tests the tests do not need to have any insight of the inner workings of the applications. In the nestjs example e2e the tests themselves are creating the server and this is what allows you to mock different aspects of the nestjs application. In cypress, the cypress tooling provides that functionality through intercept and fixtures. This logic lives outside your core application, cypress interacts with the browser api not your application. Now I could definitely see the benefit of an e2e test project and configuration like @AliYusuf95 mentions but these would likely be more for true System Integration Tests. Where they run against the devServerTarget and the specs never reference the apps AppModule. The reason I say a System Integration Test is I am unaware of a built in functionality like cypress provides to mock the connection to your database or other api. This could be done with some fore thought in your engineering and use a flag to swap out the injected providers for mock providers but I am wondering if there is a better way. Swapping the services/providers for mock providers is what they are doing in the e2e example on nestjs. |
Alright, looks like nock might be a good solution for nodejs network request mocking. https://www.npmjs.com/package/nock |
@JoA-MoS I'd love to see another way to do it if there is. I've been working on most of my projects with the suggestion I described above, and it worked very well. Having this way of testing (basically, a single collection of tests that are firing queries to the backend - for me it's not relevant if it's called e2e or integration test), helped me and the teams I worked with a lot in getting proper tests for our backend. If there are ways to optimize / improve I'd definitely be interested in giving that a shot. Happy to connect if you like to chat about it :) |
Thanks @beeman I will try your approach and the other options and see what makes most sense. How did you get around your open question in your post (the linting issue)? If I have more questions I will message you on slack |
I noticed this question was raised before Nx 8 release. It appears we are in Nx |
+1 for this feature |
+1 this would be really appreciated |
I've tried all above situations, however it did not bring what I needed. We use nx for a NestJs backend and an Angular frontend. In the frontend everything runs fine with Cypress and mocking the request to the backend. But we also want a similar test scenario for the NestJs Backend including a database. I resorted back to # create a dummy backend-e2e project
nx g @nrwl/node:app backend-e2e
import * as fetch from 'isomorphic-fetch';
describe('FormsData', () => {
const server = 'http://localhost:3333/graphql';
it('should do something', async () => {
const result = await fetch(server, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query:
`query { __schema { queryType { name } } }`
})
})
expect(result).toBeTruthy();
});
});
// [...]
"scripts": {
// [...]
"pree2e:server": "docker-compose up -d",
"e2e:server": "nx run backend:serve --skip-nx-cache",
"e2e:test": "nx run backend-e2e:e2e",
"e2e": "start-server-and-test e2e:server 'http-get://localhost:3333/graphql?query={ __schema { queryType { name } } }' e2e:test"
} It got me unstuck for now, looking forward to a nx-build-in solution |
Hi, thanks for the hint, creating an e2e-Project for a Nest Project.
Is there a way to allow the app-Import in this special case? |
So, any updates? What'd be the right way to add e2e tests? |
I've been using something like this since I wrote it and it works great for me: |
Still nothing on that ? I can't find a way to write e2e tests like in NestJs doc, that's pretty annoying right now |
Hello :) Is there a plan to implement this feature in the next releases? Thanks for your work! |
This is fixed by #14805 |
This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context. |
Prerequisites
Please answer the following questions for yourself before submitting an issue.
YOU MAY DELETE THE PREREQUISITES SECTION.
Expected Behavior
ng g node-app api --framework=nestjs
should generate bothapi
andapi-e2e
modulesCurrent Behavior
only generate
api
moduleSimilar to webApps , we need e2e testing for node-apps
Proposal :
we can generate NestJS e2e module something like this https://github.com/xmlking/ngx-starter-kit/tree/develop/apps/api-e2e
and
angular.json
entryhttps://github.com/xmlking/ngx-starter-kit/blob/develop/angular.json#L286
then we can do e2e tests with:
The text was updated successfully, but these errors were encountered: