Skip to content

Commit

Permalink
Decouple integration tests from JSON file, undo error code change (#150)
Browse files Browse the repository at this point in the history
* Return to error code 1 instead of 2

* Decouple integration tests from transient config file, and assert that server stops successfully

* Return `PORT_FOR_SERVICE_UNDER_TEST` to `PORT` for now

* Fix port type treatment
  • Loading branch information
danyalaytekin committed Nov 10, 2023
1 parent 59bf0f9 commit 3eabe7f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
18 changes: 9 additions & 9 deletions test/integration/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@
'use strict';

const app = require('../../app');
const config = require('../../config/test.json');
const createNavigator = require('./helper/navigate');
const loadFixtures = require('../../data/fixture/load');
const request = require('request');

const errorCodeForNoService = 2;
const config = {
database: process.env.DATABASE || 'mongodb://127.0.0.1/pa11y-webservice-test',
host: process.env.HOST || '0.0.0.0',
port: Number(process.env.PORT) || 3000
};

// Run before all tests
before(function(done) {
this.baseUrl = `http://localhost:${config.port}/`;
this.baseUrl = `http://${config.host}:${config.port}/`;
this.app = null;
this.last = {};
this.navigate = createNavigator(this.baseUrl, this.last);

assertTestAppIsRunning(this.baseUrl, () => {
assertServiceIsAvailable(this.baseUrl, () => {
config.dbOnly = true;
app(config, (error, initialisedApp) => {
this.app = initialisedApp;
Expand All @@ -38,17 +40,15 @@ before(function(done) {
});
});

// Run after each test
afterEach(done => {
loadFixtures('test', config, done);
});

// Check that the test application is running, and exit if not
function assertTestAppIsRunning(baseUrl, done) {
function assertServiceIsAvailable(baseUrl, done) {
request(baseUrl, error => {
if (error) {
console.error(`Error: Test app not started. NODE_ENV was ${process.env.NODE_ENV}; run with \`NODE_ENV=test node index.js\``);
process.exit(errorCodeForNoService);
process.exit();
}
done();
});
Expand Down
29 changes: 11 additions & 18 deletions test/integration/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,21 @@
// along with Pa11y Webservice. If not, see <http://www.gnu.org/licenses/>.
'use strict';

const util = require('util');
const assert = require('proclaim');
const config = require('../../config/test.json');
const app = require('../../app');

describe('pa11y-service startup', function() {
const config = {
database: process.env.DATABASE || 'mongodb://127.0.0.1/pa11y-webservice-test',
host: process.env.HOST || '0.0.0.0',
port: Number(process.env.PORT_FOR_SPINUP_TEST) || 3010
};

it('should start the service and call the callback', done => {
const modifiedConfig = {
database: config.database,
host: config.host,
port: config.port + 10
};
describe('pa11y-webservice lifecycle', function() {
it('should start and stop the service', async () => {
const service = await util.promisify(app)(config);
assert.isDefined(service);

app(modifiedConfig, (error, webservice) => {
assert.isNull(error);
assert.notStrictEqual(webservice, undefined);

webservice.server.stop();

done();
});
await service.server.stop();
});
});


0 comments on commit 3eabe7f

Please sign in to comment.