From 0f2398295bcced3af3772bb4add1f42472c97893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Thu, 12 Sep 2019 11:14:42 +0200 Subject: [PATCH 1/3] Helper message for backend tests failed by missing constraints in database --- .../src/middleware/slugifyMiddleware.spec.js | 86 +++++++++++++++---- 1 file changed, 70 insertions(+), 16 deletions(-) diff --git a/backend/src/middleware/slugifyMiddleware.spec.js b/backend/src/middleware/slugifyMiddleware.spec.js index 9e5456b01..2a78e7975 100644 --- a/backend/src/middleware/slugifyMiddleware.spec.js +++ b/backend/src/middleware/slugifyMiddleware.spec.js @@ -28,7 +28,9 @@ beforeAll(() => { beforeEach(async () => { variables = {} - const admin = await factory.create('User', { role: 'admin' }) + const admin = await factory.create('User', { + role: 'admin', + }) await factory.create('User', { email: 'someone@example.org', password: '1234', @@ -66,9 +68,16 @@ describe('slugifyMiddleware', () => { }) it('generates a slug based on title', async () => { - await expect(mutate({ mutation: createPostMutation, variables })).resolves.toMatchObject({ + await expect( + mutate({ + mutation: createPostMutation, + variables, + }), + ).resolves.toMatchObject({ data: { - CreatePost: { slug: 'i-am-a-brand-new-post' }, + CreatePost: { + slug: 'i-am-a-brand-new-post', + }, }, }) }) @@ -90,13 +99,22 @@ describe('slugifyMiddleware', () => { content: 'Some content', categoryIds, } - await expect(mutate({ mutation: createPostMutation, variables })).resolves.toMatchObject({ - data: { CreatePost: { slug: 'pre-existing-post-1' } }, + await expect( + mutate({ + mutation: createPostMutation, + variables, + }), + ).resolves.toMatchObject({ + data: { + CreatePost: { + slug: 'pre-existing-post-1', + }, + }, }) }) describe('but if the client specifies a slug', () => { - it('rejects CreatePost', async () => { + it('rejects CreatePost (on FAIL Neo4j constraints may not defined in database)', async () => { variables = { ...variables, title: 'Pre-existing post', @@ -104,8 +122,17 @@ describe('slugifyMiddleware', () => { slug: 'pre-existing-post', categoryIds, } - await expect(mutate({ mutation: createPostMutation, variables })).resolves.toMatchObject({ - errors: [{ message: 'Post with this slug already exists!' }], + await expect( + mutate({ + mutation: createPostMutation, + variables, + }), + ).resolves.toMatchObject({ + errors: [ + { + message: 'Post with this slug already exists!', + }, + ], }) }) }) @@ -156,31 +183,58 @@ describe('slugifyMiddleware', () => { }) it('generates a slug based on name', async () => { - await expect(mutate({ mutation, variables })).resolves.toMatchObject({ - data: { SignupVerification: { slug: 'i-am-a-user' } }, + await expect( + mutate({ + mutation, + variables, + }), + ).resolves.toMatchObject({ + data: { + SignupVerification: { + slug: 'i-am-a-user', + }, + }, }) }) describe('if slug exists', () => { beforeEach(async () => { - await factory.create('User', { name: 'I am a user', slug: 'i-am-a-user' }) + await factory.create('User', { + name: 'I am a user', + slug: 'i-am-a-user', + }) }) it('chooses another slug', async () => { - await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + await expect( + mutate({ + mutation, + variables, + }), + ).resolves.toMatchObject({ data: { - SignupVerification: { slug: 'i-am-a-user-1' }, + SignupVerification: { + slug: 'i-am-a-user-1', + }, }, }) }) describe('but if the client specifies a slug', () => { beforeEach(() => { - variables = { ...variables, slug: 'i-am-a-user' } + variables = { + ...variables, + slug: 'i-am-a-user', + } }) - it('rejects SignupVerification', async () => { - await expect(mutate({ mutation, variables })).resolves.toMatchObject({ + it('rejects SignupVerification (on FAIL Neo4j constraints may not defined in database)', async () => { + await expect( + mutate({ + mutation, + variables, + }), + ).resolves.toMatchObject({ errors: [ { message: 'User with this slug already exists!', From 9233049ff3ea46daddd9427e6d9556f2382b4144 Mon Sep 17 00:00:00 2001 From: roschaefer Date: Thu, 12 Sep 2019 16:57:05 +0200 Subject: [PATCH 2/3] Show a helpful message only if test fails Inspired by the `ON FAIL` markup from the previous commit 0f2398295bcced3af3772bb4add1f42472c97893 --- .../src/middleware/slugifyMiddleware.spec.js | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/backend/src/middleware/slugifyMiddleware.spec.js b/backend/src/middleware/slugifyMiddleware.spec.js index 2a78e7975..6c8495e05 100644 --- a/backend/src/middleware/slugifyMiddleware.spec.js +++ b/backend/src/middleware/slugifyMiddleware.spec.js @@ -114,7 +114,7 @@ describe('slugifyMiddleware', () => { }) describe('but if the client specifies a slug', () => { - it('rejects CreatePost (on FAIL Neo4j constraints may not defined in database)', async () => { + it('rejects CreatePost', async (done) => { variables = { ...variables, title: 'Pre-existing post', @@ -122,18 +122,31 @@ describe('slugifyMiddleware', () => { slug: 'pre-existing-post', categoryIds, } - await expect( - mutate({ - mutation: createPostMutation, - variables, - }), - ).resolves.toMatchObject({ - errors: [ - { - message: 'Post with this slug already exists!', - }, - ], - }) + try { + await expect(mutate({ mutation: createPostMutation, variables })).resolves.toMatchObject({ + errors: [ + { + message: 'Post with this slug already exists!', + }, + ], + }) + done() + } catch(error) { + throw new Error(` + ${error} + + Probably your database has no unique constraints! + + To see all constraints go to http://localhost:7474/browser/ and + paste the following: + \`\`\` + CALL db.constraints(); + \`\`\` + + Learn how to setup the database here: + https://docs.human-connection.org/human-connection/neo4j + `) + } }) }) }) From 86f9824c2e73aebc08669de9040907e47b81738a Mon Sep 17 00:00:00 2001 From: roschaefer Date: Thu, 12 Sep 2019 23:13:20 +0200 Subject: [PATCH 3/3] Fix lint --- backend/src/middleware/slugifyMiddleware.spec.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/backend/src/middleware/slugifyMiddleware.spec.js b/backend/src/middleware/slugifyMiddleware.spec.js index 6c8495e05..e65b7ba6c 100644 --- a/backend/src/middleware/slugifyMiddleware.spec.js +++ b/backend/src/middleware/slugifyMiddleware.spec.js @@ -114,7 +114,7 @@ describe('slugifyMiddleware', () => { }) describe('but if the client specifies a slug', () => { - it('rejects CreatePost', async (done) => { + it('rejects CreatePost', async done => { variables = { ...variables, title: 'Pre-existing post', @@ -123,7 +123,9 @@ describe('slugifyMiddleware', () => { categoryIds, } try { - await expect(mutate({ mutation: createPostMutation, variables })).resolves.toMatchObject({ + await expect( + mutate({ mutation: createPostMutation, variables }), + ).resolves.toMatchObject({ errors: [ { message: 'Post with this slug already exists!', @@ -131,7 +133,7 @@ describe('slugifyMiddleware', () => { ], }) done() - } catch(error) { + } catch (error) { throw new Error(` ${error}