-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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 error throwing to 'it'/'test' for incorrect arguments. #5558
Changes from 22 commits
6e41bcd
b551940
1de43af
f4a012e
a84007f
85587bd
325fe11
363e590
bf07249
918ad6d
d2a3668
b489b37
a873a75
119130c
1d6e12c
29a0f0a
d8eac51
e09303f
5106800
84d9a9b
7815602
0728d30
b10ad04
1ba971c
19d8d88
af8efd7
30ccac2
1842a5d
17d3050
754bf38
5464098
42ef481
fdc0dc3
e986609
a6ab7b0
5453e9f
1520b94
193cdf6
b2ac1c5
5ad1768
0312138
ca714e7
1a4dd3a
a1ea320
ef31915
5e0aabd
30aa554
735bbc2
9514c32
edc9231
d5aa613
0a560ed
911c6e1
07a51fb
281100c
ca02d0a
6e351e7
7eca76f
f85f956
e64357e
f2359e0
4aa3101
139973b
c0c478e
ba03582
061dab3
7195e05
e411607
5163ee3
e0ff615
29a3d64
85f359e
426cb07
895f57e
e6f8d6a
4547c55
5c1d541
dd36839
873671a
b36f4fb
07154b3
2b664de
17d2f77
b867548
b27c0b5
58e973b
f1df10b
7648643
c612bca
feaaf58
7378e67
6e2d789
bf4e287
ba0c286
8e746b4
f92e725
7ab85b1
c1753b3
b5985e1
e03c1a7
06875a8
661496b
f3f1213
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ | |
|
||
### Features | ||
|
||
* `[jest-jasmine2]` Adds error throwing and descriptive errors to `it`/ `test` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you move this under |
||
for invalid arguements. `[jest-circus]` Adds error throwing and descriptive | ||
errors to `it`/ `test` for invalid arguements. | ||
* `[jest-util]` Add the following methods to the "console" implementations: | ||
`assert`, `count`, `countReset`, `dir`, `dirxml`, `group`, `groupCollapsed`, | ||
`groupEnd`, `time`, `timeEnd` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,79 @@ Ran all test suites. | |
" | ||
`; | ||
|
||
exports[`cannot test with no implementation 1`] = ` | ||
"FAIL __tests__/only-constructs.test.js | ||
● Test suite failed to run | ||
|
||
Missing second argument. It must be a callback function. | ||
|
||
431 | } | ||
432 | if (fn === undefined) { | ||
> 433 | throw new Error( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ugh, any way we can disable code frame for this one, @SimenB? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the frame is probably unable to find There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I’ll look in it. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was pulling in the wrong frame because In jest-message-util
Wouldn't catch if the the top frame was from jest packages, so I added a condition:
that way it ignores the jest code as the topframe. |
||
434 | 'Missing second argument. It must be a callback function.'); | ||
435 | | ||
436 | } | ||
|
||
|
||
at __tests__/only-constructs.test.js:3:5 | ||
|
||
" | ||
`; | ||
|
||
exports[`cannot test with no implementation 2`] = ` | ||
"Test Suites: 1 failed, 1 total | ||
Tests: 0 total | ||
Snapshots: 0 total | ||
Time: <<REPLACED>> | ||
Ran all test suites. | ||
" | ||
`; | ||
|
||
exports[`cannot test with no implementation with expand arg 1`] = ` | ||
"FAIL __tests__/only-constructs.test.js | ||
● Test suite failed to run | ||
|
||
Missing second argument. It must be a callback function. | ||
|
||
431 | } | ||
432 | if (fn === undefined) { | ||
> 433 | throw new Error( | ||
434 | 'Missing second argument. It must be a callback function.'); | ||
435 | | ||
436 | } | ||
|
||
|
||
at __tests__/only-constructs.test.js:3:5 | ||
|
||
" | ||
`; | ||
|
||
exports[`cannot test with no implementation with expand arg 2`] = ` | ||
"Test Suites: 1 failed, 1 total | ||
Tests: 0 total | ||
Snapshots: 0 total | ||
Time: <<REPLACED>> | ||
Ran all test suites. | ||
" | ||
`; | ||
|
||
exports[`function as descriptor 1`] = ` | ||
"PASS __tests__/function-as-descriptor.test.js | ||
Foo | ||
✓ it | ||
|
||
" | ||
`; | ||
|
||
exports[`function as descriptor 2`] = ` | ||
"Test Suites: 1 passed, 1 total | ||
Tests: 1 passed, 1 total | ||
Snapshots: 0 total | ||
Time: <<REPLACED>> | ||
Ran all test suites. | ||
" | ||
`; | ||
|
||
exports[`only 1`] = ` | ||
"PASS __tests__/only-constructs.test.js | ||
✓ test.only | ||
|
@@ -121,55 +194,3 @@ Time: <<REPLACED>> | |
Ran all test suites. | ||
" | ||
`; | ||
|
||
exports[`tests with no implementation 1`] = ` | ||
"PASS __tests__/only-constructs.test.js | ||
✓ it | ||
○ skipped 2 tests | ||
|
||
" | ||
`; | ||
|
||
exports[`tests with no implementation 2`] = ` | ||
"Test Suites: 1 passed, 1 total | ||
Tests: 2 skipped, 1 passed, 3 total | ||
Snapshots: 0 total | ||
Time: <<REPLACED>> | ||
Ran all test suites. | ||
" | ||
`; | ||
|
||
exports[`tests with no implementation with expand arg 1`] = ` | ||
"PASS __tests__/only-constructs.test.js | ||
✓ it | ||
○ it, no implementation | ||
○ test, no implementation | ||
|
||
" | ||
`; | ||
|
||
exports[`tests with no implementation with expand arg 2`] = ` | ||
"Test Suites: 1 passed, 1 total | ||
Tests: 2 skipped, 1 passed, 3 total | ||
Snapshots: 0 total | ||
Time: <<REPLACED>> | ||
Ran all test suites. | ||
" | ||
`; | ||
|
||
exports[`function as descriptor 1`] = ` | ||
"PASS __tests__/function-as-descriptor.test.js | ||
Foo | ||
✓ it | ||
|
||
" | ||
`; | ||
|
||
exports[`function as descriptor 2`] = ` | ||
"Test Suites: 1 passed, 1 total | ||
Tests: 1 passed, 1 total | ||
Snapshots: 0 total | ||
Time: <<REPLACED>> | ||
Ran all test suites. | ||
" | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
let circusIt; | ||
let circusTest; | ||
|
||
//using jest-jasmine2's 'it' to test jest-circus's 'it'. Had to differentiate | ||
//the two with this aliaser. | ||
|
||
const aliasCircusIt = () => { | ||
const {it, test} = require('../index.js'); | ||
circusIt = it; | ||
circusTest = test; | ||
}; | ||
|
||
aliasCircusIt(); | ||
|
||
//A few of these tests require incorrect types to throw errors and thus pass | ||
//the test. The typechecks on jest-circus would prevent that, so | ||
//this file has been listed in the .flowconfig ignore section. | ||
|
||
describe('test/it error throwing', () => { | ||
it(`it doesn't throw an error with valid arguments`, () => { | ||
expect(() => { | ||
circusIt('test1', () => {}); | ||
}).not.toThrowError(); | ||
}); | ||
it(`it throws error with missing callback function`, () => { | ||
expect(() => { | ||
circusIt('test2'); | ||
}).toThrowError('Missing second argument. It must be a callback function.'); | ||
}); | ||
it(`it throws an error when first argument isn't a string`, () => { | ||
expect(() => { | ||
circusIt(() => {}); | ||
}).toThrowError(`Invalid first argument, () => {}. It must be a string.`); | ||
}); | ||
it('it throws an error when callback function is not a function', () => { | ||
expect(() => { | ||
circusIt('test4', 'test4b'); | ||
}).toThrowError( | ||
`Invalid second argument, test4b. It must be a callback function.`, | ||
); | ||
}); | ||
it(`test doesn't throw an error with valid arguments`, () => { | ||
expect(() => { | ||
circusTest('test5', () => {}); | ||
}).not.toThrowError(); | ||
}); | ||
it(`test throws error with missing callback function`, () => { | ||
expect(() => { | ||
circusTest('test6'); | ||
}).toThrowError('Missing second argument. It must be a callback function.'); | ||
}); | ||
it(`test throws an error when first argument isn't a string`, () => { | ||
expect(() => { | ||
circusTest(() => {}); | ||
}).toThrowError(`Invalid first argument, () => {}. It must be a string.`); | ||
}); | ||
it('test throws an error when callback function is not a function', () => { | ||
expect(() => { | ||
circusTest('test8', 'test8b'); | ||
}).toThrowError( | ||
`Invalid second argument, test8b. It must be a callback function.`, | ||
); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,8 +38,22 @@ const beforeAll = (fn: HookFn) => _addHook(fn, 'beforeAll'); | |
const afterEach = (fn: HookFn) => _addHook(fn, 'afterEach'); | ||
const afterAll = (fn: HookFn) => _addHook(fn, 'afterAll'); | ||
|
||
const test = (testName: TestName, fn?: TestFn) => | ||
dispatch({fn, name: 'add_test', testName}); | ||
const test = (testName: TestName, fn: TestFn) => { | ||
if (typeof testName !== 'string') { | ||
throw new Error( | ||
`Invalid first argument, ${testName}. It must be a string.`, | ||
); | ||
} | ||
if (fn === undefined) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is legal to not pass second argument, I use it as test placeholders. Might be a a good idea to have an explicit |
||
throw new Error('Missing second argument. It must be a callback function.'); | ||
} | ||
if (typeof fn !== 'function') { | ||
throw new Error( | ||
`Invalid second argument, ${fn}. It must be a callback function.`, | ||
); | ||
} | ||
return dispatch({fn, name: 'add_test', testName}); | ||
}; | ||
const it = test; | ||
test.skip = (testName: TestName, fn?: TestFn) => | ||
dispatch({fn, mode: 'skip', name: 'add_test', testName}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* 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. | ||
* | ||
*/ | ||
|
||
'use strict'; | ||
|
||
describe('test/it error throwing', () => { | ||
it(`it throws error with missing callback function`, () => { | ||
expect(() => { | ||
it('test1'); | ||
}).toThrowError('Missing second argument. It must be a callback function.'); | ||
}); | ||
it(`it throws an error when first argument isn't a string`, () => { | ||
expect(() => { | ||
it(() => {}); | ||
}).toThrowError(`Invalid first argument, () => {}. It must be a string.`); | ||
}); | ||
it('it throws an error when callback function is not a function', () => { | ||
expect(() => { | ||
it('test3', 'test3b'); | ||
}).toThrowError( | ||
`Invalid second argument, test3b. It must be a callback function.`, | ||
); | ||
}); | ||
test(`test throws error with missing callback function`, () => { | ||
expect(() => { | ||
test('test4'); | ||
}).toThrowError('Missing second argument. It must be a callback function.'); | ||
}); | ||
test(`test throws an error when first argument isn't a string`, () => { | ||
expect(() => { | ||
test(() => {}); | ||
}).toThrowError(`Invalid first argument, () => {}. It must be a string.`); | ||
}); | ||
test('test throws an error when callback function is not a function', () => { | ||
expect(() => { | ||
test('test6', 'test6b'); | ||
}).toThrowError( | ||
`Invalid second argument, test6b. It must be a callback function.`, | ||
); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -424,6 +424,21 @@ export default function(j$) { | |
}; | ||
|
||
this.it = function(description, fn, timeout) { | ||
if (typeof description !== 'string') { | ||
throw new Error( | ||
`Invalid first argument, ${description}. It must be a string.`, | ||
); | ||
} | ||
if (fn === undefined) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, cool. So to clarify: add an
but still throw the error for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think "yes", but it's too breaking. But I think we should do it for the next version. (I'm all for adding @cpojer thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fair enough 🙂 |
||
throw new Error( | ||
'Missing second argument. It must be a callback function.', | ||
); | ||
} | ||
if (typeof fn !== 'function') { | ||
throw new Error( | ||
`Invalid second argument, ${fn}. It must be a callback function.`, | ||
); | ||
} | ||
const spec = specFactory( | ||
description, | ||
fn, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, this is not supposed to be here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thymikee it's so the jest-circus tests can check the error for a missing or incorrect argument. If Flow is on for this file it won't run the test. I'm happy to go about this in another manner, if you'd rather.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just remove the
@flow
pragma from circus test. Let's keep this config as clean as possibleThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thymikee
The lint config throws an error during yarn test. Can I remove this line? Or add the test file to the lint ignore? Or an exception to the rule?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope. The problem with this and previous approach is that it's easy to forget we have this "exception", which may lead to falsy feel of confidence about our codebase. I've pushed a commit with a proper, in my opinion, change for this case 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! $FlowFixMe: is something I'll have to remember. Thanks!