Make TypeScript test work with ReactDOM
Ben Alpert committed
Sep 1, 2015 at 14:19 UTC
589c8364b01060a768940634a537d9c0405e280d
5 files changed
+61
-36
scripts/jest/preprocessor.js
+1
-8
@@ -7,13 +7,6 @@ var coffee = require('coffee-script');
7
8
var tsPreprocessor = require('./ts-preprocessor');
9
10
-var defaultLibraries = [
11
- require.resolve('./jest.d.ts'),
12
- require.resolve('../../src/isomorphic/modern/class/React.d.ts'),
13
-];
14
-
15
-var ts = tsPreprocessor(defaultLibraries);
16
-
10
// This assumes the module map has been built. This might not be safe.
11
// We should consider consuming this from a built fbjs module from npm.
12
var moduleMap = require('fbjs/module-map');
@@ -26,7 +19,7 @@ module.exports = {
19
return coffee.compile(src, {'bare': true});
20
}
21
if (filePath.match(/\.ts$/) && !filePath.match(/\.d\.ts$/)) {
29
- return ts.compile(src, filePath);
22
+ return tsPreprocessor.compile(src, filePath);
23
}
24
// TODO: make sure this stays in sync with gulpfile
25
if (!filePath.match(/\/node_modules\//) &&
scripts/jest/ts-preprocessor.js
+34
-21
@@ -15,14 +15,37 @@ function formatErrorMessage(error) {
15
);
16
}
17
18
-function compile(defaultLib, content, contentFilename) {
18
+function compile(content, contentFilename) {
19
var output = null;
20
var compilerHost = {
21
getSourceFile: function(filename, languageVersion) {
22
- if (filename === contentFilename) {
23
- return ts.createSourceFile(filename, content, 'ES5', '0');
22
+ var source;
23
+ if (filename === 'lib.d.ts') {
24
+ source = fs.readFileSync(
25
+ require.resolve('typescript/bin/lib.d.ts')
26
+ ).toString();
27
+ } else if (filename === 'jest.d.ts') {
28
+ source = fs.readFileSync(
29
+ path.join(__dirname, 'jest.d.ts')
30
+ ).toString();
31
+ } else if (filename === contentFilename) {
32
+ source = content;
33
+ } else if (/\/(?:React|ReactDOM)(?:\.d)?\.ts$/.test(filename)) {
34
+ // TypeScript will look for the .d.ts files in each ancestor directory,
35
+ // so there may not be a file at the referenced path as it climbs the
36
+ // hierarchy.
37
+ try {
38
+ source = fs.readFileSync(filename).toString();
39
+ } catch (e) {
40
+ if (e.code == 'ENOENT') {
41
+ return undefined;
42
+ }
43
+ throw e;
44
+ }
45
+ } else {
46
+ throw new Error('Unexpected filename ' + filename);
47
}
25
- return defaultLib;
48
+ return ts.createSourceFile(filename, source, 'ES5', '0');
49
},
50
writeFile: function(name, text, writeByteOrderMark) {
51
if (output === null) {
@@ -41,7 +64,11 @@ function compile(defaultLib, content, contentFilename) {
64
return '\n';
65
},
66
};
44
- var program = ts.createProgram([contentFilename], tsOptions, compilerHost);
67
+ var program = ts.createProgram([
68
+ 'lib.d.ts',
69
+ 'jest.d.ts',
70
+ contentFilename,
71
+ ], tsOptions, compilerHost);
72
var errors = program.getDiagnostics();
73
if (!errors.length) {
74
var checker = program.getTypeChecker(true);
@@ -54,20 +81,6 @@ function compile(defaultLib, content, contentFilename) {
81
return output;
82
}
83
57
-module.exports = function(defaultLibs) {
58
- var defaultLibSource = fs.readFileSync(
59
- path.join(path.dirname(require.resolve('typescript')), 'lib.d.ts')
60
- );
61
-
62
- for (var i = 0; i < defaultLibs.length; i++) {
63
- defaultLibSource += '\n' + fs.readFileSync(defaultLibs[i]);
64
- }
65
-
66
- var defaultLibSourceFile = ts.createSourceFile(
67
- 'lib.d.ts', defaultLibSource, 'ES5'
68
- );
69
-
70
- return {
71
- compile: compile.bind(null, defaultLibSourceFile),
72
- };
84
+module.exports = {
85
+ compile: compile,
86
};
src/isomorphic/modern/class/React.d.ts
-3
@@ -26,7 +26,4 @@ declare module 'React' {
26
}
27
export var PropTypes : any;
28
export function createElement(tag : any, props ?: any, ...children : any[]) : any
29
- export function render(element : any, container : any) : any
30
- export function unmountComponentAtNode(container : any) : void
31
- export function findDOMNode(instance : any) : any
29
}
src/isomorphic/modern/class/ReactDOM.d.ts
new
+21
@@ -0,0 +1,21 @@
1
+/*!
2
+ * Copyright 2015, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ */
9
+
10
+/**
11
+ * TypeScript Definition File for React.
12
+ *
13
+ * Full type definitions are not yet officially supported. These are mostly
14
+ * just helpers for the unit test.
15
+ */
16
+
17
+declare module 'ReactDOM' {
18
+ export function render(element : any, container : any) : any
19
+ export function unmountComponentAtNode(container : any) : void
20
+ export function findDOMNode(instance : any) : any
21
+}
src/isomorphic/modern/class/__tests__/ReactTypeScriptClass-test.ts
+5
-4
@@ -8,6 +8,7 @@
8
*/
9
10
import React = require('React');
11
+import ReactDOM = require('ReactDOM');
12
13
// Before Each
14
@@ -27,7 +28,7 @@ class Inner extends React.Component {
28
}
29
30
function test(element, expectedTag, expectedClassName) {
30
- var instance = React.render(element, container);
31
+ var instance = ReactDOM.render(element, container);
32
expect(container.firstChild).not.toBeNull();
33
expect(container.firstChild.tagName).toBe(expectedTag);
34
expect(container.firstChild.className).toBe(expectedClassName);
@@ -313,7 +314,7 @@ describe('ReactTypeScriptClass', function() {
314
it('throws if no render function is defined', function() {
315
spyOn(console, 'error');
316
316
- expect(() => React.render(React.createElement(Empty), container)).toThrow();
317
+ expect(() => ReactDOM.render(React.createElement(Empty), container)).toThrow();
318
319
expect((<any>console.error).argsForCall.length).toBe(1);
320
expect((<any>console.error).argsForCall[0][0]).toBe(
@@ -425,7 +426,7 @@ describe('ReactTypeScriptClass', function() {
426
'did-update', { value: 'foo' }, {}
427
]);
428
lifeCycles = []; // reset
428
- React.unmountComponentAtNode(container);
429
+ ReactDOM.unmountComponentAtNode(container);
430
expect(lifeCycles).toEqual([
431
'will-unmount'
432
]);
@@ -527,7 +528,7 @@ describe('ReactTypeScriptClass', function() {
528
'DIV',
529
'foo'
530
);
530
- var node = React.findDOMNode(instance);
531
+ var node = ReactDOM.findDOMNode(instance);
532
expect(node).toBe(container.firstChild);
533
});
534