From ba50241fde86e299ff3f26349efb9dc3d195ba97 Mon Sep 17 00:00:00 2001 From: Naz Date: Mon, 24 May 2021 13:11:33 +0400 Subject: [PATCH] Refactored scheduling tests setup using async/await refs https://github.com/TryGhost/Team/issues/694 - The previous `.then` chaining was outdated and was causing 2x ghost instance initialization per test suite - With a refactor there's only one intance initialization per suite (saves running time!) and we use more readable async/await syntax, which should make things more maintainable --- .../api/canary/admin/schedules_spec.js | 106 +++++++++--------- .../regression/api/v2/admin/schedules_spec.js | 106 +++++++++--------- .../regression/api/v3/admin/schedules_spec.js | 106 +++++++++--------- 3 files changed, 150 insertions(+), 168 deletions(-) diff --git a/test/regression/api/canary/admin/schedules_spec.js b/test/regression/api/canary/admin/schedules_spec.js index 52c6ab2fa9..cda139a50c 100644 --- a/test/regression/api/canary/admin/schedules_spec.js +++ b/test/regression/api/canary/admin/schedules_spec.js @@ -27,68 +27,62 @@ describe('Canary Schedules API', function () { sinon.restore(); }); - before(function () { - return ghost() - .then(() => { - request = supertest.agent(config.get('url')); - }); - }); + before(async function () { + await ghost(); - before(function () { - return ghost() - .then(function () { - resources.push(testUtils.DataGenerator.forKnex.createPost({ - created_by: testUtils.existingData.users[0].id, - author_id: testUtils.existingData.users[0].id, - published_by: testUtils.existingData.users[0].id, - published_at: moment().add(30, 'seconds').toDate(), - status: 'scheduled', - slug: 'first' - })); + request = supertest.agent(config.get('url')); - resources.push(testUtils.DataGenerator.forKnex.createPost({ - created_by: testUtils.existingData.users[0].id, - author_id: testUtils.existingData.users[0].id, - published_by: testUtils.existingData.users[0].id, - published_at: moment().subtract(30, 'seconds').toDate(), - status: 'scheduled', - slug: 'second' - })); + resources.push(testUtils.DataGenerator.forKnex.createPost({ + created_by: testUtils.existingData.users[0].id, + author_id: testUtils.existingData.users[0].id, + published_by: testUtils.existingData.users[0].id, + published_at: moment().add(30, 'seconds').toDate(), + status: 'scheduled', + slug: 'first' + })); - resources.push(testUtils.DataGenerator.forKnex.createPost({ - created_by: testUtils.existingData.users[0].id, - author_id: testUtils.existingData.users[0].id, - published_by: testUtils.existingData.users[0].id, - published_at: moment().add(10, 'minute').toDate(), - status: 'scheduled', - slug: 'third' - })); + resources.push(testUtils.DataGenerator.forKnex.createPost({ + created_by: testUtils.existingData.users[0].id, + author_id: testUtils.existingData.users[0].id, + published_by: testUtils.existingData.users[0].id, + published_at: moment().subtract(30, 'seconds').toDate(), + status: 'scheduled', + slug: 'second' + })); - resources.push(testUtils.DataGenerator.forKnex.createPost({ - created_by: testUtils.existingData.users[0].id, - author_id: testUtils.existingData.users[0].id, - published_by: testUtils.existingData.users[0].id, - published_at: moment().subtract(10, 'minute').toDate(), - status: 'scheduled', - slug: 'fourth' - })); + resources.push(testUtils.DataGenerator.forKnex.createPost({ + created_by: testUtils.existingData.users[0].id, + author_id: testUtils.existingData.users[0].id, + published_by: testUtils.existingData.users[0].id, + published_at: moment().add(10, 'minute').toDate(), + status: 'scheduled', + slug: 'third' + })); - resources.push(testUtils.DataGenerator.forKnex.createPost({ - created_by: testUtils.existingData.users[0].id, - author_id: testUtils.existingData.users[0].id, - published_by: testUtils.existingData.users[0].id, - published_at: moment().add(30, 'seconds').toDate(), - status: 'scheduled', - slug: 'fifth', - type: 'page' - })); + resources.push(testUtils.DataGenerator.forKnex.createPost({ + created_by: testUtils.existingData.users[0].id, + author_id: testUtils.existingData.users[0].id, + published_by: testUtils.existingData.users[0].id, + published_at: moment().subtract(10, 'minute').toDate(), + status: 'scheduled', + slug: 'fourth' + })); - return Promise.mapSeries(resources, function (post) { - return models.Post.add(post, {context: {internal: true}}); - }).then(function (result) { - result.length.should.eql(5); - }); - }); + resources.push(testUtils.DataGenerator.forKnex.createPost({ + created_by: testUtils.existingData.users[0].id, + author_id: testUtils.existingData.users[0].id, + published_by: testUtils.existingData.users[0].id, + published_at: moment().add(30, 'seconds').toDate(), + status: 'scheduled', + slug: 'fifth', + type: 'page' + })); + + const result = await Promise.mapSeries(resources, function (post) { + return models.Post.add(post, {context: {internal: true}}); + }); + + result.length.should.eql(5); }); describe('publish', function () { diff --git a/test/regression/api/v2/admin/schedules_spec.js b/test/regression/api/v2/admin/schedules_spec.js index 8a876fe1fb..87700d6dbf 100644 --- a/test/regression/api/v2/admin/schedules_spec.js +++ b/test/regression/api/v2/admin/schedules_spec.js @@ -27,68 +27,62 @@ describe('v2 Schedules API', function () { sinon.restore(); }); - before(function () { - return ghost() - .then(() => { - request = supertest.agent(config.get('url')); - }); - }); + before(async function () { + await ghost(); - before(function () { - return ghost() - .then(function () { - resources.push(testUtils.DataGenerator.forKnex.createPost({ - created_by: testUtils.existingData.users[0].id, - author_id: testUtils.existingData.users[0].id, - published_by: testUtils.existingData.users[0].id, - published_at: moment().add(30, 'seconds').toDate(), - status: 'scheduled', - slug: 'first' - })); + request = supertest.agent(config.get('url')); - resources.push(testUtils.DataGenerator.forKnex.createPost({ - created_by: testUtils.existingData.users[0].id, - author_id: testUtils.existingData.users[0].id, - published_by: testUtils.existingData.users[0].id, - published_at: moment().subtract(30, 'seconds').toDate(), - status: 'scheduled', - slug: 'second' - })); + resources.push(testUtils.DataGenerator.forKnex.createPost({ + created_by: testUtils.existingData.users[0].id, + author_id: testUtils.existingData.users[0].id, + published_by: testUtils.existingData.users[0].id, + published_at: moment().add(30, 'seconds').toDate(), + status: 'scheduled', + slug: 'first' + })); - resources.push(testUtils.DataGenerator.forKnex.createPost({ - created_by: testUtils.existingData.users[0].id, - author_id: testUtils.existingData.users[0].id, - published_by: testUtils.existingData.users[0].id, - published_at: moment().add(10, 'minute').toDate(), - status: 'scheduled', - slug: 'third' - })); + resources.push(testUtils.DataGenerator.forKnex.createPost({ + created_by: testUtils.existingData.users[0].id, + author_id: testUtils.existingData.users[0].id, + published_by: testUtils.existingData.users[0].id, + published_at: moment().subtract(30, 'seconds').toDate(), + status: 'scheduled', + slug: 'second' + })); - resources.push(testUtils.DataGenerator.forKnex.createPost({ - created_by: testUtils.existingData.users[0].id, - author_id: testUtils.existingData.users[0].id, - published_by: testUtils.existingData.users[0].id, - published_at: moment().subtract(10, 'minute').toDate(), - status: 'scheduled', - slug: 'fourth' - })); + resources.push(testUtils.DataGenerator.forKnex.createPost({ + created_by: testUtils.existingData.users[0].id, + author_id: testUtils.existingData.users[0].id, + published_by: testUtils.existingData.users[0].id, + published_at: moment().add(10, 'minute').toDate(), + status: 'scheduled', + slug: 'third' + })); - resources.push(testUtils.DataGenerator.forKnex.createPost({ - created_by: testUtils.existingData.users[0].id, - author_id: testUtils.existingData.users[0].id, - published_by: testUtils.existingData.users[0].id, - published_at: moment().add(30, 'seconds').toDate(), - status: 'scheduled', - slug: 'fifth', - type: 'page' - })); + resources.push(testUtils.DataGenerator.forKnex.createPost({ + created_by: testUtils.existingData.users[0].id, + author_id: testUtils.existingData.users[0].id, + published_by: testUtils.existingData.users[0].id, + published_at: moment().subtract(10, 'minute').toDate(), + status: 'scheduled', + slug: 'fourth' + })); - return Promise.mapSeries(resources, function (post) { - return models.Post.add(post, {context: {internal: true}}); - }).then(function (result) { - result.length.should.eql(5); - }); - }); + resources.push(testUtils.DataGenerator.forKnex.createPost({ + created_by: testUtils.existingData.users[0].id, + author_id: testUtils.existingData.users[0].id, + published_by: testUtils.existingData.users[0].id, + published_at: moment().add(30, 'seconds').toDate(), + status: 'scheduled', + slug: 'fifth', + type: 'page' + })); + + const result = await Promise.mapSeries(resources, function (post) { + return models.Post.add(post, {context: {internal: true}}); + }); + + result.length.should.eql(5); }); describe('publish', function () { diff --git a/test/regression/api/v3/admin/schedules_spec.js b/test/regression/api/v3/admin/schedules_spec.js index a0fb7d1662..547c95627d 100644 --- a/test/regression/api/v3/admin/schedules_spec.js +++ b/test/regression/api/v3/admin/schedules_spec.js @@ -27,68 +27,62 @@ describe('v3 Schedules API', function () { sinon.restore(); }); - before(function () { - return ghost() - .then(() => { - request = supertest.agent(config.get('url')); - }); - }); + before(async function () { + await ghost(); - before(function () { - return ghost() - .then(function () { - resources.push(testUtils.DataGenerator.forKnex.createPost({ - created_by: testUtils.existingData.users[0].id, - author_id: testUtils.existingData.users[0].id, - published_by: testUtils.existingData.users[0].id, - published_at: moment().add(30, 'seconds').toDate(), - status: 'scheduled', - slug: 'first' - })); + request = supertest.agent(config.get('url')); - resources.push(testUtils.DataGenerator.forKnex.createPost({ - created_by: testUtils.existingData.users[0].id, - author_id: testUtils.existingData.users[0].id, - published_by: testUtils.existingData.users[0].id, - published_at: moment().subtract(30, 'seconds').toDate(), - status: 'scheduled', - slug: 'second' - })); + resources.push(testUtils.DataGenerator.forKnex.createPost({ + created_by: testUtils.existingData.users[0].id, + author_id: testUtils.existingData.users[0].id, + published_by: testUtils.existingData.users[0].id, + published_at: moment().add(30, 'seconds').toDate(), + status: 'scheduled', + slug: 'first' + })); - resources.push(testUtils.DataGenerator.forKnex.createPost({ - created_by: testUtils.existingData.users[0].id, - author_id: testUtils.existingData.users[0].id, - published_by: testUtils.existingData.users[0].id, - published_at: moment().add(10, 'minute').toDate(), - status: 'scheduled', - slug: 'third' - })); + resources.push(testUtils.DataGenerator.forKnex.createPost({ + created_by: testUtils.existingData.users[0].id, + author_id: testUtils.existingData.users[0].id, + published_by: testUtils.existingData.users[0].id, + published_at: moment().subtract(30, 'seconds').toDate(), + status: 'scheduled', + slug: 'second' + })); - resources.push(testUtils.DataGenerator.forKnex.createPost({ - created_by: testUtils.existingData.users[0].id, - author_id: testUtils.existingData.users[0].id, - published_by: testUtils.existingData.users[0].id, - published_at: moment().subtract(10, 'minute').toDate(), - status: 'scheduled', - slug: 'fourth' - })); + resources.push(testUtils.DataGenerator.forKnex.createPost({ + created_by: testUtils.existingData.users[0].id, + author_id: testUtils.existingData.users[0].id, + published_by: testUtils.existingData.users[0].id, + published_at: moment().add(10, 'minute').toDate(), + status: 'scheduled', + slug: 'third' + })); - resources.push(testUtils.DataGenerator.forKnex.createPost({ - created_by: testUtils.existingData.users[0].id, - author_id: testUtils.existingData.users[0].id, - published_by: testUtils.existingData.users[0].id, - published_at: moment().add(30, 'seconds').toDate(), - status: 'scheduled', - slug: 'fifth', - type: 'page' - })); + resources.push(testUtils.DataGenerator.forKnex.createPost({ + created_by: testUtils.existingData.users[0].id, + author_id: testUtils.existingData.users[0].id, + published_by: testUtils.existingData.users[0].id, + published_at: moment().subtract(10, 'minute').toDate(), + status: 'scheduled', + slug: 'fourth' + })); - return Promise.mapSeries(resources, function (post) { - return models.Post.add(post, {context: {internal: true}}); - }).then(function (result) { - result.length.should.eql(5); - }); - }); + resources.push(testUtils.DataGenerator.forKnex.createPost({ + created_by: testUtils.existingData.users[0].id, + author_id: testUtils.existingData.users[0].id, + published_by: testUtils.existingData.users[0].id, + published_at: moment().add(30, 'seconds').toDate(), + status: 'scheduled', + slug: 'fifth', + type: 'page' + })); + + const result = await Promise.mapSeries(resources, function (post) { + return models.Post.add(post, {context: {internal: true}}); + }); + + result.length.should.eql(5); }); describe('publish', function () {