-
Notifications
You must be signed in to change notification settings - Fork 3
/
adonis-environment.js
71 lines (61 loc) · 1.83 KB
/
adonis-environment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* eslint-disable no-console */
'use strict';
const { Ignitor } = require('@adonisjs/core/build/standalone');
const { register } = require('@adonisjs/require-ts');
const NodeEnvironment = require('jest-environment-node').TestEnvironment;
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.projectConfig.rootDir;
const options = {
cache: true,
transformers: {
before: [],
after: [{ transform: `${__dirname}/ioc-transformer.js` }],
afterDeclarations: [],
},
};
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();
// Set by Jest
if (process.env.NODE_ENV === 'test') {
process.env.NODE_ENV = 'testing';
}
try {
await this.createApplication();
app.providersWithReadyHook = providersWithReadyHook;
app.providersWithShutdownHook = providersWithShutdownHook;
await app.start();
this.global[iocSymbol] = global[iocSymbol];
} catch (e) {
console.error('Error while setting up Adonis test environment');
console.error(e);
process.exit(1);
}
}
async teardown() {
await app.shutdown();
app.isShuttingDown = false;
app.state = 'booted';
await super.teardown();
}
}
module.exports = AdonisEnvironment;