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

Fail jest tests on any un-spied warnings #5002

Merged
merged 1 commit into from
Sep 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"rootDir": "",
"scriptPreprocessor": "scripts/jest/preprocessor.js",
"setupEnvScriptFile": "scripts/jest/environment.js",
"setupTestFrameworkScriptFile": "scripts/jest/test-framework-setup.js",
"testFileExtensions": [
"coffee",
"js",
Expand Down
35 changes: 35 additions & 0 deletions scripts/jest/test-framework-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var env = jasmine.getEnv();

var oldError = console.error;
var newError = function() {
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.

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);
}
});
12 changes: 6 additions & 6 deletions src/renderers/dom/client/__tests__/ReactMount-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,16 @@ describe('ReactMount', function() {
});

var container = document.createElement('div');
console.error = mocks.getMockFunction();
spyOn(console, 'error');
var component = RD1.render(<X />, container);
expect(console.error.mock.calls.length).toBe(0);
expect(console.error.argsForCall.length).toBe(0);

// This fails but logs a warning first
expect(function() {
RD2.findDOMNode(component);
}).toThrow();
expect(console.error.mock.calls.length).toBe(1);
expect(console.error.mock.calls[0][0]).toContain('two copies of React');
expect(console.error.argsForCall.length).toBe(1);
expect(console.error.argsForCall[0][0]).toContain('two copies of React');
});

it('should warn if render removes React-rendered children', function() {
Expand All @@ -231,12 +231,12 @@ describe('ReactMount', function() {
return <div><div /></div>;
},
});
React.render(<Component />, container);
ReactDOM.render(<Component />, container);

// Test that blasting away children throws a warning
spyOn(console, 'error');
var rootNode = container.firstChild;
React.render(<span />, rootNode);
ReactDOM.render(<span />, rootNode);
expect(console.error.callCount).toBe(1);
expect(console.error.mostRecentCall.args[0]).toBe(
'Warning: render(...): Replacing React-rendered children with a new ' +
Expand Down