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

Better error message when a describe block is empty #6372

Merged
merged 10 commits into from
Oct 7, 2018
30 changes: 28 additions & 2 deletions e2e/__tests__/__snapshots__/globals.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,37 @@ Time: <<REPLACED>>
Ran all test suites."
`;

exports[`cannot have describe with no implementation 1`] = `
"FAIL __tests__/only-constructs.test.js
● Test suite failed to run

Missing second argument supplied to 'describe'. It must be a callback function.

1 |
> 2 | describe('describe, no implementation');
| ^
3 |

at packages/jest-jasmine2/build/jasmine/Env.js:<<LINE>>:<<COLUMN>>
at __tests__/only-constructs.test.js:<<LINE>>:<<COLUMN>>

"
`;

exports[`cannot have describe 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 1`] = `
"FAIL __tests__/only-constructs.test.js
● Test suite failed to run

Missing second argument. It must be a callback function.
Missing second argument supplied to 'it'. It must be a callback function.

1 |
2 | it('it', () => {});
Expand All @@ -50,7 +76,7 @@ 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.
Missing second argument supplied to 'it'. It must be a callback function.

1 |
2 | it('it', () => {});
Expand Down
15 changes: 15 additions & 0 deletions e2e/__tests__/globals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ test('only', () => {
expect(summary).toMatchSnapshot();
});

test('cannot have describe with no implementation', () => {
const filename = 'only-constructs.test.js';
const content = `
describe('describe, no implementation');
`;

writeFiles(TEST_DIR, {[filename]: content});
const {stderr, status} = runJest(DIR);
expect(status).toBe(1);

const {summary, rest} = extractSummary(stderr, {stripLocation: true});
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
});

test('cannot test with no implementation', () => {
const filename = 'only-constructs.test.js';
const content = `
Expand Down
12 changes: 8 additions & 4 deletions packages/jest-jasmine2/src/__tests__/it_test_error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ 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.');
}).toThrowError(
"Missing second argument supplied to 'it'. It must be a callback function.",
);
});
it(`it throws an error when first argument isn't a string`, () => {
expect(() => {
Expand All @@ -23,13 +25,15 @@ describe('test/it error throwing', () => {
expect(() => {
it('test3', 'test3b');
}).toThrowError(
`Invalid second argument, test3b. It must be a callback function.`,
"Invalid second argument supplied to 'it', 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.');
}).toThrowError(
"Missing second argument supplied to 'it'. It must be a callback function.",
);
});
test(`test throws an error when first argument isn't a string`, () => {
expect(() => {
Expand All @@ -40,7 +44,7 @@ describe('test/it error throwing', () => {
expect(() => {
test('test6', 'test6b');
}).toThrowError(
`Invalid second argument, test6b. It must be a callback function.`,
"Invalid second argument supplied to 'it', test6b. It must be a callback function.",
);
});
});
14 changes: 12 additions & 2 deletions packages/jest-jasmine2/src/jasmine/Env.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,16 @@ export default function(j$) {

this.describe = function(description, specDefinitions) {
const suite = suiteFactory(description);
if (specDefinitions === undefined) {
throw new Error(
`Missing second argument supplied to 'describe'. It must be a callback function.`,
);
}
if (typeof specDefinitions !== 'function') {
throw new Error(
`Invalid second argument supplied to 'describe', ${specDefinitions}. It must be a callback function.`,
);
}
if (specDefinitions.length > 0) {
throw new Error('describe does not expect any arguments');
}
Expand Down Expand Up @@ -450,12 +460,12 @@ export default function(j$) {
}
if (fn === undefined) {
throw new Error(
'Missing second argument. It must be a callback function.',
`Missing second argument supplied to 'it'. It must be a callback function.`,
);
}
if (typeof fn !== 'function') {
throw new Error(
`Invalid second argument, ${fn}. It must be a callback function.`,
`Invalid second argument supplied to 'it', ${fn}. It must be a callback function.`,
);
}
const spec = specFactory(
Expand Down