Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Jstn2004 committed Apr 17, 2024
2 parents bde101f + c7ab38a commit 4709767
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 362 deletions.
79 changes: 79 additions & 0 deletions backend/docs/testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Testing

## Introduction

[Mocha](https://github.com/mochajs/mocha) and [Jest](https://github.com/jestjs/jest) were considered as testing frameworks. Jest was chosen, because it is more commonly used and the team was more familiar with it. NestJS, which is used as the backend framework, also uses jest for testing by default.

## Getting started

<https://jestjs.io/docs/getting-started>
<https://docs.nestjs.com/fundamentals/testing#unit-testing>

## Conventions

Tests should be defined in a `test` folder in both the frontend and backend directories. Unit test files use the file ending `.spec.ts`. End to end tests use the `.e2e-spec.ts` file ending.

For unit tests we use the assertions provided by Jest. For end to end tests we use [`supertest`](https://github.com/ladjs/supertest).

## Example Unit Test

```ts
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';

describe('CatsController', () => {
let catsController: CatsController;
let catsService: CatsService;

beforeEach(() => {
catsService = new CatsService();
catsController = new CatsController(catsService);
});

describe('findAll', () => {
it('should return an array of cats', async () => {
const result = ['test'];
jest.spyOn(catsService, 'findAll').mockImplementation(() => result);

expect(await catsController.findAll()).toBe(result);
});
});
});
```

## Example End to End Test

```ts
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { CatsModule } from '../../src/cats/cats.module';
import { CatsService } from '../../src/cats/cats.service';
import { INestApplication } from '@nestjs/common';

describe('Cats', () => {
let app: INestApplication;
let catsService = { findAll: () => ['test'] };

beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [CatsModule],
})
.overrideProvider(CatsService)
.useValue(catsService)
.compile();

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

it(`/GET cats`, () => {
return request(app.getHttpServer()).get('/cats').expect(200).expect({
data: catsService.findAll(),
});
});

afterAll(async () => {
await app.close();
});
});
```
Binary file removed docs/01-requirements/anforderungen.pdf
Binary file not shown.
208 changes: 0 additions & 208 deletions docs/01-requirements/anforderungen.typ

This file was deleted.

Binary file not shown.
Loading

0 comments on commit 4709767

Please sign in to comment.