Skip to content

Commit

Permalink
refactor: put test executor in an isolated module for better execution
Browse files Browse the repository at this point in the history
  • Loading branch information
m2rads committed Oct 31, 2024
1 parent e42dbe4 commit 7290924
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
21 changes: 21 additions & 0 deletions packages/shortest/src/core/executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Reporter } from './reporter';
import { TestCompiler } from './compiler';

export class TestExecutor {
private reporter: Reporter;
private compiler: TestCompiler;

constructor() {
this.reporter = new Reporter();
this.compiler = new TestCompiler();
}

async executeTest(file: string) {
this.reporter.startFile(file);
this.reporter.reportTest('Sample test');
}

getReporter(): Reporter {
return this.reporter;
}
}
34 changes: 16 additions & 18 deletions packages/shortest/src/core/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import { glob } from 'glob';
import { resolve } from 'path';
import type { ShortestConfig } from '../config/types';
import { defaultConfig } from '../config/types';
import { Reporter } from './reporter';
import { TestExecutor } from './executor';
import { TestCompiler } from './compiler';

export class TestRunner {
private config!: ShortestConfig;
private cwd: string;
private reporter: Reporter;
private exitOnSuccess: boolean;
private compiler: TestCompiler;
private executor: TestExecutor;

constructor(cwd: string, exitOnSuccess = true) {
this.cwd = cwd;
this.reporter = new Reporter();
this.exitOnSuccess = exitOnSuccess;
this.compiler = new TestCompiler();
this.executor = new TestExecutor();
}

async initialize() {
Expand Down Expand Up @@ -82,11 +82,6 @@ export class TestRunner {
return files;
}

private async executeTest(file: string) {
this.reporter.startFile(file);
this.reporter.reportTest('Sample test');
}

async runFile(pattern: string) {
await this.initialize();
const files = await this.findTestFiles(pattern);
Expand All @@ -97,12 +92,13 @@ export class TestRunner {
}

for (const file of files) {
await this.executeTest(file);
await this.executor.executeTest(file);
}

this.reporter.summary();
const reporter = this.executor.getReporter();
reporter.summary();

if (this.exitOnSuccess && this.reporter.allTestsPassed()) {
if (this.exitOnSuccess && reporter.allTestsPassed()) {
process.exit(0);
}

Expand All @@ -114,29 +110,31 @@ export class TestRunner {
const files = await this.findTestFiles();

for (const file of files) {
await this.executeTest(file);
await this.executor.executeTest(file);
}

this.reporter.summary();
const reporter = this.executor.getReporter();
reporter.summary();

if (this.exitOnSuccess && this.reporter.allTestsPassed()) {
if (this.exitOnSuccess && reporter.allTestsPassed()) {
process.exit(0);
}

this.watchMode(files);
}

private watchMode(files: string[]) {
this.reporter.watchMode();
const reporter = this.executor.getReporter();
reporter.watchMode();

const watcher = watch(files, {
ignoreInitial: true
});

watcher.on('change', async (file) => {
this.reporter.fileChanged(file);
await this.executeTest(file);
this.reporter.summary();
reporter.fileChanged(file);
await this.executor.executeTest(file);
reporter.summary();
});
}
}

0 comments on commit 7290924

Please sign in to comment.