From 90304880259d3a08553bafb4c5aad279b3790c6c Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sat, 5 Mar 2022 18:47:45 +0100 Subject: [PATCH] test: make tests order-independent MariaDB seems to order the returned media objects in a different way, making our tests fail. This refactors the tests to be independent of the order of returned data. Signed-off-by: David Mehren --- test/private-api/me.e2e-spec.ts | 25 +++++++++++++++++-------- test/public-api/me.e2e-spec.ts | 27 ++++++++++++++++++--------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/test/private-api/me.e2e-spec.ts b/test/private-api/me.e2e-spec.ts index f729b820c..13a7ffcf2 100644 --- a/test/private-api/me.e2e-spec.ts +++ b/test/private-api/me.e2e-spec.ts @@ -71,20 +71,29 @@ describe('Me', () => { expect(responseBefore.body).toHaveLength(0); const testImage = await fs.readFile('test/public-api/fixtures/test.png'); - const url0 = await testSetup.mediaService.saveFile(testImage, user, note1); - const url1 = await testSetup.mediaService.saveFile(testImage, user, note1); - const url2 = await testSetup.mediaService.saveFile(testImage, user, note2); - const url3 = await testSetup.mediaService.saveFile(testImage, user, note2); + const imageUrls = []; + imageUrls.push( + await testSetup.mediaService.saveFile(testImage, user, note1), + ); + imageUrls.push( + await testSetup.mediaService.saveFile(testImage, user, note1), + ); + imageUrls.push( + await testSetup.mediaService.saveFile(testImage, user, note2), + ); + imageUrls.push( + await testSetup.mediaService.saveFile(testImage, user, note2), + ); const response = await agent .get('/api/private/me/media/') .expect('Content-Type', /json/) .expect(200); expect(response.body).toHaveLength(4); - expect(response.body[0].url).toEqual(url0); - expect(response.body[1].url).toEqual(url1); - expect(response.body[2].url).toEqual(url2); - expect(response.body[3].url).toEqual(url3); + expect(imageUrls).toContain(response.body[0].url); + expect(imageUrls).toContain(response.body[1].url); + expect(imageUrls).toContain(response.body[2].url); + expect(imageUrls).toContain(response.body[3].url); const mediaUploads = await testSetup.mediaService.listUploadsByUser(user); for (const upload of mediaUploads) { await testSetup.mediaService.deleteFile(upload); diff --git a/test/public-api/me.e2e-spec.ts b/test/public-api/me.e2e-spec.ts index c0f13c087..a498f8ed8 100644 --- a/test/public-api/me.e2e-spec.ts +++ b/test/public-api/me.e2e-spec.ts @@ -202,21 +202,30 @@ describe('Me', () => { expect(response1.body).toHaveLength(0); const testImage = await fs.readFile('test/public-api/fixtures/test.png'); - const url0 = await testSetup.mediaService.saveFile(testImage, user, note1); - const url1 = await testSetup.mediaService.saveFile(testImage, user, note1); - const url2 = await testSetup.mediaService.saveFile(testImage, user, note2); - const url3 = await testSetup.mediaService.saveFile(testImage, user, note2); + const imageUrls = []; + imageUrls.push( + await testSetup.mediaService.saveFile(testImage, user, note1), + ); + imageUrls.push( + await testSetup.mediaService.saveFile(testImage, user, note1), + ); + imageUrls.push( + await testSetup.mediaService.saveFile(testImage, user, note2), + ); + imageUrls.push( + await testSetup.mediaService.saveFile(testImage, user, note2), + ); const response = await request(httpServer) .get('/api/v2/me/media/') .expect('Content-Type', /json/) .expect(200); expect(response.body).toHaveLength(4); - expect(response.body[0].url).toEqual(url0); - expect(response.body[1].url).toEqual(url1); - expect(response.body[2].url).toEqual(url2); - expect(response.body[3].url).toEqual(url3); - for (const fileUrl of [url0, url1, url2, url3]) { + expect(imageUrls).toContain(response.body[0].url); + expect(imageUrls).toContain(response.body[1].url); + expect(imageUrls).toContain(response.body[2].url); + expect(imageUrls).toContain(response.body[3].url); + for (const fileUrl of imageUrls) { const fileName = fileUrl.replace('/uploads/', ''); // delete the file afterwards await fs.unlink(join(uploadPath, fileName));