Skip to content

Commit 3bdeecf

Browse files
committed
terminal tests and fixes
1 parent 35a27b1 commit 3bdeecf

4 files changed

Lines changed: 353 additions & 7 deletions

File tree

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,15 @@
166166
"build:tsc": "yarn build:tsc:cjs && yarn build:tsc:esm",
167167
"build:tsc:cjs": "tsc -p ./tsconfig.cjs.json",
168168
"build:tsc:esm": "tsc -p ./tsconfig.esm.json",
169-
"test": "nyc ts-mocha -r tsx tests/*.test.ts",
169+
"test": "yarn test:env nyc ts-mocha -r tsx tests/*.test.ts",
170+
"test:env": "NODE_OPTIONS=\"--disable-warning=ExperimentalWarning --experimental-loader @istanbuljs/esm-loader-hook\"",
170171
"report": "nyc report -r lcov"
171172
},
172173
"dependencies": {
173174
"@inquirer/prompts": "7.1.0"
174175
},
175176
"devDependencies": {
177+
"@istanbuljs/esm-loader-hook": "0.3.0",
176178
"@types/chai": "4.3.20",
177179
"@types/mocha": "10.0.10",
178180
"@types/node": "22.9.3",

src/router/Terminal.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//modules
22
import { input } from '@inquirer/prompts';
3+
//lib
4+
import type { TypeOf } from '../types.js';
35
//data
46
import { objectFromArgs } from '../data/Nest.js';
57
//local
@@ -144,7 +146,7 @@ export default class Terminal<R = unknown, S = unknown>
144146
/**
145147
* Retrieves the first value found from the given flag/s in cli
146148
*/
147-
public expect<T>(flags: string[], defaults: T) {
149+
public expect<T>(flags: string[], defaults: TypeOf<T>) {
148150
for (const flag of flags) {
149151
if (this.data[flag]) {
150152
return this.data[flag] as T;
@@ -156,9 +158,9 @@ export default class Terminal<R = unknown, S = unknown>
156158
/**
157159
* Runs the command
158160
*/
159-
public run() {
161+
public run<T = unknown>() {
160162
const req = this.request({ data: this.data });
161163
const res = this.response();
162-
return this.emit(this.command, req, res);
164+
return this.resolve<T>(this.command, req, res);
163165
}
164166
}

tests/Terminal.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { describe, it } from 'mocha';
2+
import { expect } from 'chai';
3+
//NOTE: no extensions in tests because it's excluded in tsconfig.json and
4+
//we are testing in a typescript environment via `ts-mocha -r tsx` (esm)
5+
import Terminal from '../src/router/Terminal';
6+
7+
describe('Terminal Tests', () => {
8+
it('Should get args', async () => {
9+
const terminal = new Terminal(['command', '--i', 'value']);
10+
expect(terminal.args[0]).to.equal('--i');
11+
expect(terminal.args[1]).to.equal('value');
12+
})
13+
it('Should get brand', async () => {
14+
const terminal = new Terminal([], '[test]');
15+
expect(terminal.brand).to.equal('[test]');
16+
})
17+
it('Should configure controls', async () => {
18+
const terminal = new Terminal(['command', '--i', 'value'], '[test]');
19+
const control = terminal.control;
20+
expect(control.brand).to.equal('[test]');
21+
expect(typeof control.error).to.equal('function');
22+
expect(typeof control.success).to.equal('function');
23+
expect(typeof control.warning).to.equal('function');
24+
expect(typeof control.info).to.equal('function');
25+
expect(typeof control.output).to.equal('function');
26+
expect(typeof control.input).to.equal('function');
27+
})
28+
it('Should parse data', async () => {
29+
const terminal = new Terminal(['command', '--i', 'value']);
30+
expect(terminal.data.i).to.equal('value');
31+
})
32+
it('Should expect arguments', async () => {
33+
const terminal = new Terminal(['command', '--i', 'value']);
34+
const input = terminal.expect(['i', 'input'], 'default');
35+
expect(input).to.equal('value');
36+
})
37+
it('Should run command successfully', async () => {
38+
const terminal = new Terminal(['command', '--i', 'value']);
39+
terminal.on('command', (req, res) => {
40+
res.setResults(req.data());
41+
})
42+
const response = await terminal.run<{ i: string }>();
43+
expect(response.code).to.equal(200);
44+
expect(response.status).to.equal('OK');
45+
expect(response.total).to.equal(1);
46+
expect(response.results?.i).to.equal('value');
47+
})
48+
it('Should run command then error', async () => {
49+
const terminal = new Terminal(['command', '--i', 'value']);
50+
terminal.on('command', (req, res) => {
51+
res.setError('error message');
52+
})
53+
const response = await terminal.run<{ i: string }>();
54+
expect(response.code).to.equal(400);
55+
expect(response.status).to.equal('Bad Request');
56+
expect(response.error).to.equal('error message');
57+
})
58+
})

0 commit comments

Comments
 (0)