Skip to content

Commit

Permalink
prototype jest watch plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jperl committed Apr 3, 2020
1 parent 2b5c392 commit d59016e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 14 deletions.
1 change: 1 addition & 0 deletions qawolf.config.js
@@ -1,3 +1,4 @@
module.exports = {
config: 'node_modules/qawolf/ts-jest.config.json',
rootDir: 'test/.qawolf',
};
20 changes: 10 additions & 10 deletions src/cli/createCommand.ts
@@ -1,7 +1,7 @@
import program, { Command } from 'commander';
import { loadConfig } from '../config';
import { parseUrl } from './parseUrl';
import { RunServer } from '../run/RunServer';
// import { RunServer } from '../run/RunServer';
import { saveTemplate } from './saveTemplate';

export type CreateOptions = {
Expand Down Expand Up @@ -29,15 +29,15 @@ export const runCreate = async (options: CreateOptions): Promise<void> => {
return;
}

await RunServer.start({
codePath,
config,
env: {
QAW_CREATE: 'true',
QAW_HEADLESS: 'false',
},
watch: true,
});
// await RunServer.start({
// codePath,
// config,
// env: {
// QAW_CREATE: 'true',
// QAW_HEADLESS: 'false',
// },
// watch: true,
// });
};

export const buildCreateCommand = (): program.Command => {
Expand Down
4 changes: 1 addition & 3 deletions src/create-code/CreateManager.ts
Expand Up @@ -122,14 +122,12 @@ export class CreateManager {

public async finalize(): Promise<void> {
const shouldSave = await promptSaveRepl(this._codeUpdater.path());

if (shouldSave) {
await this._codeUpdater.finalize();
} else {
await this._codeUpdater.discard();
await this._selectorUpdater.discard();
}

Run.stopRunner();
// Run.stopRunner();
}
}
66 changes: 66 additions & 0 deletions src/watchPlugin.ts
@@ -0,0 +1,66 @@
import { AddressInfo, createServer, Server, Socket } from 'net';
// import split from 'split';

class WatchPlugin {
private _ready: Promise<void>;
private _server: Server;
private _socket: Socket;

constructor() {
this._server = createServer((socket) => {
this._setSocket(socket);
});

this._ready = new Promise((resolve) => {
this._server.listen(0, () => resolve());
});
}

_setSocket(socket: Socket) {
this._socket = socket;

// this._socket.pipe(split()).on('data', (data: string) => {
// debug('received: %s', data);

// try {
// const message = JSON.parse(data);

// // if (message.name === 'codeupdate') {
// // this.emit('codeupdate', message.code);
// // } else if (message.name === 'stopped') {
// // this.emit('stopped');
// // } else if (message.name === 'stoprunner') {
// // this.emit('stoprunner');
// // }
// } catch (e) {
// // ignore non JSON messages (last empty message)
// }
// });
}

apply(jestHooks) {
jestHooks.shouldRunTestSuite(async () => {
await this._ready;
const port = (this._server.address() as AddressInfo).port;
process.env.QAW_RUN_SERVER_PORT = `${port}`;

if (this._socket) {
try {
this._socket.write(JSON.stringify({ name: 'stop' }) + '\n');
} catch (e) {}
}

return true;
});

jestHooks.onTestRunComplete(() => {
// console.log('results', results);
});
}

getUsageInfo() {}

run() {}
}

module.exports = WatchPlugin;
3 changes: 2 additions & 1 deletion ts-jest.config.json
Expand Up @@ -4,5 +4,6 @@
"ts-jest": {
"diagnostics": false
}
}
},
"watchPlugins": ["/Users/jon/dev/qawolf/build/watchPlugin.js"]
}

0 comments on commit d59016e

Please sign in to comment.