Skip to content

Commit

Permalink
Globalise mocha and runner instances (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgalvarez authored and Alhadis committed Feb 8, 2018
1 parent 1b05610 commit d2e82ec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
17 changes: 9 additions & 8 deletions lib/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
const {remote, ipcRenderer} = require("electron");
const EVENT_CLOSE_PROJECT = "atom-mocha:close-project";
const EVENT_JUMP_TO_FILE = "atom-mocha:jump-to-file";
const VAR_CMD_MANAGER = 'AtomMochaCmdManager';


class IPC{

init(projectPath){
global.AtomMocha = {};
global[ VAR_CMD_MANAGER ] = {};
this.projectPath = projectPath;
this.injectHooks();
}
Expand Down Expand Up @@ -36,14 +37,14 @@ class IPC{


getInjectedCode(path){
return "if(global.atom && null == global.AtomMocha && " +
`-1 !== global.atom.project.getPaths().indexOf("${ path }")){
return `if(global.atom && null == global.${ VAR_CMD_MANAGER } &&
-1 !== global.atom.project.getPaths().indexOf("${ path }")){
const {remote, ipcRenderer} = require("electron");
const {Range} = require("atom");
const AtomMocha = {
const ${ VAR_CMD_MANAGER } = {
handleJump(event, ...args){
AtomMocha.jumpToFile(...args);
${ VAR_CMD_MANAGER }.jumpToFile(...args);
},
jumpToFile(file, row, col){
atom.project.contains(file) && atom.workspace.open(file).then(editor => {
Expand All @@ -56,11 +57,11 @@ class IPC{
}
};
global.AtomMocha = AtomMocha;
remote.ipcMain.on("${ EVENT_JUMP_TO_FILE }", AtomMocha.handleJump);
global.${ VAR_CMD_MANAGER } = ${ VAR_CMD_MANAGER };
remote.ipcMain.on("${ EVENT_JUMP_TO_FILE }", ${ VAR_CMD_MANAGER }.handleJump);
window.addEventListener("beforeunload", () => {
ipcRenderer.send("${ EVENT_CLOSE_PROJECT }", "${ path }");
remote.ipcMain.removeListener("${ EVENT_JUMP_TO_FILE }", AtomMocha.handleJump);
remote.ipcMain.removeListener("${ EVENT_JUMP_TO_FILE }", ${ VAR_CMD_MANAGER }.handleJump);
});
}`;
}
Expand Down
11 changes: 7 additions & 4 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ class AtomMocha{
this.options.noExtensions || require("./extensions");

// Talk to Mocha
this.runner = new Mocha(this.options);
this.reporter = this.runner._reporter;
this.mocha = new Mocha(this.options);
this.reporter = this.mocha._reporter;

// Tell it what specs to load
this.files.forEach(s => this.runner.addFile(s));
this.files.forEach(s => this.mocha.addFile(s));

// Finally, initialise Atom's test environment
this.setup(args);

// Globalise `AtomMocha` instance.
global.AtomMocha = this;
}


Expand Down Expand Up @@ -517,7 +520,7 @@ class AtomMocha{
run = run.then(this.attachAssets(this.options));

return run.then(_=> new Promise(resolve =>
this.runner.run(failures => resolve(failures))
(this.runner = this.mocha.run(failures => resolve(failures)))
));
}
}
Expand Down

0 comments on commit d2e82ec

Please sign in to comment.