Skip to content

Commit

Permalink
fix: let it work with --runInBand
Browse files Browse the repository at this point in the history
Hack into the AdonisJS application to allow it to be restarted after
being shut down.
  • Loading branch information
targos committed Jul 19, 2021
1 parent 5e7ec55 commit b9574db
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions adonis-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ const NodeEnvironment = require('jest-environment-node');

const iocSymbol = Symbol.for('ioc.use');

let app;
let providersWithReadyHook;
let providersWithShutdownHook;

class AdonisEnvironment extends NodeEnvironment {
constructor(config, context) {
super(config, context);
this.rootDir = config.rootDir;
this.app = null;

const options = {
cache: true,
Expand All @@ -25,21 +28,30 @@ class AdonisEnvironment extends NodeEnvironment {
register(this.rootDir, options);
}

async createApplication() {
if (!app) {
const ignitor = new Ignitor(this.rootDir);
app = ignitor.application('test');
await app.setup();
await app.registerProviders();
await app.bootProviders();
await app.requirePreloads();
providersWithReadyHook = app.providersWithReadyHook;
providersWithShutdownHook = app.providersWithShutdownHook;
}
}

async setup() {
await super.setup();
const ignitor = new Ignitor(this.rootDir);
// Set by Jest
if (process.env.NODE_ENV === 'test') {
process.env.NODE_ENV = 'testing';
}
try {
const app = ignitor.application('test');
await app.setup();
await app.registerProviders();
await app.bootProviders();
await app.requirePreloads();
await this.createApplication();
app.providersWithReadyHook = providersWithReadyHook;
app.providersWithShutdownHook = providersWithShutdownHook;
await app.start();
this.app = app;
this.global[iocSymbol] = global[iocSymbol];
} catch (e) {
console.error('Error while setting up Adonis test environment');
Expand All @@ -49,7 +61,9 @@ class AdonisEnvironment extends NodeEnvironment {
}

async teardown() {
await this.app.shutdown();
await app.shutdown();
app.isShuttingDown = false;
app.state = 'booted';
await super.teardown();
}
}
Expand Down

0 comments on commit b9574db

Please sign in to comment.