The Wayback Machine - https://web.archive.org/web/20200615020739/https://github.com/nestjs/nest/issues/1539
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 · 3 comments
Open

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

SKCrawford opened this issue Feb 8, 2019 · 3 comments

Comments

@SKCrawford
Copy link

@SKCrawford SKCrawford commented Feb 8, 2019

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

@changyeamoon changyeamoon commented Jun 26, 2019

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

@BrunnerLivio
Copy link
Member

@BrunnerLivio 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
@TannerGabriel TannerGabriel mentioned this issue Oct 16, 2019
3 of 3 tasks complete
@kamilmysliwiec kamilmysliwiec removed the PRs open label Nov 3, 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 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);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.