Ghost/ghost/core/test/utils/overrides.js
Simon Backx 098f4353a7
Disabled network retries for webmentions in tests (#18269)
refs https://ghost.slack.com/archives/C02G9E68C/p1695296293667689

- We block all outgoing networking by default in tests. When editing a
post/page, Ghost tries to send a webmention. Because of my earlier
changes
(1e3232cf82)
it tries 3 times - all network requests fail instantly when networking
is disabled - but it adds a delay in between those retries. So my change
disables retries during tests.
- Adds a warning when afterEach hook tooks longer than 2s due to
awaiting jobs/events
- We reduce the amount of webmentions that are send, by only sending
webmentions for added or removed urls, no longer when a post html is
changed (unless post is published/removed).
2023-09-21 16:17:05 +02:00

46 lines
1.5 KiB
JavaScript

process.env.NODE_ENV = process.env.NODE_ENV || 'testing';
process.env.WEBHOOK_SECRET = process.env.WEBHOOK_SECRET || 'TEST_STRIPE_WEBHOOK_SECRET';
require('../../core/server/overrides');
const {mochaHooks} = require('@tryghost/express-test').snapshot;
exports.mochaHooks = mochaHooks;
const chalk = require('chalk');
const mockManager = require('./e2e-framework-mock-manager');
const originalBeforeAll = mochaHooks.beforeAll;
mochaHooks.beforeAll = async function () {
if (originalBeforeAll) {
await originalBeforeAll();
}
// Disable network in tests to prevent any accidental requests
mockManager.disableNetwork();
};
const originalAfterEach = mochaHooks.afterEach;
mochaHooks.afterEach = async function () {
const domainEvents = require('@tryghost/domain-events');
const mentionsJobsService = require('../../core/server/services/mentions-jobs');
const jobsService = require('../../core/server/services/jobs');
let timeout = setTimeout(() => {
// eslint-disable-next-line no-console
console.error(chalk.yellow('\n[SLOW TEST] It takes longer than 2s to wait for all jobs and events to settle in the afterEach hook\n'));
}, 2000);
await domainEvents.allSettled();
await mentionsJobsService.allSettled();
await jobsService.allSettled();
// Last time for events emitted during jobs
await domainEvents.allSettled();
clearTimeout(timeout);
if (originalAfterEach) {
await originalAfterEach();
}
};