Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tests #1892

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const POLY = { fetch: metarhia.metautil.fetch };
const SANDBOX = { ...COMMON_CONTEXT, ...ERRORS, ...NAMESPACES, ...POLY };

const ERR_INIT = 'Can not initialize an Application';
const ERR_TEST = 'Application tests failed';

class Application extends node.events.EventEmitter {
constructor() {
Expand All @@ -62,11 +63,13 @@ class Application extends node.events.EventEmitter {
this.domain = new Code('domain', this);

this.starts = [];
this.tests = [];
this.config = null;
this.logger = null;
this.console = null;
this.auth = null;
this.watcher = null;
this.mode = process.env.MODE || 'prod';
}

absolute(relative) {
Expand Down Expand Up @@ -111,6 +114,19 @@ class Application extends node.events.EventEmitter {
}
this.initialization = false;
this.sandbox.application.emit('initialized');
if (this.mode === 'test' && threadId === 1) this.test();
}

async test() {
setTimeout(async () => {
await this.parallel(
this.tests.map((test) =>
node.test(test.name, (t) => this.execute(test.run, t)),
),
ERR_TEST,
);
process.kill(process.pid, 'SIGINT');
}, 1000);
}

async shutdown() {
Expand Down Expand Up @@ -158,8 +174,8 @@ class Application extends node.events.EventEmitter {
return hook.router;
}

execute(method) {
return method().catch((error) => {
execute(method, ...args) {
return method(...args).catch((error) => {
const msg = `Failed to execute method: ${error?.message}`;
this.console.error(msg, error.stack);
return Promise.reject(error);
Expand Down
4 changes: 4 additions & 0 deletions lib/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ class Code extends Place {

async change(filePath) {
if (!filePath.endsWith('.js')) return;
if (filePath.startsWith('.eslint')) return;
const { application, path, name } = this;
const isTest = filePath.endsWith('.test.js');
if (isTest && application.mode !== 'test') return;
const options = { context: application.sandbox, filename: filePath };
try {
const { exports } = await metarhia.metavm.readScript(filePath, options);
const relPath = filePath.substring(path.length + 1);
const exp = name === 'bus' ? bus.prepare(exports, application) : exports;
this.set(relPath, exp);
if (isTest) application.tests.push(exp);
} catch (error) {
if (error.code !== 'ENOENT') {
application.console.error(error.stack);
Expand Down
Loading