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

[Doc request] Add unit/e2e tests to repo's samples #1539

Open
SKCrawford opened this issue Feb 8, 2019 · 14 comments
Open

[Doc request] Add unit/e2e tests to repo's samples #1539

SKCrawford opened this issue Feb 8, 2019 · 14 comments
Labels

Comments

@SKCrawford
Copy link

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Samples in nest/samples are missing examples of unit and/or e2e tests. Particularly of interest to me is 05-sql-typeorm, but other samples seem to be the same. Sample 01-cats-app contains some tests, but they are quite basic.

Expected Desired behavior

Samples for specific features/techniques include unit and/or e2e tests pertaining to those features/techniques. For example, the sample given above (05) could include tests that show how to test endpoints, mock data, or connect to a testing database.

Minimal reproduction of the problem with instructions

Sample 05-sql-typeorm. Notably, the tests/ dir and /src/**/*.spec.ts files are not present. Also, they are excluded by the samples' local gitignore.

What is the motivation / use case for changing the behavior?

  • Give new users like me simple and practical examples for implementing tests for different features/techniques.
  • Better reflect the documentation's perspective of testing as one of the "fundamentals" (if it's a fundamental, it could be reasoned that having more than a page in the docs would be suitable)
  • Serve as quick reference to users.

Environment

Nest version: master branch

For Tooling issues: N/A

Others: N/A

@changyeamoon
Copy link

@kamilmysliwiec Any updates on this? New user here -would love to use right techniques

@BrunnerLivio
Copy link
Member

BrunnerLivio commented Oct 11, 2019

Please submit only one Pull Request per sample.

These contributions do have a great impact on NestJS. Not only will you provide better documentation for developers (the sample folder is a widely used resource to look up real-world examples) but also improve NestJSs integration tests.

If you need help with your Pull Request, you can find good resources on the Hacktoberfest website. Additionally, you can always ask for support at our Discord channel. Happy hacking! :)

kiwikern added a commit to kiwikern/nest that referenced this issue Oct 12, 2019
kiwikern added a commit to kiwikern/nest that referenced this issue Oct 12, 2019
kiwikern added a commit to kiwikern/nest that referenced this issue Oct 12, 2019
kiwikern added a commit to kiwikern/nest that referenced this issue Oct 12, 2019
kiwikern added a commit to kiwikern/nest that referenced this issue Nov 3, 2019
kiwikern added a commit to kiwikern/nest that referenced this issue Nov 3, 2019
@abinhho
Copy link

abinhho commented Jan 2, 2020

A sample e2e test.

// mytest.e2e-spec.ts
import * as request from 'supertest';
import { Test } from "@nestjs/testing";
import { INestApplication } from '@nestjs/common';
import { MyTestsController } from './myTests.controller';
import { MyTestsService } from ".";
import { Warehouse } from './myTest.entity';
import { getRepositoryToken } from '@nestjs/typeorm';

describe("MyTestsController (e2e)", () => {

  let app: INestApplication;
  const myTests = [
    {
      id: "1ccc2222a-8072-4ff0-b5ff-103cc85f3be6",
      name: "Name #1",
    }
  ];

  const myTestsCount = 1;
  const getAllResult = { myTests, myTestsCount };
  // Mock data for service
  let myTestsService = { getAll: () => getAllResult };

  beforeAll(async () => {
    const module = await Test.createTestingModule({
      providers: [
        MyTestsService,
        {
          provide: getRepositoryToken(Warehouse),
          useValue: myTestsService
        }
      ],
      controllers: [MyTestsController],
    })
      .overrideProvider(MyTestsService)
      .useValue(myTestsService)
      .compile();

    app = module.createNestApplication();
    await app.init();
  });

  beforeEach(async () => {});

  it(`/GET all myTests`, async() => {
    return await request(app.getHttpServer())
      .get('/myTests')
      .expect(200)
      .expect(myTestsService.getAll());
  });

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

});

// myTests.service.ts
public async getAll(query?): Promise<myTestsRO> {
  const qb = await this.repo.createQueryBuilder("myTests");
  const myTestsCount = await qb.getCount();

  if ("limit" in query) {
    qb.limit(query.limit);
  }

  if ("offset" in query) {
    qb.offset(query.offset);
  }

  const myTests = await qb
    .getMany()
    .then(myTests =>
      myTests.map(entity => WarehouseDto.fromEntity(entity))
    );

  return { myTests, myTestsCount };
}
// myTest.controller.ts
@Get()
public async getAll(@Query() query): Promise<myTestsRO> {
  try {
    return await this.myTestsService.getAll(query);
  } catch (error) {
    throw new InternalServerErrorException(error.message);
  }
}

@YashKumarVerma
Copy link

YashKumarVerma commented Dec 10, 2020

are we maintaining a list somewhere of the samples that have E2E/units written, or contributors are expected to search through the samples 😅.

Maintaining a list will most probably avoid multiple people working on the same items.

@kamilmysliwiec kamilmysliwiec added the priority: very low (5) Very low-priority issue for consideration label Feb 2, 2021
@omidh28
Copy link

omidh28 commented Aug 23, 2021

I think current E2E sample in documentations (cat) is oversimplified and misses many points. For instance it basically mocks cats service and avoids database connection altogether which makes me think what'd be the point of that? How's that any different from unit testing?

Another flaw is app setup, the sample in docs completely skips over features that main function might have been implemented, things like Guards, Interceptors and... Which has a huge effect on E2E test results.

@kamilmysliwiec
Copy link
Member

@omidh28 PRs are more than welcome!

@jmcdo29
Copy link
Member

jmcdo29 commented Aug 24, 2021

For those looking for more as well, I have a growing repository of examples for testing.

@nathanArseneau
Copy link
Contributor

I just did one PR for sampl-02 let me know if this is useful and I can do the other sample that doesn't have any tests.

Any feedback is welcome.

@nathanArseneau
Copy link
Contributor

@kamilmysliwiec any update on my pr, I have a bit of time this week I would love to help add more unit/e2e tests

@nathanArseneau
Copy link
Contributor

@kamilmysliwiec any update on this, i was looking to do more contribution :)

@nathanArseneau
Copy link
Contributor

Is this issue still needed, or my pr is not good?

@AxelDavid45
Copy link

Hi! Do you still need help with this?

@mwelwankuta
Copy link

Is this issue still open ?

@Mbistami
Copy link

Hello I can help with this issue if no one working on it? @kamilmysliwiec @BrunnerLivio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests