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
82 changes: 82 additions & 0 deletions e2e/__tests__/__snapshots__/test-todo.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 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

Invalid first argument: () => {}. Todo must be called with a string.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This might look weird, when the fn is not a oneliner.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, perhaps we should just remove this as per @rickhanlonii's comments below. I'm pretty flexible with how this API looks. Maybe just validation of a minimum one arg with the first being a string? :)

Copy link
Member

Choose a reason for hiding this comment

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

I think just a simple "todo must be called with just a string", or something similar, is better. No need to stringify the implementation as long as our stack trace point points back to the declaration


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

at packages/jest-jasmine2/build/jasmine/Env.js:480:15
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we strip this from stack trace? cc @SimenB

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 please teach me this magic (again), I've tried but not been able to remove it :(

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 packages/jest-jasmine2/build/jasmine/Env.js:476:15
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 packages/jest-jasmine2/build/jasmine/Env.js:476:15
at __tests__/todo_no_args.test.js:8:4

"
`;

exports[`works with all statuses 1`] = `
"FAIL __tests__/statuses.test.js
✓ passes
✕ fails
✎ todo
○ skipped 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

"
`;
55 changes: 55 additions & 0 deletions e2e/__tests__/test-todo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 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 output = extractSummary(result.stderr)
.rest.split('\n')
.map(line => line.trimRight())
Copy link
Member

Choose a reason for hiding this comment

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

Is this needed? If yes, please extract to a helper function instead of copying it in every test

.join('\n');
expect(output).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 output = extractSummary(result.stderr)
.rest.split('\n')
.map(line => line.trimRight())
.join('\n');
expect(output).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 output = extractSummary(result.stderr)
.rest.split('\n')
.map(line => line.trimRight())
.join('\n');
expect(output).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 output = extractSummary(result.stderr)
.rest.split('\n')
.map(line => line.trimRight())
.join('\n');
expect(output).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"
}
}
44 changes: 44 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,44 @@
/**
* 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(
'Invalid first argument: () => {}. Todo must be called with a string.',
);
});
});
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
25 changes: 25 additions & 0 deletions packages/jest-circus/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,31 @@ test.only = (testName: TestName, fn: TestFn, timeout?: number) => {
});
};

test.todo = (testName: TestName, ...rest: Array<mixed>) => {
if (rest.length > 0 || testName === undefined) {
throw new Error('Todo must be called with only a description.');
Copy link
Member

Choose a reason for hiding this comment

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

const e = new Error('Todo must be called with only a description.');

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

throw e;

ish 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

❤️amazing!

I tried something similar to this but wasn't passing the correct call site

}

if (typeof testName !== 'string') {
throw new Error(
`Invalid first argument: ${testName}. Todo must be called with a string.`,
);
}
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
2 changes: 2 additions & 0 deletions packages/jest-cli/src/reporters/verbose_reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export default class VerboseReporter extends DefaultReporter {
return chalk.red(ICONS.failed);
} else if (status === 'pending') {
return chalk.yellow(ICONS.pending);
} else if (status === 'todo') {
return chalk.magenta(ICONS.todo);
} else {
return chalk.green(ICONS.success);
}
Expand Down
6 changes: 5 additions & 1 deletion packages/jest-cli/src/testResultHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const makeEmptyAggregatedTestResult = (): AggregatedResult => ({
numPendingTestSuites: 0,
numPendingTests: 0,
numRuntimeErrorTestSuites: 0,
numTodoTests: 0,
numTotalTestSuites: 0,
numTotalTests: 0,
openHandles: [],
Expand Down Expand Up @@ -57,6 +58,7 @@ export const buildFailureTestResult = (
numFailingTests: 0,
numPassingTests: 0,
numPendingTests: 0,
numTodoTests: 0,
openHandles: [],
perfStats: {
end: 0,
Expand Down Expand Up @@ -87,10 +89,12 @@ export const addResult = (
aggregatedResults.numTotalTests +=
testResult.numPassingTests +
testResult.numFailingTests +
testResult.numPendingTests;
testResult.numPendingTests +
testResult.numTodoTests;
aggregatedResults.numFailedTests += testResult.numFailingTests;
aggregatedResults.numPassedTests += testResult.numPassingTests;
aggregatedResults.numPendingTests += testResult.numPendingTests;
aggregatedResults.numTodoTests += testResult.numTodoTests;

if (testResult.testExecError) {
aggregatedResults.numRuntimeErrorTestSuites++;
Expand Down
Loading