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

Add test.todo #6996

Merged
merged 11 commits into from
Sep 29, 2018
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

- `[jest-jasmine2/jest-circus/jest-cli]` Add test.todo ([#6996](https://github.com/facebook/jest/pull/6996))
- `[pretty-format]` Option to not escape strings in diff messages ([#5661](https://github.com/facebook/jest/pull/5661))

### Fixes
Expand Down
18 changes: 18 additions & 0 deletions docs/GlobalAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,21 @@ test('will be ran', () => {
expect(1 / 0).toBe(Infinity);
});
```

### `test.todo(name)`

Use `test.todo` when you are planning on writing tests. These tests will be highlighted in the summary output at the end so you know how many tests you still need todo.

_Note_: If you supply a test callback function then the `test.todo` will throw an error. If you have already implemented the test and it is broken and you do not want it to run, then use `test.skip` instead.

#### API

- `name`: `String` the title of the test plan.

Example:

```js
const add = (a, b) => a + b;

test.todo('add should be associative');
```
79 changes: 79 additions & 0 deletions e2e/__tests__/__snapshots__/test-todo.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`shows error messages when called with invalid argument 1`] = `
"FAIL __tests__/todo_non_string.test.js
● Test suite failed to run

Todo must be called with only a description.

6 | */
7 |
> 8 | it.todo(() => {});
| ^
9 |

at __tests__/todo_non_string.test.js:8:4

"
`;

exports[`shows error messages when called with multiple arguments 1`] = `
"FAIL __tests__/todo_multiple_args.test.js
● Test suite failed to run

Todo must be called with only a description.

6 | */
7 |
> 8 | it.todo('todo later', () => {});
| ^
9 |

at __tests__/todo_multiple_args.test.js:8:4

"
`;

exports[`shows error messages when called with no arguments 1`] = `
"FAIL __tests__/todo_no_args.test.js
● Test suite failed to run

Todo must be called with only a description.

6 | */
7 |
> 8 | it.todo();
| ^
9 |

at __tests__/todo_no_args.test.js:8:4

"
`;

exports[`works with all statuses 1`] = `
"FAIL __tests__/statuses.test.js
✓ passes
✕ fails
○ skipped 1 test
✎ todo 1 test

● fails

expect(received).toBe(expected) // Object.is equality

Expected: 101
Received: 10

11 |
12 | it('fails', () => {
> 13 | expect(10).toBe(101);
| ^
14 | });
15 |
16 | it.skip('skips', () => {

at __tests__/statuses.test.js:13:14

"
`;
43 changes: 43 additions & 0 deletions e2e/__tests__/test-todo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2018-present, Facebook, Inc. 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.
*
* @flow
*/

'use strict';

const path = require('path');
const runJest = require('../runJest');
const {extractSummary} = require('../Utils');
const dir = path.resolve(__dirname, '../test-todo');

test('works with all statuses', () => {
const result = runJest(dir, ['statuses.test.js']);
expect(result.status).toBe(1);
const {rest} = extractSummary(result.stderr);
expect(rest).toMatchSnapshot();
});

test('shows error messages when called with no arguments', () => {
const result = runJest(dir, ['todo_no_args.test.js']);
expect(result.status).toBe(1);
const {rest} = extractSummary(result.stderr);
expect(rest).toMatchSnapshot();
});

test('shows error messages when called with multiple arguments', () => {
const result = runJest(dir, ['todo_multiple_args.test.js']);
expect(result.status).toBe(1);
const {rest} = extractSummary(result.stderr);
expect(rest).toMatchSnapshot();
});

test('shows error messages when called with invalid argument', () => {
const result = runJest(dir, ['todo_non_string.test.js']);
expect(result.status).toBe(1);
const {rest} = extractSummary(result.stderr);
expect(rest).toMatchSnapshot();
});
20 changes: 20 additions & 0 deletions e2e/test-todo/__tests__/statuses.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) 2018-present, Facebook, Inc. 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.
*/

it('passes', () => {
expect(10).toBe(10);
});

it('fails', () => {
expect(10).toBe(101);
});

it.skip('skips', () => {
expect(10).toBe(101);
});

it.todo('todo');
8 changes: 8 additions & 0 deletions e2e/test-todo/__tests__/todo_multiple_args.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Copyright (c) 2018-present, Facebook, Inc. 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.
*/

it.todo('todo later', () => {});
8 changes: 8 additions & 0 deletions e2e/test-todo/__tests__/todo_no_args.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Copyright (c) 2018-present, Facebook, Inc. 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.
*/

it.todo();
8 changes: 8 additions & 0 deletions e2e/test-todo/__tests__/todo_non_string.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Copyright (c) 2018-present, Facebook, Inc. 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.
*/

it.todo(() => {});
5 changes: 5 additions & 0 deletions e2e/test-todo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
42 changes: 42 additions & 0 deletions packages/jest-circus/src/__tests__/circus_todo_test_error.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) 2015-present, Facebook, Inc. 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.
*
* @flow strict-local
*/

'use strict';

let circusIt;

// using jest-jasmine2's 'it' to test jest-circus's 'it'. Had to differentiate
// the two with this alias.

const aliasCircusIt = () => {
const {it} = require('../index.js');
circusIt = it;
};

aliasCircusIt();

describe('test/it.todo error throwing', () => {
it('todo throws error when given no arguments', () => {
expect(() => {
// $FlowFixMe: Testing runitme errors here
circusIt.todo();
}).toThrowError('Todo must be called with only a description.');
});
it('todo throws error when given more than one argument', () => {
expect(() => {
circusIt.todo('test1', () => {});
}).toThrowError('Todo must be called with only a description.');
});
it('todo throws error when given none string description', () => {
expect(() => {
// $FlowFixMe: Testing runitme errors here
circusIt.todo(() => {});
}).toThrowError('Todo must be called with only a description.');
});
});
4 changes: 4 additions & 0 deletions packages/jest-circus/src/event_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ const handler: EventHandler = (event, state): void => {
event.test.status = 'skip';
break;
}
case 'test_todo': {
event.test.status = 'todo';
break;
}
case 'test_done': {
event.test.duration = getTestDuration(event.test);
event.test.status = 'done';
Expand Down
26 changes: 26 additions & 0 deletions packages/jest-circus/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,32 @@ test.only = (testName: TestName, fn: TestFn, timeout?: number) => {
});
};

test.todo = (testName: TestName, ...rest: Array<mixed>) => {
if (rest.length > 0 || typeof testName !== 'string') {
const e = new Error('Todo must be called with only a description.');

if (Error.captureStackTrace) {
Error.captureStackTrace(e, test.todo);
}

throw e;
}

const asyncError = new Error();
if (Error.captureStackTrace) {
Error.captureStackTrace(asyncError, test);
}

return dispatch({
asyncError,
fn: () => {},
mode: 'todo',
name: 'add_test',
testName,
timeout: undefined,
});
};

test.each = bindEach(test);
test.only.each = bindEach(test.only);
test.skip.each = bindEach(test.skip);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,16 @@ export const runAndTransformResultsToJestFormat = async ({
let numFailingTests = 0;
let numPassingTests = 0;
let numPendingTests = 0;
let numTodoTests = 0;

const assertionResults = runResult.testResults.map(testResult => {
let status: Status;
if (testResult.status === 'skip') {
status = 'pending';
numPendingTests += 1;
} else if (testResult.status === 'todo') {
status = 'todo';
numTodoTests += 1;
} else if (testResult.errors.length) {
status = 'failed';
numFailingTests += 1;
Expand Down Expand Up @@ -184,6 +188,7 @@ export const runAndTransformResultsToJestFormat = async ({
numFailingTests,
numPassingTests,
numPendingTests,
numTodoTests,
openHandles: [],
perfStats: {
// populated outside
Expand Down
5 changes: 5 additions & 0 deletions packages/jest-circus/src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ const _runTest = async (test: TestEntry): Promise<void> => {
return;
}

if (test.mode === 'todo') {
dispatch({name: 'test_todo', test});
return;
}

const {afterEach, beforeEach} = getEachHooksForTest(test);

for (const hook of beforeEach) {
Expand Down
1 change: 1 addition & 0 deletions packages/jest-cli/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const ICONS = {
failed: isWindows ? '\u00D7' : '\u2715',
pending: '\u25CB',
success: isWindows ? '\u221A' : '\u2713',
todo: '\u270E',
Copy link
Member

Choose a reason for hiding this comment

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

Can you check how this looks on windows?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@SimenB I don't have a windows machine, does anyone else have one?

Copy link
Member

Choose a reason for hiding this comment

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

};
export const PACKAGE_JSON = 'package.json';
export const JEST_CONFIG = 'jest.config.js';
2 changes: 2 additions & 0 deletions packages/jest-cli/src/reporters/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export const getSummary = (
const testsFailed = aggregatedResults.numFailedTests;
const testsPassed = aggregatedResults.numPassedTests;
const testsPending = aggregatedResults.numPendingTests;
const testsTodo = aggregatedResults.numTodoTests;
const testsTotal = aggregatedResults.numTotalTests;
const width = (options && options.width) || 0;

Expand All @@ -141,6 +142,7 @@ export const getSummary = (
chalk.bold('Tests: ') +
(testsFailed ? chalk.bold.red(`${testsFailed} failed`) + ', ' : '') +
(testsPending ? chalk.bold.yellow(`${testsPending} skipped`) + ', ' : '') +
(testsTodo ? chalk.bold.magenta(`${testsTodo} todo`) + ', ' : '') +
(testsPassed ? chalk.bold.green(`${testsPassed} passed`) + ', ' : '') +
`${testsTotal} total`;

Expand Down
Loading