Ghost/ghost/core/test/unit/server/notify.test.js
Simon Backx 8f8ca481a6
Fixed configUtils and adapter cache issues in E2E tests (#16167)
no issue

There are a couple of issues with resetting the Ghost instance between
E2E test files:

These issues came to the surface because of new tests written in
https://github.com/TryGhost/Ghost/pull/16117

**1. configUtils.restore does not work correctly**
`config.reset()` is a callback based method. On top of that, it doesn't
really work reliably (https://github.com/indexzero/nconf/issues/93)

What kinda happens, is that you first call `config.reset` but
immediately after you correcty reset the config using the `config.set`
calls afterwards. But since `config.reset` is async, that reset will
happen after all those sets, and the end result is that it isn't reset
correctly.

This mainly caused issues in the new updated images tests, which were
updating the config `imageOptimization.contentImageSizes`, which is a
deeply nested config value. Maybe some references to objects are reused
in nconf that cause this issue?

Wrapping `config.reset()` in a promise does fix the issue.

**2. Adapters cache not reset between tests**
At the start of each test, we set `paths:contentPath` to a nice new
temporary directory. But if a previous test already requests a
localStorage adapter, that adapter would have been created and in the
constructor `paths:contentPath` would have been passed. That same
instance will be reused in the next test run. So it won't read the new
config again. To fix this, we need to reset the adapter instances
between E2E tests.

How was this visible? Test uploads were stored in the actual git
repository, and not in a temporary directory. When writing the new image
upload tests, this also resulted in unreliable test runs because some
image names were already taken (from previous test runs).

**3. Old 2E2 test Ghost server not stopped**
Sometimes we still need access to the frontend test server using
`getAgentsWithFrontend`. But that does start a new Ghost server which is
actually listening for HTTP traffic. This could result in a fatal error
in tests because the port is already in use. The issue is that old E2E
tests also start a HTTP server, but they don't stop the server. When you
used the old `startGhost` util, it would check if a server was already
running and stop it first. The new `getAgentsWithFrontend` now also has
the same functionality to fix that issue.
2023-01-30 14:06:20 +01:00

105 lines
3.9 KiB
JavaScript

const should = require('should');
const sinon = require('sinon');
const configUtils = require('../../utils/configUtils');
const events = require('../../../core/server/lib/common/events');
const bootstrapSocket = require('@tryghost/bootstrap-socket');
describe('Notify', function () {
describe('notifyServerStarted', function () {
let notify;
let socketStub;
let eventSpy;
beforeEach(function () {
// Have to re-require each time to clear the internal flag
delete require.cache[require.resolve('../../../core/server/notify')];
notify = require('../../../core/server/notify');
// process.send isn't set for tests, we can safely override;
process.send = sinon.stub();
// stub socket connectAndSend method
socketStub = sinon.stub(bootstrapSocket, 'connectAndSend');
// Spy for the events that get called
eventSpy = sinon.spy(events, 'emit');
});
afterEach(async function () {
process.send = undefined;
await configUtils.restore();
socketStub.restore();
eventSpy.restore();
});
it('it resolves a promise', function () {
notify.notifyServerStarted().should.be.fulfilled();
});
it('it communicates with IPC correctly on success', function () {
notify.notifyServerStarted();
process.send.calledOnce.should.be.true();
let message = process.send.firstCall.args[0];
message.should.be.an.Object().with.properties('started', 'debug');
message.should.not.have.property('error');
message.started.should.be.true();
});
it('communicates with IPC correctly on failure', function () {
notify.notifyServerStarted(new Error('something went wrong'));
process.send.calledOnce.should.be.true();
let message = process.send.firstCall.args[0];
message.should.be.an.Object().with.properties('started', 'debug', 'error');
message.started.should.be.false();
message.error.should.be.an.Object().with.properties('message');
message.error.message.should.eql('something went wrong');
});
it('communicates via bootstrap socket correctly on success', function () {
configUtils.set('bootstrap-socket', 'testing');
notify.notifyServerStarted();
socketStub.calledOnce.should.be.true();
socketStub.firstCall.args[0].should.eql('testing');
let message = socketStub.firstCall.args[1];
message.should.be.an.Object().with.properties('started', 'debug');
message.should.not.have.property('error');
message.started.should.be.true();
});
it('communicates via bootstrap socket correctly on failure', function () {
configUtils.set('bootstrap-socket', 'testing');
notify.notifyServerStarted(new Error('something went wrong'));
socketStub.calledOnce.should.be.true();
socketStub.firstCall.args[0].should.eql('testing');
let message = socketStub.firstCall.args[1];
message.should.be.an.Object().with.properties('started', 'debug', 'error');
message.started.should.be.false();
message.error.should.be.an.Object().with.properties('message');
message.error.message.should.eql('something went wrong');
});
it('can be called multiple times, but only communicates once', function () {
configUtils.set('bootstrap-socket', 'testing');
notify.notifyServerStarted();
notify.notifyServerStarted(new Error('something went wrong'));
notify.notifyServerStarted();
process.send.calledOnce.should.be.true();
socketStub.calledOnce.should.be.true();
});
});
});