@samitouri / QOS-React-2 / commits / b8c7b13042

Fail jest tests on any un-spied warnings

Originally #4223 -- we lost this when we switched to jest.

Ben Alpert committed Sep 29, 2015 at 17:35 UTC b8c7b13042734c86f03120bda8a13f5ed63bd124
3 files changed +42 -6
package.json
+1
@@ -62,6 +62,7 @@
62 "rootDir": "",
63 "scriptPreprocessor": "scripts/jest/preprocessor.js",
64 "setupEnvScriptFile": "scripts/jest/environment.js",
65 + "setupTestFrameworkScriptFile": "scripts/jest/test-framework-setup.js",
66 "testFileExtensions": [
67 "coffee",
68 "js",
scripts/jest/test-framework-setup.js new
+35
@@ -0,0 +1,35 @@
1 +'use strict';
2 +
3 +var env = jasmine.getEnv();
4 +
5 +var oldError = console.error;
6 +var newError = function() {
7 + oldError.apply(this, arguments);
8 + var spec = env.currentSpec;
9 + if (spec) {
10 + var expectationResult = new jasmine.ExpectationResult({
11 + passed: false,
12 + message:
13 + 'Expected test not to warn. If the warning is expected, mock it ' +
14 + 'out using spyOn(console, \'error\'); and test that the warning ' +
15 + 'occurs.',
16 + });
17 + spec.addMatcherResult(expectationResult);
18 + }
19 +};
20 +console.error = newError;
21 +
22 +// Make sure console.error is set back at the end of each test, or else the
23 +// above logic won't work
24 +afterEach(function() {
25 + // TODO: Catch test cases that call spyOn() but don't inspect the mock
26 + // properly.
27 +
28 + if (console.error !== newError && !console.error.isSpy) {
29 + var expectationResult = new jasmine.ExpectationResult({
30 + passed: false,
31 + message: 'Test did not tear down console.error mock properly.',
32 + });
33 + env.currentSpec.addMatcherResult(expectationResult);
34 + }
35 +});
src/renderers/dom/client/__tests__/ReactMount-test.js
+6 -6
@@ -212,16 +212,16 @@ describe('ReactMount', function() {
212 });
213
214 var container = document.createElement('div');
215 - console.error = mocks.getMockFunction();
215 + spyOn(console, 'error');
216 var component = RD1.render(<X />, container);
217 - expect(console.error.mock.calls.length).toBe(0);
217 + expect(console.error.argsForCall.length).toBe(0);
218
219 // This fails but logs a warning first
220 expect(function() {
221 RD2.findDOMNode(component);
222 }).toThrow();
223 - expect(console.error.mock.calls.length).toBe(1);
224 - expect(console.error.mock.calls[0][0]).toContain('two copies of React');
223 + expect(console.error.argsForCall.length).toBe(1);
224 + expect(console.error.argsForCall[0][0]).toContain('two copies of React');
225 });
226
227 it('should warn if render removes React-rendered children', function() {
@@ -231,12 +231,12 @@ describe('ReactMount', function() {
231 return <div><div /></div>;
232 },
233 });
234 - React.render(<Component />, container);
234 + ReactDOM.render(<Component />, container);
235
236 // Test that blasting away children throws a warning
237 spyOn(console, 'error');
238 var rootNode = container.firstChild;
239 - React.render(<span />, rootNode);
239 + ReactDOM.render(<span />, rootNode);
240 expect(console.error.callCount).toBe(1);
241 expect(console.error.mostRecentCall.args[0]).toBe(
242 'Warning: render(...): Replacing React-rendered children with a new ' +