Skip to content
This repository was archived by the owner on Jan 26, 2019. It is now read-only.

Commit 4da0d01

Browse files
Carsten FuehrmannCarsten Fuehrmann
Carsten Fuehrmann
authored and
Carsten Fuehrmann
committed
Synced with upstream
2 parents 109b53d + 8e0ebe2 commit 4da0d01

File tree

5 files changed

+58
-40
lines changed

5 files changed

+58
-40
lines changed

packages/react-scripts/config/paths.js

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ module.exports = {
5959
testsSetup: resolveApp('src/setupTests.ts'),
6060
appNodeModules: resolveApp('node_modules'),
6161
appTsConfig: resolveApp('tsconfig.json'),
62+
appTsLint: resolveApp('tslint.json'),
6263
publicUrl: getPublicUrl(resolveApp('package.json')),
6364
servedPath: getServedPath(resolveApp('package.json')),
6465
};
@@ -81,6 +82,7 @@ module.exports = {
8182
appNodeModules: resolveApp('node_modules'),
8283
appTsConfig: resolveApp('tsconfig.json'),
8384
appTsTestConfig: resolveApp('tsconfig.test.json'),
85+
appTsLint: resolveApp('tslint.json'),
8486
publicUrl: getPublicUrl(resolveApp('package.json')),
8587
servedPath: getServedPath(resolveApp('package.json')),
8688
// These properties only exist before ejecting:
@@ -112,6 +114,7 @@ if (
112114
testsSetup: resolveOwn('template/src/setupTests.ts'),
113115
appNodeModules: resolveOwn('node_modules'),
114116
appTsConfig: resolveOwn('template/tsconfig.json'),
117+
appTsLint: resolveOwn('template/tslint.json'),
115118
appTsTestConfig: resolveOwn('template/tsconfig.test.json'),
116119
publicUrl: getPublicUrl(resolveOwn('package.json')),
117120
servedPath: getServedPath(resolveOwn('package.json')),

packages/react-scripts/config/webpack.config.dev.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
1616
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
1717
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
1818
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
19+
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
1920
const getClientEnvironment = require('./env');
2021
const paths = require('./paths');
2122

@@ -132,14 +133,6 @@ module.exports = {
132133
// We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
133134
// { parser: { requireEnsure: false } },
134135

135-
// First, run the linter.
136-
// It's important to do this before Babel processes the JS.
137-
{
138-
test: /\.(ts|tsx)$/,
139-
loader: require.resolve('tslint-loader'),
140-
enforce: 'pre',
141-
include: paths.appSrc,
142-
},
143136
{
144137
test: /\.js$/,
145138
loader: require.resolve('source-map-loader'),
@@ -166,7 +159,15 @@ module.exports = {
166159
{
167160
test: /\.(ts|tsx)$/,
168161
include: paths.appSrc,
169-
loader: require.resolve('ts-loader'),
162+
use: [
163+
{
164+
loader: require.resolve('ts-loader'),
165+
options: {
166+
// disable type checker - we will use it in fork plugin
167+
transpileOnly: true,
168+
},
169+
},
170+
],
170171
},
171172
// Process JS with Babel.
172173
{
@@ -276,6 +277,13 @@ module.exports = {
276277
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
277278
// You can remove this if you don't use Moment.js:
278279
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
280+
// Perform type checking and linting in a separate process to speed up compilation
281+
new ForkTsCheckerWebpackPlugin({
282+
async: false,
283+
watch: paths.appSrc,
284+
tsconfig: paths.appTsConfig,
285+
tslint: paths.appTsLint,
286+
}),
279287
],
280288
// Some libraries import Node modules but don't use them in the browser.
281289
// Tell Webpack to provide empty mocks for them so importing them works.

packages/react-scripts/config/webpack.config.prod.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const ManifestPlugin = require('webpack-manifest-plugin');
1717
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
1818
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin');
1919
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
20+
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
2021
const paths = require('./paths');
2122
const getClientEnvironment = require('./env');
2223

@@ -140,15 +141,6 @@ module.exports = {
140141
// TODO: Disable require.ensure as it's not a standard language feature.
141142
// We are waiting for https://github.com/facebookincubator/create-react-app/issues/2176.
142143
// { parser: { requireEnsure: false } },
143-
144-
// First, run the linter.
145-
// It's important to do this before Babel processes the JS.
146-
{
147-
test: /\.(ts|tsx)$/,
148-
loader: require.resolve('tslint-loader'),
149-
enforce: 'pre',
150-
include: paths.appSrc,
151-
},
152144
{
153145
test: /\.js$/,
154146
loader: require.resolve('source-map-loader'),
@@ -170,11 +162,20 @@ module.exports = {
170162
name: 'static/media/[name].[hash:8].[ext]',
171163
},
172164
},
173-
//Compile .tsx?
165+
// Compile .tsx?
174166
{
175167
test: /\.(ts|tsx)$/,
176168
include: paths.appSrc,
177-
loader: require.resolve('ts-loader'),
169+
170+
use: [
171+
{
172+
loader: require.resolve('ts-loader'),
173+
options: {
174+
// disable type checker - we will use it in fork plugin
175+
transpileOnly: true,
176+
},
177+
},
178+
],
178179
},
179180
// Process JS with Babel.
180181
{
@@ -365,6 +366,12 @@ module.exports = {
365366
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
366367
// You can remove this if you don't use Moment.js:
367368
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
369+
// Perform type checking and linting in a separate process to speed up compilation
370+
new ForkTsCheckerWebpackPlugin({
371+
async: false,
372+
tsconfig: paths.appTsConfig,
373+
tslint: paths.appTsLint,
374+
}),
368375
],
369376
// Some libraries import Node modules but don't use them in the browser.
370377
// Tell Webpack to provide empty mocks for them so importing them works.

packages/react-scripts/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"dotenv": "4.0.0",
3535
"extract-text-webpack-plugin": "3.0.2",
3636
"file-loader": "1.1.5",
37+
"fork-ts-checker-webpack-plugin": "^0.2.8",
3738
"fs-extra": "3.0.1",
3839
"html-webpack-plugin": "2.29.0",
3940
"jest": "20.0.4",
@@ -53,7 +54,6 @@
5354
"ts-jest": "^21.1.4",
5455
"ts-loader": "^3.1.1",
5556
"tslint": "^5.8.0",
56-
"tslint-loader": "^3.5.3",
5757
"tslint-react": "^3.2.0",
5858
"typescript": "~2.6.1",
5959
"source-map-loader": "^0.2.3"

packages/react-scripts/template/README.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -1246,9 +1246,9 @@ There is a broad spectrum of component testing techniques. They range from a “
12461246

12471247
Different projects choose different testing tradeoffs based on how often components change, and how much logic they contain. If you haven’t decided on a testing strategy yet, we recommend that you start with creating simple smoke tests for your components:
12481248

1249-
```js
1250-
import React from 'react';
1251-
import ReactDOM from 'react-dom';
1249+
```ts
1250+
import * as React from 'react';
1251+
import * asReactDOM from 'react-dom';
12521252
import App from './App';
12531253

12541254
it('renders without crashing', () => {
@@ -1257,38 +1257,38 @@ it('renders without crashing', () => {
12571257
});
12581258
```
12591259

1260-
This test mounts a component and makes sure that it didn’t throw during rendering. Tests like this provide a lot value with very little effort so they are great as a starting point, and this is the test you will find in `src/App.test.js`.
1260+
This test mounts a component and makes sure that it didn’t throw during rendering. Tests like this provide a lot value with very little effort so they are great as a starting point, and this is the test you will find in `src/App.test.tsx`.
12611261

12621262
When you encounter bugs caused by changing components, you will gain a deeper insight into which parts of them are worth testing in your application. This might be a good time to introduce more specific tests asserting specific expected output or behavior.
12631263

12641264
If you’d like to test components in isolation from the child components they render, we recommend using [`shallow()` rendering API](http://airbnb.io/enzyme/docs/api/shallow.html) from [Enzyme](http://airbnb.io/enzyme/). To install it, run:
12651265

12661266
```sh
1267-
npm install --save enzyme enzyme-adapter-react-16 react-test-renderer
1267+
npm install --save enzyme @types/enzyme enzyme-adapter-react-16 @types/enzyme-adapter-react-16 react-test-renderer @types/react-test-renderer
12681268
```
12691269

12701270
Alternatively you may use `yarn`:
12711271

12721272
```sh
1273-
yarn add enzyme enzyme-adapter-react-16 react-test-renderer
1273+
yarn add enzyme @types/enzyme enzyme-adapter-react-16 @types/enzyme-adapter-react-16 react-test-renderer @types/react-test-renderer
12741274
```
12751275

12761276
As of Enzyme 3, you will need to install Enzyme along with an Adapter corresponding to the version of React you are using. (The examples above use the adapter for React 16.)
12771277

12781278
The adapter will also need to be configured in your [global setup file](#initializing-test-environment):
12791279

1280-
#### `src/setupTests.js`
1281-
```js
1282-
import { configure } from 'enzyme';
1283-
import Adapter from 'enzyme-adapter-react-16';
1280+
#### `src/setupTests.ts`
1281+
```ts
1282+
import * as Enzyme from 'enzyme';
1283+
import * as Adapter from 'enzyme-adapter-react-16';
12841284

1285-
configure({ adapter: new Adapter() });
1285+
Enzyme.configure({ adapter: new Adapter() });
12861286
```
12871287

12881288
Now you can write a smoke test with it:
12891289

1290-
```js
1291-
import React from 'react';
1290+
```ts
1291+
import * as React from 'react';
12921292
import { shallow } from 'enzyme';
12931293
import App from './App';
12941294

@@ -1303,8 +1303,8 @@ You can read the [Enzyme documentation](http://airbnb.io/enzyme/) for more testi
13031303

13041304
Here is an example from Enzyme documentation that asserts specific output, rewritten to use Jest matchers:
13051305

1306-
```js
1307-
import React from 'react';
1306+
```ts
1307+
import * as React from 'react';
13081308
import { shallow } from 'enzyme';
13091309
import App from './App';
13101310

@@ -1337,7 +1337,7 @@ Alternatively you may use `yarn`:
13371337
yarn add jest-enzyme
13381338
```
13391339

1340-
Import it in [`src/setupTests.js`](#initializing-test-environment) to make its matchers available in every test:
1340+
Import it in [`src/setupTests.ts`](#initializing-test-environment) to make its matchers available in every test:
13411341

13421342
```js
13431343
import 'jest-enzyme';
@@ -1360,12 +1360,12 @@ and then use them in your tests like you normally do.
13601360

13611361
>Note: this feature is available with `[email protected]` and higher.
13621362
1363-
If your app uses a browser API that you need to mock in your tests or if you just need a global setup before running your tests, add a `src/setupTests.js` to your project. It will be automatically executed before running your tests.
1363+
If your app uses a browser API that you need to mock in your tests or if you just need a global setup before running your tests, add a `src/setupTests.ts` to your project. It will be automatically executed before running your tests.
13641364

13651365
For example:
13661366

1367-
#### `src/setupTests.js`
1368-
```js
1367+
#### `src/setupTests.ts`
1368+
```ts
13691369
const localStorageMock = {
13701370
getItem: jest.fn(),
13711371
setItem: jest.fn(),

0 commit comments

Comments
 (0)