Skip to content

Commit

Permalink
Add hook tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cb1kenobi committed Oct 5, 2023
1 parent 23dc557 commit 5995656
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ export class CLI {
let data = Object.assign({}, dataPayload, {
type: name,
args,
fn: fn,
ctx: ctx
fn,
ctx
});
const callback = data.args.pop();
const pres = this.hooks.pre[name] || [];
Expand Down Expand Up @@ -525,6 +525,8 @@ export class CLI {
return;
}

// TODO: Prompt for missing required options

// // any keys in the conf object that aren't explicitly 'flags',
// // 'options', 'args', or 'subcommands' is probably a option branch
// // that changes the available flags/options
Expand Down Expand Up @@ -765,6 +767,8 @@ export class CLI {
this.applyConfig(cmdName, cmd, conf);
}

// TODO: load platform specific hooks

const { conf } = cmd;
if (conf) {
// if the command has a --platform option, then we need to load the platform-specific configuration
Expand Down
100 changes: 100 additions & 0 deletions test/cli/hook.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';
import { CLI } from '../../src/cli.js';
import { setTimeout } from 'timers/promises';

describe('CLI hooks', { concurrency: true }, () => {
it('should fire sync event hooks', async () => {
const cli = new CLI();
let fooCounter = 0;
let barCounter = 0;
let bazPreCounter = 0;
let bazPostCounter = 0;

cli.on('foo', data => {
fooCounter += data.count;
});

cli.addHook('bar', () => {
barCounter++;
});

cli.addHook('baz', {
pre() {
bazPreCounter++;
},
post() {
bazPostCounter++;
}
});

await cli.emit('foo', { count: 2 });
assert.strictEqual(fooCounter, 2);

await cli.emit('bar');
assert.strictEqual(barCounter, 1);

await cli.emit('baz');
assert.strictEqual(bazPreCounter, 1);
assert.strictEqual(bazPostCounter, 1);
});

it('should fire async event hooks', async () => {
const cli = new CLI();

let fooCounter = 0;

cli.on('foo', async data => {
await setTimeout(1);
fooCounter += data.count;
});

await cli.emit('foo', { count: 2 });
assert.strictEqual(fooCounter, 2);

let barCounter = 0;

cli.addHook('bar', async () => {
await setTimeout(1);
barCounter++;
});

await cli.emit('bar');
assert.strictEqual(barCounter, 1);
});

it('should fire function hooks', async () => {
const cli = new CLI();
let fooCounter = 0;

cli.on('foo', data => {
fooCounter += data.ctx.count;
});

const foo = cli.createHook('foo', { count: 2 }, function (x, cb) {
fooCounter += x;
fooCounter += this.count;
cb();
});
await new Promise(resolve => foo(3, resolve));
assert.strictEqual(fooCounter, 7);

let barCounter = 0;

cli.on('bar', {
pre() {
barCounter++;
},
post(data) {
barCounter += data.result[0];
}
});

const bar = cli.createHook('bar', cb => {
barCounter += 3;
cb(9);
});
await new Promise(resolve => bar(resolve));
assert.strictEqual(barCounter, 13);
});
});

0 comments on commit 5995656

Please sign in to comment.