Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print todo and skip descriptions when in verbose mode #8038

Merged
merged 12 commits into from Mar 19, 2019
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,8 @@
- `[jest-config]` Print error information on preset normalization error ([#7935](https://github.com/facebook/jest/pull/7935))
- `[jest-haste-map]` Add `skipPackageJson` option ([#7778](https://github.com/facebook/jest/pull/7778))
- `[jest-get-type]` Add `isPrimitive` function ([#7708](https://github.com/facebook/jest/pull/7708))
- `[jest-get-type]` Add `isPrimitive` function ([#7708](https://github.com/facebook/jest/pull/7708))
- `[@jest/reporter]` Display todo description when verbose is true ([#8038](https://github.com/facebook/jest/pull/8038))

### Fixes

Expand Down
10 changes: 9 additions & 1 deletion e2e/__tests__/__snapshots__/testTodo.test.ts.snap
Expand Up @@ -45,12 +45,20 @@ FAIL __tests__/todoNoArgs.test.js
at Object.todo (__tests__/todoNoArgs.test.js:8:4)
`;

exports[`shows todo messages when in verbose mode 1`] = `
PASS __tests__/verbose.test.js
todos in verbose reporter
✎ todo this
✎ todo should
✎ todo work
`;

exports[`works with all statuses 1`] = `
FAIL __tests__/statuses.test.js
✓ passes
✕ fails
○ skipped 1 test
✎ todo 1 test
✎ todo todo
SimenB marked this conversation as resolved.
Show resolved Hide resolved

● fails

Expand Down
7 changes: 7 additions & 0 deletions e2e/__tests__/testTodo.test.ts
Expand Up @@ -38,3 +38,10 @@ test('shows error messages when called with invalid argument', () => {
const {rest} = extractSummary(result.stderr);
expect(wrap(rest)).toMatchSnapshot();
});

test('shows todo messages when in verbose mode', () => {
const result = runJest(dir, ['verbose.test.js', '--verbose']);
expect(result.status).toBe(0);
const {rest} = extractSummary(result.stderr);
expect(wrap(rest)).toMatchSnapshot();
});
13 changes: 13 additions & 0 deletions e2e/test-todo/__tests__/verbose.test.js
@@ -0,0 +1,13 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

describe('todos in verbose reporter', () => {
test.todo('this');
test.todo('should');
test.todo('work');
});
6 changes: 6 additions & 0 deletions e2e/verbose-reporter-todo/package.json
@@ -0,0 +1,6 @@
{
natealcedo marked this conversation as resolved.
Show resolved Hide resolved
"jest": {
"testEnvironment": "node",
"verbose": true
}
}
30 changes: 21 additions & 9 deletions packages/jest-reporters/src/verbose_reporter.ts
Expand Up @@ -118,14 +118,14 @@ export default class VerboseReporter extends DefaultReporter {
if (test.status === 'pending') {
result.pending += 1;
} else if (test.status === 'todo') {
result.todo += 1;
result.todo.push(test);
} else {
this._logTest(test, indentLevel);
}

return result;
},
{pending: 0, todo: 0},
{pending: 0, todo: [] as Array<TestResult.AssertionResult>},
natealcedo marked this conversation as resolved.
Show resolved Hide resolved
);

if (summedTests.pending > 0) {
Expand All @@ -137,17 +137,29 @@ export default class VerboseReporter extends DefaultReporter {
);
}

if (summedTests.todo > 0) {
this._logSummedTests(
'todo',
this._getIcon('todo'),
summedTests.todo,
indentLevel,
);
if (summedTests.todo.length > 0) {
summedTests.todo.forEach(todoTest => {
this._logTodoTest(
'todo',
this._getIcon('todo'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems silly to pass in the icons and title, but I see it's consistent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Didn't want to stray too far from what was being done. The same could be said for printing skipped tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think given that we are explicitly pulling this behaviour out into _logTodoTest we should just move the concrete data into the function definition.

 private _logTodoTest(
    todoTest: TestResult.AssertionResult,
    indentLevel: number,
  ) {
    const text = chalk.dim(`todo ${todoTest.title}`);
    this._logLine(`${this._getIcon('todo')} ${text}`, indentLevel);
  }

Then if you want to be fancy you could curry this to:

 private _logTodoTest = (indentLevel: number) => (todoTest: TestResult.AssertionResult) => {
    const text = chalk.dim(`todo ${todoTest.title}`);
    this._logLine(`${this._getIcon('todo')} ${text}`, indentLevel);
  }

and then you can just do the following in the forEach:

summedTests.todo.forEach(this._logTodoTest(indentLevel));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this approach. Should we do a similar approach to skipped tests then since their functions are very similar?

todoTest,
indentLevel,
);
});
}
}
}

private _logTodoTest(
prefix: string,
icon: string,
todoTest: TestResult.AssertionResult,
indentLevel: number,
) {
const text = chalk.dim(`${prefix} ${todoTest.title}`);
this._logLine(`${icon} ${text}`, indentLevel);
}

private _logSummedTests(
prefix: string,
icon: string,
Expand Down