-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
repl.spec.ts
233 lines (190 loc) · 5.96 KB
/
repl.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { clc } from '@nestjs/common/utils/cli-colors.util';
import { repl } from '@nestjs/core';
import { ReplContext } from '@nestjs/core/repl/repl-context';
import {
HelpReplFn,
GetReplFn,
ResolveReplFn,
SelectReplFn,
DebugReplFn,
MethodsReplFn,
} from '@nestjs/core/repl/native-functions';
import { expect } from 'chai';
import * as sinon from 'sinon';
import { AppModule } from '../src/app.module';
const PROMPT = '\u001b[1G\u001b[0J> \u001b[3G';
describe('REPL', () => {
beforeEach(() => {
// To avoid coloring the output:
sinon.stub(clc, 'bold').callsFake(text => text);
sinon.stub(clc, 'green').callsFake(text => text);
sinon.stub(clc, 'yellow').callsFake(text => text);
sinon.stub(clc, 'red').callsFake(text => text);
sinon.stub(clc, 'magentaBright').callsFake(text => text);
sinon.stub(clc, 'cyanBright').callsFake(text => text);
});
afterEach(() => {
sinon.restore();
});
it('get()', async () => {
const server = await repl(AppModule);
server.context
let outputText = '';
sinon.stub(process.stdout, 'write').callsFake(text => {
outputText += text;
return true;
});
server.emit('line', 'get(UsersService)');
expect(outputText).to.equal(
`UsersService { usersRepository: UsersRepository {} }
${PROMPT}`,
);
outputText = '';
server.emit('line', 'get(UsersService).findAll()');
expect(outputText).to
.equal(`\u001b[32m'This action returns all users'\u001b[39m
${PROMPT}`);
outputText = '';
server.emit('line', 'get(UsersRepository)');
expect(outputText).to.equal(`UsersRepository {}
${PROMPT}`);
});
it('debug()', async () => {
const server = await repl(AppModule);
let outputText = '';
sinon.stub(process.stdout, 'write').callsFake(text => {
outputText += text;
return true;
});
server.emit('line', 'debug(UsersModule)');
expect(outputText).to.equal(
`
UsersModule:
- controllers:
◻ UsersController
- providers:
◻ UsersService
◻ UsersRepository
${PROMPT}`,
);
});
it('methods()', async () => {
const server = await repl(AppModule);
let outputText = '';
sinon.stub(process.stdout, 'write').callsFake(text => {
outputText += text;
return true;
});
server.emit('line', 'methods(UsersRepository)');
expect(outputText).to.equal(
`
Methods:
◻ find
${PROMPT}`,
);
outputText = '';
server.emit('line', 'methods(UsersService)');
expect(outputText).to.equal(
`
Methods:
◻ create
◻ findAll
◻ findOne
◻ update
◻ remove
${PROMPT}`,
);
});
describe('<native_function>.help', () => {
it(`Typing "help.help" should print function's description and interface`, async () => {
const replServer = await repl(AppModule);
const { description, signature } = new HelpReplFn(
sinon.stub() as unknown as ReplContext,
).fnDefinition;
let outputText = '';
sinon.stub(process.stdout, 'write').callsFake(text => {
outputText += text;
return true;
});
replServer.emit('line', 'help.help');
expect(outputText).to.equal(`${description}
Interface: help${signature}
${PROMPT}`);
});
it(`Typing "get.help" should print function's description and interface`, async () => {
const replServer = await repl(AppModule);
const { description, signature } = new GetReplFn(
sinon.stub() as unknown as ReplContext,
).fnDefinition;
let outputText = '';
sinon.stub(process.stdout, 'write').callsFake(text => {
outputText += text;
return true;
});
replServer.emit('line', 'get.help');
expect(outputText).to.equal(`${description}
Interface: get${signature}
${PROMPT}`);
});
it(`Typing "resolve.help" should print function's description and interface`, async () => {
const replServer = await repl(AppModule);
const { description, signature } = new ResolveReplFn(
sinon.stub() as unknown as ReplContext,
).fnDefinition;
let outputText = '';
sinon.stub(process.stdout, 'write').callsFake(text => {
outputText += text;
return true;
});
replServer.emit('line', 'resolve.help');
expect(outputText).to.equal(`${description}
Interface: resolve${signature}
${PROMPT}`);
});
it(`Typing "select.help" should print function's description and interface`, async () => {
const replServer = await repl(AppModule);
const { description, signature } = new SelectReplFn(
sinon.stub() as unknown as ReplContext,
).fnDefinition;
let outputText = '';
sinon.stub(process.stdout, 'write').callsFake(text => {
outputText += text;
return true;
});
replServer.emit('line', 'select.help');
expect(outputText).to.equal(`${description}
Interface: select${signature}
${PROMPT}`);
});
it(`Typing "debug.help" should print function's description and interface`, async () => {
const replServer = await repl(AppModule);
const { description, signature } = new DebugReplFn(
sinon.stub() as unknown as ReplContext,
).fnDefinition;
let outputText = '';
sinon.stub(process.stdout, 'write').callsFake(text => {
outputText += text;
return true;
});
replServer.emit('line', 'debug.help');
expect(outputText).to.equal(`${description}
Interface: debug${signature}
${PROMPT}`);
});
it(`Typing "methods.help" should print function's description and interface`, async () => {
const replServer = await repl(AppModule);
const { description, signature } = new MethodsReplFn(
sinon.stub() as unknown as ReplContext,
).fnDefinition;
let outputText = '';
sinon.stub(process.stdout, 'write').callsFake(text => {
outputText += text;
return true;
});
replServer.emit('line', 'methods.help');
expect(outputText).to.equal(`${description}
Interface: methods${signature}
${PROMPT}`);
});
});
});