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

Commit 6ba6134

Browse files
committed
Add support for setupTests.ts
1 parent 666fc26 commit 6ba6134

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

packages/react-scripts/config/paths.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module.exports = {
4949
appPackageJson: resolveApp('package.json'),
5050
appSrc: resolveApp('src'),
5151
yarnLockFile: resolveApp('yarn.lock'),
52-
testsSetup: resolveApp('src/setupTests.js'),
52+
testsSetup: resolveApp('src/setupTests.ts'),
5353
appNodeModules: resolveApp('node_modules'),
5454
ownNodeModules: resolveApp('node_modules'),
5555
nodePaths: nodePaths
@@ -69,7 +69,7 @@ module.exports = {
6969
appPackageJson: resolveApp('package.json'),
7070
appSrc: resolveApp('src'),
7171
yarnLockFile: resolveApp('yarn.lock'),
72-
testsSetup: resolveApp('src/setupTests.js'),
72+
testsSetup: resolveApp('src/setupTests.ts'),
7373
appNodeModules: resolveApp('node_modules'),
7474
// this is empty with npm3 but node resolution searches higher anyway:
7575
ownNodeModules: resolveOwn('../node_modules'),
@@ -86,7 +86,7 @@ if (__dirname.indexOf(path.join('packages', 'react-scripts', 'config')) !== -1)
8686
appPackageJson: resolveOwn('../package.json'),
8787
appSrc: resolveOwn('../template/src'),
8888
yarnLockFile: resolveOwn('../template/yarn.lock'),
89-
testsSetup: resolveOwn('../template/src/setupTests.js'),
89+
testsSetup: resolveOwn('../template/src/setupTests.ts'),
9090
appNodeModules: resolveOwn('../node_modules'),
9191
ownNodeModules: resolveOwn('../node_modules'),
9292
nodePaths: nodePaths

packages/react-scripts/template/README.md

+27-11
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ Now you are ready to use the imported React Bootstrap components within your com
455455

456456
Flow is a static type checker that helps you write code with fewer bugs. Check out this [introduction to using static types in JavaScript](https://medium.com/@preethikasireddy/why-use-static-types-in-javascript-part-1-8382da1e0adb) if you are new to this concept.
457457

458-
Recent versions of [Flow](http://flowtype.org/) work with Create React App projects out of the box.
458+
Recent versions of [Flow](http://flowtype.org/) work with Create React App projects out of the box.
459459

460460
To add Flow to a Create React App project, follow these steps:
461461

@@ -793,18 +793,34 @@ and then use them in your tests like you normally do.
793793

794794
>Note: this feature is available with `[email protected]` and higher.
795795
796-
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.
796+
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.
797797

798798
For example:
799799

800-
#### `src/setupTests.js`
800+
#### `src/setupTests.ts`
801801
```js
802-
const localStorageMock = {
803-
getItem: jest.fn(),
804-
setItem: jest.fn(),
805-
clear: jest.fn()
802+
declare global {
803+
interface localStorage {
804+
getItem: any;
805+
setItem: any;
806+
clean: any;
807+
}
808+
}
809+
810+
class LocalStorageMock {
811+
store = {};
812+
clear() {
813+
this.store = {};
814+
}
815+
getItem(key: any) {
816+
return this.store[key];
817+
}
818+
setItem(key: any, value: any) {
819+
this.store[key] = value.toString();
820+
}
806821
};
807-
global.localStorage = localStorageMock
822+
823+
global.localStorage = new LocalStorageMock();
808824
```
809825

810826
### Focusing and Excluding Tests
@@ -913,7 +929,7 @@ This feature is experimental and still [has major usage issues](https://github.c
913929

914930
### Editor Integration
915931

916-
If you use [Visual Studio Code](https://code.visualstudio.com), there is a [Jest extension](https://github.com/orta/vscode-jest) which works with Create React App out of the box. This provides a lot of IDE-like features while using a text editor: showing the status of a test run with potential fail messages inline, starting and stopping the watcher automatically, and offering one-click snapshot updates.
932+
If you use [Visual Studio Code](https://code.visualstudio.com), there is a [Jest extension](https://github.com/orta/vscode-jest) which works with Create React App out of the box. This provides a lot of IDE-like features while using a text editor: showing the status of a test run with potential fail messages inline, starting and stopping the watcher automatically, and offering one-click snapshot updates.
917933

918934
![VS Code Jest Preview](https://cloud.githubusercontent.com/assets/49038/20795349/a032308a-b7c8-11e6-9b34-7eeac781003f.png)
919935

@@ -1147,15 +1163,15 @@ GitHub Pages doesn't support routers that use the HTML5 `pushState` history API
11471163
### Heroku
11481164
11491165
Use the [Heroku Buildpack for Create React App](https://github.com/mars/create-react-app-buildpack).<br>
1150-
You can find instructions in [Deploying React with Zero Configuration](https://blog.heroku.com/deploying-react-with-zero-configuration).
1166+
You can find instructions in [Deploying React with Zero Configuration](https://blog.heroku.com/deploying-react-with-zero-configuration).
11511167
11521168
#### Resolving "Module not found: Error: Cannot resolve 'file' or 'directory'"
11531169
11541170
Sometimes `npm run build` works locally but fails during deploy via Heroku with an error like this:
11551171
11561172
```
11571173
remote: Failed to create a production build. Reason:
1158-
remote: Module not found: Error: Cannot resolve 'file' or 'directory'
1174+
remote: Module not found: Error: Cannot resolve 'file' or 'directory'
11591175
MyDirectory in /tmp/build_1234/src
11601176
```
11611177

packages/react-scripts/utils/createJestConfig.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const paths = require('../config/paths');
1515
module.exports = (resolve, rootDir, isEjecting) => {
1616
// Use this instead of `paths.testsSetup` to avoid putting
1717
// an absolute filename into configuration after ejecting.
18-
const setupTestsFile = fs.existsSync(paths.testsSetup) ? '<rootDir>/src/setupTests.js' : undefined;
18+
const setupTestsFile = fs.existsSync(paths.testsSetup) ? '<rootDir>/src/setupTests.ts' : undefined;
1919

2020
// TODO: I don't know if it's safe or not to just use / as path separator
2121
// in Jest configs. We need help from somebody with Windows to determine this.

0 commit comments

Comments
 (0)