Skip to content

Commit

Permalink
Merge pull request #6872 from gaearon/jest-cli@12
Browse files Browse the repository at this point in the history
Update to Jest 12.1.1 and Jasmine 2
(cherry picked from commit c0ecde6)
  • Loading branch information
gaearon authored and zpao committed Jun 14, 2016
1 parent 5831341 commit c8b2a3d
Show file tree
Hide file tree
Showing 66 changed files with 832 additions and 903 deletions.
9 changes: 7 additions & 2 deletions grunt/tasks/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function run(done, configPath) {
grunt.log.writeln('running jest');

var args = [
path.join('node_modules', 'jest-cli', 'bin', 'jest'),
path.join('node_modules', 'jest', 'bin', 'jest'),
'--runInBand',
'--no-watchman',
];
Expand All @@ -83,7 +83,12 @@ function run(done, configPath) {
grunt.util.spawn({
cmd: 'node',
args: args,
opts: { stdio: 'inherit', env: { NODE_ENV: 'test' } },
opts: {
stdio: 'inherit',
env: Object.assign({}, process.env, {
NODE_ENV: 'test',
}),
},
}, function(spawnErr, result, code) {
if (spawnErr) {
onError(spawnErr);
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"gulp-flatten": "^0.2.0",
"gulp-util": "^3.0.7",
"gzip-js": "~0.3.2",
"jest-cli": "^12.0.2",
"jest": "^12.1.1",
"loose-envify": "^1.1.0",
"object-assign": "^4.1.0",
"platform": "^1.1.0",
Expand Down Expand Up @@ -90,7 +90,6 @@
"scriptPreprocessor": "scripts/jest/preprocessor.js",
"setupEnvScriptFile": "scripts/jest/environment.js",
"setupTestFrameworkScriptFile": "scripts/jest/test-framework-setup.js",
"testRunner": "jasmine1",
"testFileExtensions": [
"coffee",
"js",
Expand Down
1 change: 1 addition & 0 deletions scripts/jest/jest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ declare function xit(name: string, fn: any): void;
interface Expect {
not: Expect
toThrow(message?: string): void
toThrowError(message?: string): void
toBe(value: any): void
toEqual(value: any): void
toBeFalsy(): void
Expand Down
63 changes: 39 additions & 24 deletions scripts/jest/test-framework-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,49 @@

var env = jasmine.getEnv();

var callCount = 0;
var oldError = console.error;
var newError = function() {
callCount++;
oldError.apply(this, arguments);
var spec = env.currentSpec;
if (spec) {
var expectationResult = new jasmine.ExpectationResult({
passed: false,
message:
'Expected test not to warn. If the warning is expected, mock it ' +
'out using spyOn(console, \'error\'); and test that the warning ' +
'occurs.',
});
spec.addMatcherResult(expectationResult);
}
};
console.error = newError;

// Make sure console.error is set back at the end of each test, or else the
// above logic won't work
afterEach(function() {
// TODO: Catch test cases that call spyOn() but don't inspect the mock
// properly.
console.error = newError;

if (console.error !== newError && !console.error.isSpy) {
var expectationResult = new jasmine.ExpectationResult({
passed: false,
message: 'Test did not tear down console.error mock properly.',
});
env.currentSpec.addMatcherResult(expectationResult);
}
env.beforeEach(() => {
callCount = 0;
jasmine.addMatchers({
toBeReset() {
return {
compare(actual) {
// TODO: Catch test cases that call spyOn() but don't inspect the mock
// properly.
if (actual !== newError && !jasmine.isSpy(actual)) {
return {
pass: false,
message: 'Test did not tear down console.error mock properly.',
};
}
return {pass: true};
},
};
},
toNotHaveBeenCalled() {
return {
compare(actual) {
return {
pass: callCount === 0,
message:
'Expected test not to warn. If the warning is expected, mock ' +
'it out using spyOn(console, \'error\'); and test that the ' +
'warning occurs.',
};
},
};
},
});
});
env.afterEach(() => {
expect(console.error).toBeReset();
expect(console.error).toNotHaveBeenCalled();
});
22 changes: 11 additions & 11 deletions src/addons/__tests__/ReactFragment-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('ReactFragment', function() {
};
var element = <div>{[children]}</div>;
var container = document.createElement('div');
expect(() => ReactDOM.render(element, container)).toThrow(
expect(() => ReactDOM.render(element, container)).toThrowError(
'Objects are not valid as a React child (found: object with keys ' +
'{x, y, z}). If you meant to render a collection of children, use an ' +
'array instead or wrap the object using createFragment(object) from ' +
Expand All @@ -51,7 +51,7 @@ describe('ReactFragment', function() {
}
}
var container = document.createElement('div');
expect(() => ReactDOM.render(<Foo />, container)).toThrow(
expect(() => ReactDOM.render(<Foo />, container)).toThrowError(
'Objects are not valid as a React child (found: object with keys ' +
'{a, b, c}). If you meant to render a collection of children, use an ' +
'array instead or wrap the object using createFragment(object) from ' +
Expand All @@ -62,7 +62,7 @@ describe('ReactFragment', function() {
it('should throw if a plain object looks like an old element', function() {
var oldEl = {_isReactElement: true, type: 'span', props: {}};
var container = document.createElement('div');
expect(() => ReactDOM.render(<div>{oldEl}</div>, container)).toThrow(
expect(() => ReactDOM.render(<div>{oldEl}</div>, container)).toThrowError(
'Objects are not valid as a React child (found: object with keys ' +
'{_isReactElement, type, props}). It looks like you\'re using an ' +
'element created by a different version of React. Make sure to use ' +
Expand All @@ -75,35 +75,35 @@ describe('ReactFragment', function() {

ReactFragment.create({1: <span />, 2: <span />});

expect(console.error.argsForCall.length).toBe(1);
expect(console.error.argsForCall[0][0]).toContain(
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toContain(
'Child objects should have non-numeric keys so ordering is preserved.'
);
});

it('should warn if passing null to createFragment', function() {
spyOn(console, 'error');
ReactFragment.create(null);
expect(console.error.calls.length).toBe(1);
expect(console.error.argsForCall[0][0]).toContain(
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toContain(
'React.addons.createFragment only accepts a single object.'
);
});

it('should warn if passing an array to createFragment', function() {
spyOn(console, 'error');
ReactFragment.create([]);
expect(console.error.calls.length).toBe(1);
expect(console.error.argsForCall[0][0]).toContain(
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toContain(
'React.addons.createFragment only accepts a single object.'
);
});

it('should warn if passing a ReactElement to createFragment', function() {
spyOn(console, 'error');
ReactFragment.create(<div />);
expect(console.error.calls.length).toBe(1);
expect(console.error.argsForCall[0][0]).toContain(
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toContain(
'React.addons.createFragment does not accept a ReactElement without a ' +
'wrapper object.'
);
Expand Down
2 changes: 1 addition & 1 deletion src/addons/__tests__/renderSubtreeIntoContainer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('renderSubtreeIntoContainer', function() {
componentDidMount: function() {
expect(function() {
renderSubtreeIntoContainer(<Parent />, <Component />, portal);
}).toThrow('parentComponentmust be a valid React Component');
}).toThrowError('parentComponentmust be a valid React Component');
},
});
});
Expand Down
22 changes: 11 additions & 11 deletions src/addons/__tests__/update-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ describe('update', function() {
expect(obj).toEqual([1]);
});
it('only pushes an array', function() {
expect(update.bind(null, [], {$push: 7})).toThrow(
expect(update.bind(null, [], {$push: 7})).toThrowError(
'update(): expected spec of $push to be an array; got 7. Did you ' +
'forget to wrap your parameter in an array?'
);
});
it('only pushes unto an array', function() {
expect(update.bind(null, 1, {$push: 7})).toThrow(
expect(update.bind(null, 1, {$push: 7})).toThrowError(
'update(): expected target of $push to be an array; got 1.'
);
});
Expand All @@ -47,13 +47,13 @@ describe('update', function() {
expect(obj).toEqual([1]);
});
it('only unshifts an array', function() {
expect(update.bind(null, [], {$unshift: 7})).toThrow(
expect(update.bind(null, [], {$unshift: 7})).toThrowError(
'update(): expected spec of $unshift to be an array; got 7. Did you ' +
'forget to wrap your parameter in an array?'
);
});
it('only unshifts unto an array', function() {
expect(update.bind(null, 1, {$unshift: 7})).toThrow(
expect(update.bind(null, 1, {$unshift: 7})).toThrowError(
'update(): expected target of $unshift to be an array; got 1.'
);
});
Expand All @@ -69,17 +69,17 @@ describe('update', function() {
expect(obj).toEqual([1, 4, 3]);
});
it('only splices an array of arrays', function() {
expect(update.bind(null, [], {$splice: 1})).toThrow(
expect(update.bind(null, [], {$splice: 1})).toThrowError(
'update(): expected spec of $splice to be an array of arrays; got 1. ' +
'Did you forget to wrap your parameters in an array?'
);
expect(update.bind(null, [], {$splice: [1]})).toThrow(
expect(update.bind(null, [], {$splice: [1]})).toThrowError(
'update(): expected spec of $splice to be an array of arrays; got 1. ' +
'Did you forget to wrap your parameters in an array?'
);
});
it('only splices unto an array', function() {
expect(update.bind(null, 1, {$splice: 7})).toThrow(
expect(update.bind(null, 1, {$splice: 7})).toThrowError(
'Expected $splice target to be an array; got 1'
);
});
Expand All @@ -95,12 +95,12 @@ describe('update', function() {
expect(obj).toEqual({a: 'b'});
});
it('only merges with an object', function() {
expect(update.bind(null, {}, {$merge: 7})).toThrow(
expect(update.bind(null, {}, {$merge: 7})).toThrowError(
'update(): $merge expects a spec of type \'object\'; got 7'
);
});
it('only merges with an object', function() {
expect(update.bind(null, 7, {$merge: {a: 'b'}})).toThrow(
expect(update.bind(null, 7, {$merge: {a: 'b'}})).toThrowError(
'update(): $merge expects a target of type \'object\'; got 7'
);
});
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('update', function() {
expect(obj).toEqual({v: 2});
});
it('only applies a function', function() {
expect(update.bind(null, 2, {$apply: 123})).toThrow(
expect(update.bind(null, 2, {$apply: 123})).toThrowError(
'update(): expected spec of $apply to be a function; got 123.'
);
});
Expand Down Expand Up @@ -170,7 +170,7 @@ describe('update', function() {
});

it('should require a command', function() {
expect(update.bind(null, {a: 'b'}, {a: 'c'})).toThrow(
expect(update.bind(null, {a: 'b'}, {a: 'c'})).toThrowError(
'update(): You provided a key path to update() that did not contain ' +
'one of $push, $unshift, $splice, $set, $merge, $apply. Did you ' +
'forget to include {$set: ...}?'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('ReactCSSTransitionGroup', function() {
);

// Warning about the missing transitionLeaveTimeout prop
expect(console.error.argsForCall.length).toBe(1);
expect(console.error.calls.count()).toBe(1);
});

it('should not warn if timeouts is zero', function() {
Expand All @@ -61,7 +61,7 @@ describe('ReactCSSTransitionGroup', function() {
container
);

expect(console.error.argsForCall.length).toBe(0);
expect(console.error.calls.count()).toBe(0);
});

it('should clean-up silently after the timeout elapses', function() {
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('ReactCSSTransitionGroup', function() {
}

// No warnings
expect(console.error.argsForCall.length).toBe(0);
expect(console.error.calls.count()).toBe(0);

// The leaving child has been removed
expect(ReactDOM.findDOMNode(a).childNodes.length).toBe(1);
Expand Down
6 changes: 3 additions & 3 deletions src/addons/transitions/__tests__/ReactTransitionGroup-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,14 @@ describe('ReactTransitionGroup', function() {

ReactDOM.render(<Component />, container);

expect(console.error.argsForCall.length).toBe(2);
expect(console.error.argsForCall[0][0]).toBe(
expect(console.error.calls.count()).toBe(2);
expect(console.error.calls.argsFor(0)[0]).toBe(
'Warning: flattenChildren(...): ' +
'Encountered two children with the same key, `1`. ' +
'Child keys must be unique; when two children share a key, ' +
'only the first child will be used.'
);
expect(normalizeCodeLocInfo(console.error.argsForCall[1][0])).toBe(
expect(normalizeCodeLocInfo(console.error.calls.argsFor(1)[0])).toBe(
'Warning: flattenChildren(...): ' +
'Encountered two children with the same key, `1`. ' +
'Child keys must be unique; when two children share a key, ' +
Expand Down
Loading

0 comments on commit c8b2a3d

Please sign in to comment.