mirror of
https://github.com/LukeHagar/vercel.git
synced 2025-12-06 21:07:47 +00:00
[cli] Add outputBuffer getter and mutable isTTY for tests (#6827)
These changes originally from #6652, but pulled out to be merged separately. `outputBuffer` is a simpler way of asserting tests against the CLI output instead of working directly withe Jest mock function. `output.isTTY` is also now mutable, so that we can write tests for both cases when the output is different based on TTY-ness (for example, see the updated `vc whoami` tests in this PR).
This commit is contained in:
@@ -20,15 +20,13 @@ export class Output {
|
|||||||
private debugEnabled: boolean;
|
private debugEnabled: boolean;
|
||||||
private spinnerMessage: string;
|
private spinnerMessage: string;
|
||||||
private _spinner: StopSpinner | null;
|
private _spinner: StopSpinner | null;
|
||||||
|
isTTY: boolean;
|
||||||
|
|
||||||
constructor({ debug: debugEnabled = false }: OutputOptions = {}) {
|
constructor({ debug: debugEnabled = false }: OutputOptions = {}) {
|
||||||
this.debugEnabled = debugEnabled;
|
this.debugEnabled = debugEnabled;
|
||||||
this.spinnerMessage = '';
|
this.spinnerMessage = '';
|
||||||
this._spinner = null;
|
this._spinner = null;
|
||||||
}
|
this.isTTY = process.stdout.isTTY || false;
|
||||||
|
|
||||||
get isTTY() {
|
|
||||||
return process.stdout.isTTY;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
isDebugEnabled = () => {
|
isDebugEnabled = () => {
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ describe('inspect', () => {
|
|||||||
client.setArgv('inspect', 'bad.com');
|
client.setArgv('inspect', 'bad.com');
|
||||||
const exitCode = await inspect(client);
|
const exitCode = await inspect(client);
|
||||||
expect(exitCode).toEqual(1);
|
expect(exitCode).toEqual(1);
|
||||||
expect(client.mockOutput.mock.calls[0][0]).toEqual(
|
expect(client.outputBuffer).toEqual(
|
||||||
`Error! Failed to find deployment "bad.com" in ${user.username}\n`
|
`Error! Failed to find deployment "bad.com" in ${user.username}\n`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,11 +6,8 @@ describe('login', () => {
|
|||||||
client.setArgv('login', '--token', 'foo');
|
client.setArgv('login', '--token', 'foo');
|
||||||
const exitCode = await login(client);
|
const exitCode = await login(client);
|
||||||
expect(exitCode).toEqual(2);
|
expect(exitCode).toEqual(2);
|
||||||
expect(client.mockOutput.mock.calls.length).toEqual(1);
|
expect(client.outputBuffer).toEqual(
|
||||||
expect(
|
'Error! `--token` may not be used with the "login" command\n'
|
||||||
client.mockOutput.mock.calls[0][0].includes(
|
);
|
||||||
'`--token` may not be used with the "login" command'
|
|
||||||
)
|
|
||||||
).toEqual(true);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -14,7 +14,14 @@ describe('whoami', () => {
|
|||||||
const user = useUser();
|
const user = useUser();
|
||||||
const exitCode = await whoami(client);
|
const exitCode = await whoami(client);
|
||||||
expect(exitCode).toEqual(0);
|
expect(exitCode).toEqual(0);
|
||||||
expect(client.mockOutput.mock.calls.length).toEqual(1);
|
expect(client.outputBuffer).toEqual(`> ${user.username}\n`);
|
||||||
expect(client.mockOutput.mock.calls[0][0]).toEqual(`${user.username}\n`);
|
});
|
||||||
|
|
||||||
|
it('should print only the Vercel username when output is not a TTY', async () => {
|
||||||
|
const user = useUser();
|
||||||
|
client.output.isTTY = false;
|
||||||
|
const exitCode = await whoami(client);
|
||||||
|
expect(exitCode).toEqual(0);
|
||||||
|
expect(client.outputBuffer).toEqual(`${user.username}\n`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -69,6 +69,12 @@ export class MockClient extends Client {
|
|||||||
this.output.spinner = () => {};
|
this.output.spinner = () => {};
|
||||||
|
|
||||||
this.scenario = Router();
|
this.scenario = Router();
|
||||||
|
|
||||||
|
this.output.isTTY = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
get outputBuffer() {
|
||||||
|
return this.mockOutput.mock.calls.map(c => c[0]).join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
async startMockServer() {
|
async startMockServer() {
|
||||||
|
|||||||
Reference in New Issue
Block a user