From 3b7084fa8792b4dcb59f996788871d097d7765e8 Mon Sep 17 00:00:00 2001 From: Boaz Poolman Date: Thu, 19 Oct 2023 08:19:06 +0200 Subject: [PATCH] chore: Add integration test for the importOnBootstrap setting --- playground/__tests__/cli.test.js | 5 +- playground/__tests__/helpers.js | 34 +++++++++++++ .../__tests__/import-on-boostrap.test.js | 48 +++++++++++++++++++ playground/config/plugins.js | 8 ++++ 4 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 playground/__tests__/helpers.js create mode 100644 playground/__tests__/import-on-boostrap.test.js create mode 100644 playground/config/plugins.js diff --git a/playground/__tests__/cli.test.js b/playground/__tests__/cli.test.js index 322f416..8c9a86d 100644 --- a/playground/__tests__/cli.test.js +++ b/playground/__tests__/cli.test.js @@ -6,7 +6,6 @@ const exec = util.promisify(require('child_process').exec); jest.setTimeout(20000); describe('Test the config-sync CLI', () => { - afterAll(async () => { // Remove the generated files and the DB. await exec('rm -rf config/sync'); @@ -33,8 +32,8 @@ describe('Test the config-sync CLI', () => { let error; try { await exec('yarn cs diff'); - } catch(e) { - error = e; + } catch (e) { + error = e; } expect(error).toHaveProperty('code', 1); }); diff --git a/playground/__tests__/helpers.js b/playground/__tests__/helpers.js new file mode 100644 index 0000000..2012430 --- /dev/null +++ b/playground/__tests__/helpers.js @@ -0,0 +1,34 @@ +const fs = require('fs'); +const Strapi = require('@strapi/strapi'); + +let instance; + +async function setupStrapi() { + if (!instance) { + await Strapi().load(); + instance = strapi; + + await instance.server.mount(); + } + return instance; +} + +async function cleanupStrapi() { + const dbSettings = strapi.config.get('database.connection'); + + // close server to release the db-file. + await strapi.server.httpServer.close(); + + // close the connection to the database before deletion. + await strapi.db.connection.destroy(); + + // delete test database after all tests have completed. + if (dbSettings && dbSettings.connection && dbSettings.connection.filename) { + const tmpDbFile = dbSettings.connection.filename; + if (fs.existsSync(tmpDbFile)) { + fs.unlinkSync(tmpDbFile); + } + } +} + +module.exports = { setupStrapi, cleanupStrapi }; diff --git a/playground/__tests__/import-on-boostrap.test.js b/playground/__tests__/import-on-boostrap.test.js new file mode 100644 index 0000000..bab91fb --- /dev/null +++ b/playground/__tests__/import-on-boostrap.test.js @@ -0,0 +1,48 @@ +const util = require('util'); +const exec = util.promisify(require('child_process').exec); + +const { setupStrapi, cleanupStrapi } = require('./helpers'); + +jest.setTimeout(20000); + +afterAll(async () => { + // Disable importOnBootstrap + await exec('gsed -i "s/importOnBootstrap: true/importOnBootstrap: false/g" config/plugins.js'); + + await cleanupStrapi(); + await exec('rm -rf config/sync'); +}); + +describe('Test the importOnBootstrap feature', () => { + test('Without a database', async () => { + // Do the initial export and remove the database. + await exec('yarn cs export -y'); + await exec('rm -rf .tmp'); + + // Manually change the plugins.js to enable importOnBoostrap. + await exec('gsed -i "s/importOnBootstrap: false/importOnBootstrap: true/g" config/plugins.js'); + + // Start up Strapi to initiate the importOnBootstrap function. + await setupStrapi(); + + expect(strapi).toBeDefined(); + }); + + test('With a database', async () => { + // Delete any existing database and do an export. + await exec('rm -rf .tmp'); + await exec('yarn cs export -y'); + + // Manually change the plugins.js to enable importOnBoostrap. + await exec('gsed -i "s/importOnBootstrap: false/importOnBootstrap: true/g" config/plugins.js'); + + // Remove a config file to make sure the importOnBoostrap + // function actually attempts to import. + await exec('rm -rf config/sync/admin-role.strapi-editor.json'); + + // Start up Strapi to initiate the importOnBootstrap function. + await setupStrapi(); + + expect(strapi).toBeDefined(); + }); +}); diff --git a/playground/config/plugins.js b/playground/config/plugins.js new file mode 100644 index 0000000..8594e9a --- /dev/null +++ b/playground/config/plugins.js @@ -0,0 +1,8 @@ +module.exports = { + 'config-sync': { + enabled: true, + config: { + importOnBootstrap: false, + }, + }, +};