Cleaned test utils exposing fixtures

- This is actually a really useful part of our test framework
- Rewrite it to be cleaner and clearer what does
- Add timing function to see if its worth optimising this later
This commit is contained in:
Hannah Wolfe 2021-02-18 14:22:05 +00:00
parent 9f718750b4
commit d6a319aa3e

View File

@ -115,31 +115,34 @@ const createEmailedPost = async function createEmailedPost({postOptions, emailOp
let ghostServer; let ghostServer;
const dirtyDataFunction = () => { /**
/** * Because we use ObjectID we don't know the ID of fixtures ahead of time
* @TODO: this is dirty, but makes routing testing a lot easier for now, because the routing test * This function fetches all of our fixtures and exposes them so that tests can use them
* has no easy way to access existing resource id's, which are added from the Ghost fixtures. * @TODO: Optimise this by making it optional / selective
* I can do `testUtils.existingData.roles[0].id`. */
*/ const exposeFixtures = async () => {
console.time('Expose fixtures'); // eslint-disable-line no-console
const fixturePromises = {
roles: models.Role.findAll({columns: ['id']}),
users: models.User.findAll({columns: ['id', 'email']}),
tags: models.Tag.findAll({columns: ['id']}),
apiKeys: models.ApiKey.findAll({withRelated: 'integration'})
};
const keys = Object.keys(fixturePromises);
module.exports.existingData = {}; module.exports.existingData = {};
return models.Role.findAll({columns: ['id']})
.then((roles) => {
module.exports.existingData.roles = roles.toJSON();
return models.User.findAll({columns: ['id', 'email']}); return Promise
}) .all(Object.values(fixturePromises))
.then((users) => { .then((results) => {
module.exports.existingData.users = users.toJSON(context.internal); for (let i = 0; i < keys.length; i += 1) {
module.exports.existingData[keys[i]] = results[i].toJSON(context.internal);
}
return models.Tag.findAll({columns: ['id']}); console.timeEnd('Expose fixtures'); // eslint-disable-line no-console
}) })
.then((tags) => { .catch((err) => {
module.exports.existingData.tags = tags.toJSON(); console.error('Unable to expose fixtures', err); // eslint-disable-line no-console
process.exit(1);
return models.ApiKey.findAll({withRelated: 'integration'});
})
.then((keys) => {
module.exports.existingData.apiKeys = keys.toJSON(context.internal);
}); });
}; };
@ -223,8 +226,8 @@ const startGhost = async function startGhost(options) {
// Trigger server start, which is ONLY used for theme reload // Trigger server start, which is ONLY used for theme reload
events.emit('server.start'); events.emit('server.start');
// Expose some data, wrap-up and return // Expose fixture data, wrap-up and return
await dirtyDataFunction(); await exposeFixtures();
console.log('Restart Mode'); // eslint-disable-line no-console console.log('Restart Mode'); // eslint-disable-line no-console
console.timeEnd('Start Ghost'); // eslint-disable-line no-console console.timeEnd('Start Ghost'); // eslint-disable-line no-console
@ -281,8 +284,8 @@ const startGhost = async function startGhost(options) {
// Wait for the URL service to be ready, which happens after boot, but don't re-trigger db.ready // Wait for the URL service to be ready, which happens after boot, but don't re-trigger db.ready
await urlServiceUtils.isFinished({disableDbReadyEvent: true}); await urlServiceUtils.isFinished({disableDbReadyEvent: true});
// Expose some data, wrap-up and return // Expose fixture data, wrap-up and return
await dirtyDataFunction(); await exposeFixtures();
console.log('Fresh Start Mode'); // eslint-disable-line no-console console.log('Fresh Start Mode'); // eslint-disable-line no-console
console.timeEnd('Start Ghost'); // eslint-disable-line no-console console.timeEnd('Start Ghost'); // eslint-disable-line no-console
return ghostServer; return ghostServer;