From 959e6e1645928799cd437cdfc713b329a19b0a91 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Fri, 21 Aug 2020 21:13:11 +0200 Subject: [PATCH 1/2] Add Jest config for E2E tests Signed-off-by: David Mehren --- test/jest-e2e.json | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 test/jest-e2e.json diff --git a/test/jest-e2e.json b/test/jest-e2e.json new file mode 100644 index 000000000..e9d912f3e --- /dev/null +++ b/test/jest-e2e.json @@ -0,0 +1,9 @@ +{ + "moduleFileExtensions": ["js", "json", "ts"], + "rootDir": ".", + "testEnvironment": "node", + "testRegex": ".e2e-spec.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + } +} From 4b0729cc9c692f78042b43857374dc1de77a9496 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Fri, 21 Aug 2020 21:16:20 +0200 Subject: [PATCH 2/2] Add E2E tests for /notes route Most tests already test the correct thing, but all obviously fail because nothing is implemented yet. Signed-off-by: David Mehren --- test/public-api/notes.e2e-spec.ts | 119 ++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 test/public-api/notes.e2e-spec.ts diff --git a/test/public-api/notes.e2e-spec.ts b/test/public-api/notes.e2e-spec.ts new file mode 100644 index 000000000..3446c0f44 --- /dev/null +++ b/test/public-api/notes.e2e-spec.ts @@ -0,0 +1,119 @@ +import { INestApplication } from '@nestjs/common'; +import { Test } from '@nestjs/testing'; +import * as request from 'supertest'; +import { AppModule } from '../../src/app.module'; +import { NotesService } from '../../src/notes/notes.service'; + +describe('Notes', () => { + let app: INestApplication; + let notesService: NotesService; + + beforeAll(async () => { + const moduleRef = await Test.createTestingModule({ + imports: [AppModule], + }).compile(); + + app = moduleRef.createNestApplication(); + notesService = moduleRef.get(NotesService); + await app.init(); + }); + + it(`POST /notes`, async () => { + const newNote = 'This is a test note.'; + const response = await request(app.getHttpServer()) + .post('/notes') + .send(newNote) + .expect('Content-Type', /json/) + .expect(201); + expect(response.body.metadata?.id).toBeDefined(); + expect( + notesService.getNoteByIdOrAlias(response.body.metadata.id).content, + ).toEqual(newNote); + }); + + it(`GET /notes/{note}`, async () => { + notesService.createNote('This is a test note.', 'test1'); + const response = await request(app.getHttpServer()) + .get('/notes/test1') + .expect('Content-Type', /json/) + .expect(200); + expect(response.body.content).toEqual('This is a test note.'); + }); + + it(`POST /notes/{note}`, async () => { + const newNote = 'This is a test note.'; + const response = await request(app.getHttpServer()) + .post('/notes/test2') + .send(newNote) + .expect('Content-Type', /json/) + .expect(201); + expect(response.body.metadata?.id).toBeDefined(); + return expect( + notesService.getNoteByIdOrAlias(response.body.metadata.id).content, + ).toEqual(newNote); + }); + + it(`DELETE /notes/{note}`, async () => { + notesService.createNote('This is a test note.', 'test3'); + await request(app.getHttpServer()) + .delete('/notes/test3') + .expect(200); + return expect(notesService.getNoteByIdOrAlias('test3')).toBeNull(); + }); + + it(`PUT /notes/{note}`, async () => { + notesService.createNote('This is a test note.', 'test4'); + await request(app.getHttpServer()) + .put('/notes/test4') + .send('New note text') + .expect(200); + return expect(notesService.getNoteByIdOrAlias('test4').content).toEqual( + 'New note text', + ); + }); + + it.skip(`PUT /notes/{note}/metadata`, () => { + // TODO + return request(app.getHttpServer()) + .post('/notes/test5/metadata') + .expect(200); + }); + + it.skip(`GET /notes/{note}/metadata`, () => { + notesService.createNote('This is a test note.', 'test6'); + return request(app.getHttpServer()) + .get('/notes/test6/metadata') + .expect(200); + // TODO: Find out how to check the structure of the returned JSON + }); + + it(`GET /notes/{note}/revisions`, async () => { + notesService.createNote('This is a test note.', 'test7'); + const response = await request(app.getHttpServer()) + .get('/notes/test7/revisions') + .expect('Content-Type', /json/) + .expect(200); + expect(response.body.revisions).toHaveLength(1); + }); + + it(`GET /notes/{note}/revisions/{revision-id}`, async () => { + notesService.createNote('This is a test note.', 'test8'); + const response = await request(app.getHttpServer()) + .get('/notes/test8/revisions/1') + .expect('Content-Type', /json/) + .expect(200); + expect(response.body.content).toEqual('This is a test note.'); + }); + + it(`GET /notes/{note}/content`, async () => { + notesService.createNote('This is a test note.', 'test9'); + const response = await request(app.getHttpServer()) + .get('/notes/test9/content') + .expect(200); + expect(response.body).toEqual('This is a test note.'); + }); + + afterAll(async () => { + await app.close(); + }); +});