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 error throwing to 'it'/'test' for incorrect arguments. #5558

Merged
merged 103 commits into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
6e41bcd
feat: adds error when using it or test without proper arguments
brianlmacdonald Feb 12, 2018
b551940
fix: adds more descriptive error message
brianlmacdonald Feb 12, 2018
1de43af
feat: adds tests for errors
brianlmacdonald Feb 12, 2018
f4a012e
fix: removes bad test
brianlmacdonald Feb 12, 2018
a84007f
feat: adds tests for circus and jasmine2
brianlmacdonald Feb 13, 2018
85587bd
fix: small tweaks to test names
brianlmacdonald Feb 13, 2018
325fe11
fix: adds circus test to flow ignore
brianlmacdonald Feb 13, 2018
363e590
fix: variable names for circus it
brianlmacdonald Feb 13, 2018
bf07249
chore: updates changelog.
brianlmacdonald Feb 13, 2018
918ad6d
fixes: lint error
brianlmacdonald Feb 13, 2018
d2a3668
updates: globals tests and snapshots
brianlmacdonald Feb 13, 2018
b489b37
fix: resets snapshot file
brianlmacdonald Feb 13, 2018
a873a75
fix: temp skips failing tests until further direction
brianlmacdonald Feb 13, 2018
119130c
feat: adds tests for test alias
brianlmacdonald Feb 13, 2018
1d6e12c
fix: renamed unimplemented test. Comments out rest snapshot for now.
brianlmacdonald Feb 14, 2018
29a0f0a
fix: restores rest snapshot and removes abs path from snapshot stack
brianlmacdonald Feb 15, 2018
d8eac51
fix: cleans up stack using cleanUpStackTrace
brianlmacdonald Feb 15, 2018
e09303f
fix: modifies regex in utils
brianlmacdonald Feb 15, 2018
5106800
fix: reverts utils regex
brianlmacdonald Feb 15, 2018
84d9a9b
fix: reverts line deletion in utils.
brianlmacdonald Feb 15, 2018
7815602
fix: replaces env in cleanup stack trace
brianlmacdonald Feb 15, 2018
0728d30
fix: updates utils to current version
brianlmacdonald Feb 15, 2018
b10ad04
fix: updates master
brianlmacdonald Feb 16, 2018
1ba971c
Merge remote-tracking branch 'upstream/master'
brianlmacdonald Feb 20, 2018
19d8d88
feat: adds error when using it or test without proper arguments
brianlmacdonald Feb 12, 2018
af8efd7
fix: adds more descriptive error message
brianlmacdonald Feb 12, 2018
30ccac2
feat: adds tests for errors
brianlmacdonald Feb 12, 2018
1842a5d
fix: removes bad test
brianlmacdonald Feb 12, 2018
17d3050
feat: adds tests for circus and jasmine2
brianlmacdonald Feb 13, 2018
754bf38
fix: small tweaks to test names
brianlmacdonald Feb 13, 2018
5464098
fix: adds circus test to flow ignore
brianlmacdonald Feb 13, 2018
42ef481
fix: variable names for circus it
brianlmacdonald Feb 13, 2018
fdc0dc3
chore: updates changelog.
brianlmacdonald Feb 13, 2018
e986609
updates: globals tests and snapshots
brianlmacdonald Feb 13, 2018
a6ab7b0
fix: resets snapshot file
brianlmacdonald Feb 13, 2018
5453e9f
fix: temp skips failing tests until further direction
brianlmacdonald Feb 13, 2018
1520b94
feat: adds tests for test alias
brianlmacdonald Feb 13, 2018
193cdf6
fix: renamed unimplemented test. Comments out rest snapshot for now.
brianlmacdonald Feb 14, 2018
b2ac1c5
fix: restores rest snapshot and removes abs path from snapshot stack
brianlmacdonald Feb 15, 2018
5ad1768
fix: cleans up stack using cleanUpStackTrace
brianlmacdonald Feb 15, 2018
0312138
fix: modifies regex in utils
brianlmacdonald Feb 15, 2018
ca714e7
fix: reverts utils regex
brianlmacdonald Feb 15, 2018
1a4dd3a
fix: reverts line deletion in utils.
brianlmacdonald Feb 15, 2018
a1ea320
fix: replaces env in cleanup stack trace
brianlmacdonald Feb 15, 2018
ef31915
fix: addresses patch
brianlmacdonald Feb 20, 2018
5e0aabd
fix: replaces overly long packages path for CI test
brianlmacdonald Feb 20, 2018
30aa554
fix: adds new snapshot after packages fix
brianlmacdonald Feb 20, 2018
735bbc2
fix: updates globals snapshot after packages fix
brianlmacdonald Feb 20, 2018
9514c32
fix: finalizes globals snapshot
brianlmacdonald Feb 20, 2018
edc9231
fix: reverses bad snapshot commit
brianlmacdonald Feb 20, 2018
d5aa613
fix: adds missing cli snapshot
brianlmacdonald Feb 20, 2018
0a560ed
fix: bad snap commit
brianlmacdonald Feb 20, 2018
911c6e1
fix: format test name bad commit
brianlmacdonald Feb 20, 2018
07a51fb
fix: adds back trailing newline
brianlmacdonald Feb 20, 2018
281100c
Merge remote-tracking branch 'upstream/master'
brianlmacdonald Feb 21, 2018
ca02d0a
updates master
brianlmacdonald Feb 22, 2018
6e351e7
feat: adds error when using it or test without proper arguments
brianlmacdonald Feb 12, 2018
7eca76f
fix: adds more descriptive error message
brianlmacdonald Feb 12, 2018
f85f956
feat: adds tests for errors
brianlmacdonald Feb 12, 2018
e64357e
fix: removes bad test
brianlmacdonald Feb 12, 2018
f2359e0
feat: adds tests for circus and jasmine2
brianlmacdonald Feb 13, 2018
4aa3101
fix: small tweaks to test names
brianlmacdonald Feb 13, 2018
139973b
fix: adds circus test to flow ignore
brianlmacdonald Feb 13, 2018
c0c478e
fix: variable names for circus it
brianlmacdonald Feb 13, 2018
ba03582
chore: updates changelog.
brianlmacdonald Feb 13, 2018
061dab3
updates: globals tests and snapshots
brianlmacdonald Feb 13, 2018
7195e05
fix: resets snapshot file
brianlmacdonald Feb 13, 2018
e411607
fix: temp skips failing tests until further direction
brianlmacdonald Feb 13, 2018
5163ee3
feat: adds tests for test alias
brianlmacdonald Feb 13, 2018
e0ff615
fix: renamed unimplemented test. Comments out rest snapshot for now.
brianlmacdonald Feb 14, 2018
29a3d64
fix: restores rest snapshot and removes abs path from snapshot stack
brianlmacdonald Feb 15, 2018
85f359e
fix: cleans up stack using cleanUpStackTrace
brianlmacdonald Feb 15, 2018
426cb07
fix: modifies regex in utils
brianlmacdonald Feb 15, 2018
895f57e
fix: reverts utils regex
brianlmacdonald Feb 15, 2018
e6f8d6a
fix: reverts line deletion in utils.
brianlmacdonald Feb 15, 2018
4547c55
fix: replaces env in cleanup stack trace
brianlmacdonald Feb 15, 2018
5c1d541
fix: resolves rebase conflict
brianlmacdonald Feb 21, 2018
dd36839
feat: adds tests for errors
brianlmacdonald Feb 12, 2018
873671a
fix: removes bad test
brianlmacdonald Feb 12, 2018
b36f4fb
fix: rebase conflict resolved
brianlmacdonald Feb 21, 2018
07154b3
fix: variable names for circus it
brianlmacdonald Feb 13, 2018
2b664de
fix: temp skips failing tests until further direction
brianlmacdonald Feb 13, 2018
17d2f77
feat: adds tests for test alias
brianlmacdonald Feb 13, 2018
b867548
fix: renamed unimplemented test. Comments out rest snapshot for now.
brianlmacdonald Feb 14, 2018
b27c0b5
fix: restores rest snapshot and removes abs path from snapshot stack
brianlmacdonald Feb 15, 2018
58e973b
fix: cleans up stack using cleanUpStackTrace
brianlmacdonald Feb 15, 2018
f1df10b
fix: modifies regex in utils
brianlmacdonald Feb 15, 2018
7648643
fix: reverts utils regex
brianlmacdonald Feb 15, 2018
c612bca
fix: reverts line deletion in utils.
brianlmacdonald Feb 15, 2018
feaaf58
fix: replaces env in cleanup stack trace
brianlmacdonald Feb 15, 2018
7378e67
fix: addresses patch
brianlmacdonald Feb 20, 2018
6e2d789
fix: replaces overly long packages path for CI test
brianlmacdonald Feb 20, 2018
bf4e287
fix: adds new snapshot after packages fix
brianlmacdonald Feb 20, 2018
ba0c286
fix: updates globals snapshot after packages fix
brianlmacdonald Feb 20, 2018
8e746b4
fix: reverses bad snapshot commit
brianlmacdonald Feb 20, 2018
f92e725
fix: adds missing cli snapshot
brianlmacdonald Feb 20, 2018
7ab85b1
fix: format test name bad commit
brianlmacdonald Feb 20, 2018
c1753b3
fix: adds back trailing newline
brianlmacdonald Feb 20, 2018
b5985e1
fix: removes redundant BUILD path. updates snapshots
brianlmacdonald Feb 22, 2018
e03c1a7
fix: resolves remote changes
brianlmacdonald Feb 22, 2018
06875a8
fix: deletes empty file, updates changelog
brianlmacdonald Feb 23, 2018
661496b
Merge branch 'master' into feat/iterrorthrowing
brianlmacdonald Feb 23, 2018
f3f1213
fix: handle flow ignores in circus tests
thymikee Feb 23, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[ignore]
.*/examples/.*
.*/node_modules/metro-bundler/.*
.*/circus_it_test_error.test.js*.
Copy link
Collaborator

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

Copy link
Contributor Author

@brianlmacdonald brianlmacdonald Feb 23, 2018

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.

Copy link
Collaborator

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 possible

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@thymikee

        'integration-tests/__tests__/**/*',
        'packages/babel-jest/**/*.test.js',
        'packages/babel-plugin-jest-hoist/**/*.test.js',
        'packages/babel-preset-jest/**/*.test.js',
        'packages/eslint-config-fb-strict/**/*.test.js',
        'packages/eslint-plugin-jest/**/*.test.js',
        'packages/jest-changed-files/**/*.test.js',
       ===> 'packages/jest-circus/**/*.test.js',<====
        'packages/jest-diff/**/*.test.js',
        'packages/jest-docblock/**/*.test.js',
        'packages/jest-editor-support/**/*.test.js',
        'packages/jest/**/*.test.js',
        'packages/pretty-format/**/*.test.js',
      ],
      rules: {
        'flowtype/require-valid-file-annotation': [2, 'always'],
      },

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?

Copy link
Collaborator

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 🙂

Copy link
Contributor Author

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!


[options]
module.name_mapper='^pretty-format$' -> '<PROJECT_ROOT>/packages/pretty-format/src/index.js'
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

### Features

* `[jest-jasmine2]` Adds error throwing and descriptive errors to `it`/ `test`
Copy link
Member

Choose a reason for hiding this comment

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

could you move this under master

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`
Expand Down
125 changes: 73 additions & 52 deletions integration-tests/__tests__/__snapshots__/globals.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ugh, any way we can disable code frame for this one, @SimenB?

Copy link
Member

@SimenB SimenB Feb 16, 2018

Choose a reason for hiding this comment

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

It points to the it in real code, not sure why it shows the throw here..

image

image

image

image

Copy link
Member

Choose a reason for hiding this comment

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

the frame is probably unable to find __tests__/only-constructs.test.js for whatever reason - I'd debug that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I’ll look in it. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was pulling in the wrong frame because In jest-message-util

  for (const line of lines) {
    if (
      line.includes(PATH_NODE_MODULES) ||
      line.includes(PATH_EXPECT_BUILD)
    ) {
      continue;
    }

Wouldn't catch if the the top frame was from jest packages, so I added a condition:

const PATH_JEST_PACKAGES = `${path.sep}jest${path.sep}packages${path.sep}`;

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
Expand Down Expand Up @@ -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.
"
`;
9 changes: 5 additions & 4 deletions integration-tests/__tests__/globals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ test('only', () => {
expect(summary).toMatchSnapshot();
});

test('tests with no implementation', () => {
test('cannot test with no implementation', () => {
const filename = 'only-constructs.test.js';
const content = `
it('it', () => {});
Expand All @@ -120,9 +120,10 @@ test('tests with no implementation', () => {

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

const {summary, rest} = extractSummary(stderr);

expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
});
Expand Down Expand Up @@ -188,7 +189,7 @@ test('only with expand arg', () => {
expect(summary).toMatchSnapshot();
});

test('tests with no implementation with expand arg', () => {
test('cannot test with no implementation with expand arg', () => {
const filename = 'only-constructs.test.js';
const content = `
it('it', () => {});
Expand All @@ -198,7 +199,7 @@ test('tests with no implementation with expand arg', () => {

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

const {summary, rest} = extractSummary(stderr);
expect(rest).toMatchSnapshot();
Expand Down
11 changes: 7 additions & 4 deletions integration-tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import type {Path} from 'types/Config';

const {spawnSync} = require('child_process');
const {sync: spawnSync} = require('cross-spawn');
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
Expand Down Expand Up @@ -43,8 +43,9 @@ const linkJestPackage = (packageName: string, cwd: Path) => {
const packagesDir = path.resolve(__dirname, '../packages');
const packagePath = path.resolve(packagesDir, packageName);
const destination = path.resolve(cwd, 'node_modules/');
run(`mkdir -p ${destination}`);
return run(`ln -sf ${packagePath} ${destination}`);
mkdirp.sync(destination);
rimraf.sync(destination);
fs.symlinkSync(packagePath, destination, 'dir');
};

const fileExists = (filePath: Path) => {
Expand Down Expand Up @@ -158,7 +159,9 @@ const extractSummary = (stdout: string) => {
// unifies their output to make it possible to snapshot them.
// TODO: Remove when we drop support for node 4
const cleanupStackTrace = (output: string) => {
return output.replace(/^.*at.*[\s][\(]?(\S*\:\d*\:\d*).*$/gm, ' at $1');
return output
.replace(/^.*\b(at Env.it)\b.*$/gm, '')
.replace(/^.*at.*[\s][\(]?(\S*\:\d*\:\d*).*$/gm, ' at $1');
};

module.exports = {
Expand Down
75 changes: 75 additions & 0 deletions packages/jest-circus/src/__tests__/circus_it_test_error.test.js
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.`,
);
});
});
18 changes: 16 additions & 2 deletions packages/jest-circus/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The 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 test.todo for that case, though

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});
Expand Down
46 changes: 46 additions & 0 deletions packages/jest-jasmine2/src/__tests__/it_test_error.test.js
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.`,
);
});
});
15 changes: 15 additions & 0 deletions packages/jest-jasmine2/src/jasmine/Env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

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

Same here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, cool. So to clarify: add an it.todo and test.todo to ignore cases like

test.todo('justOneArgument')

but still throw the error for test('justOneArgument')?

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 "yes", but it's too breaking. But I think we should do it for the next version.

(I'm all for adding test/it.todo now - though probably in a separate PR)

@cpojer thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about test.todo, let's discuss that separately. I'm definitely on board with making test/it with one argument throw. I do not consider it a breaking change but rather a bug fix, so it's fine to go into a minor.

Copy link
Member

Choose a reason for hiding this comment

The 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,
Expand Down
Loading