Skip to content

Commit 7ce229d

Browse files
author
Brian Vaughn
committed
Made some incremental progress on Jest tests
1 parent 4c4f5fd commit 7ce229d

File tree

9 files changed

+80
-28
lines changed

9 files changed

+80
-28
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
"error-stack-parser": "^2.0.2",
4848
"eslint": "^6.1.0",
4949
"eslint-config-fbjs": "^1.1.1",
50+
"eslint-plugin-babel": "^5.3.0",
5051
"eslint-plugin-flowtype": "^2.25.0",
5152
"eslint-plugin-jest": "^22.15.0",
5253
"eslint-plugin-no-for-of-loops": "^1.0.0",
53-
"eslint-plugin-babel": "^5.3.0",
5454
"eslint-plugin-react": "^6.7.1",
5555
"eslint-plugin-react-internal": "link:./scripts/eslint-rules",
5656
"fbjs-scripts": "^0.8.3",
@@ -61,8 +61,8 @@
6161
"google-closure-compiler": "20190301.0.0",
6262
"gzip-size": "^3.0.0",
6363
"jasmine-check": "^1.0.0-rc.0",
64-
"jest": "^23.1.0",
65-
"jest-diff": "^23.0.1",
64+
"jest": "^23",
65+
"jest-diff": "^23",
6666
"jest-snapshot-serializer-raw": "^1.1.0",
6767
"minimatch": "^3.0.4",
6868
"minimist": "^1.2.0",
@@ -102,6 +102,7 @@
102102
"postinstall": "node node_modules/fbjs-scripts/node/check-dev-engines.js package.json && node ./scripts/flow/createFlowConfigs.js",
103103
"debug-test": "cross-env NODE_ENV=development node --inspect-brk node_modules/.bin/jest --config ./scripts/jest/config.source.js --runInBand",
104104
"test": "cross-env NODE_ENV=development jest --config ./scripts/jest/config.source.js",
105+
"test-devtools": "cross-env NODE_ENV=development jest --config ./scripts/jest/config.devtools.js",
105106
"test-persistent": "cross-env NODE_ENV=development jest --config ./scripts/jest/config.source-persistent.js",
106107
"debug-test-persistent": "cross-env NODE_ENV=development node --inspect-brk node_modules/.bin/jest --config ./scripts/jest/config.source-persistent.js --runInBand",
107108
"test-prod": "cross-env NODE_ENV=production jest --config ./scripts/jest/config.source.js",

packages/react-devtools-shared/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@reach/tooltip": "^0.2.2",
99
"clipboard-js": "^0.3.6",
1010
"events": "^3.0.0",
11+
"local-storage-fallback": "^4.1.1",
1112
"lodash.throttle": "^4.1.1",
1213
"memoize-one": "^3.1.1",
1314
"react-virtualized-auto-sizer": "^1.0.2"

packages/react-devtools-shared/src/__tests__/bridge-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('Bridge', () => {
3030
expect(wall.send).toHaveBeenCalledWith('shutdown');
3131

3232
// Verify that the Bridge doesn't send messages after shutdown.
33-
spyOn(console, 'warn');
33+
spyOnDevAndProd(console, 'warn');
3434
wall.send.mockClear();
3535
bridge.send('should not send');
3636
jest.runAllTimers();

packages/react-devtools-shared/src/__tests__/console-test.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,10 @@ describe('console', () => {
1313
let unpatchConsole;
1414

1515
beforeEach(() => {
16-
const Console = require('../backend/console');
16+
const Console = require('react-devtools-shared/src/backend/console');
1717
patchConsole = Console.patch;
1818
unpatchConsole = Console.unpatch;
1919

20-
const inject = global.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject;
21-
global.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject = internals => {
22-
inject(internals);
23-
24-
Console.registerRenderer(internals);
25-
};
26-
27-
React = require('react');
28-
ReactDOM = require('react-dom');
29-
30-
const utils = require('./utils');
31-
act = utils.act;
32-
3320
// Patch a fake console so we can verify with tests below.
3421
// Patching the real console is too complicated,
3522
// because Jest itself has hooks into it as does our test env setup.
@@ -46,7 +33,22 @@ describe('console', () => {
4633

4734
Console.dangerous_setTargetConsoleForTesting(fakeConsole);
4835

36+
// Note the Console module only patches once,
37+
// so it's important to patch the test console before injection.
4938
patchConsole();
39+
40+
const inject = global.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject;
41+
global.__REACT_DEVTOOLS_GLOBAL_HOOK__.inject = internals => {
42+
inject(internals);
43+
44+
Console.registerRenderer(internals);
45+
};
46+
47+
React = require('react');
48+
ReactDOM = require('react-dom');
49+
50+
const utils = require('./utils');
51+
act = utils.act;
5052
});
5153

5254
function normalizeCodeLocInfo(str) {

packages/react-devtools-shared/src/__tests__/setupEnv.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
// @flow
1+
'use strict';
22

3-
import storage from 'local-storage-fallback';
3+
// Import test renderer and then reset modules for two reasons:
4+
// 1. We don't want test renderer connected to the DevTools hooks.
5+
// 2. This enables us to work around the on-renderer limitation.
6+
// TODO This (or the resetModules below) might be the cause of the hooks erorr in some tests
7+
const ReactTestRenderer = require.requireActual('react-test-renderer');
48

5-
// In case async/await syntax is used in a test.
6-
import 'regenerator-runtime/runtime';
9+
jest.resetModules();
10+
11+
jest.mock('react-test-renderer', () => ReactTestRenderer);
712

813
// DevTools stores preferences between sessions in localStorage
914
if (!global.hasOwnProperty('localStorage')) {
10-
global.localStorage = storage;
15+
global.localStorage = require('local-storage-fallback').default;
1116
}
1217

1318
// Mimic the global we set with Webpack's DefinePlugin
1419
global.__DEV__ = process.env.NODE_ENV !== 'production';
15-
global.__TEST__ = process.env.NODE_ENV === 'test';

packages/react-devtools-shared/src/__tests__/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export async function actAsync(
4040
});
4141

4242
if (recursivelyFlush) {
43-
while (jest.getTimerCount() > 0) {
43+
// TODO (Jest v24) Replace with jest.getTimerCount() after we upgrade Jest
44+
while (global.mockGetTimersCount() > 0) {
4445
// $FlowFixMe Flow doens't know about "await act()" yet
4546
await actDOM(async () => {
4647
await actTestRenderer(async () => {

scripts/jest/config.devtools.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const baseConfig = require('./config.base');
4+
5+
module.exports = Object.assign({}, baseConfig, {
6+
testRegex: 'packages/react-devtools-shared/src/__tests__/[^]+.test.js$',
7+
modulePathIgnorePatterns: [],
8+
snapshotSerializers: [
9+
require.resolve(
10+
'../../packages/react-devtools-shared/src/__tests__/inspectedElementSerializer.js'
11+
),
12+
require.resolve(
13+
'../../packages/react-devtools-shared/src/__tests__/storeSerializer.js'
14+
),
15+
],
16+
setupFiles: [
17+
...baseConfig.setupFiles,
18+
require.resolve('./setupHostConfigs.js'),
19+
require.resolve(
20+
'../../packages/react-devtools-shared/src/__tests__/setupEnv.js'
21+
),
22+
],
23+
// TODO (Jest v24) Rename "setupFilesAfterEnv" after Jest upgrade
24+
setupTestFrameworkScriptFile: require.resolve(
25+
'../../packages/react-devtools-shared/src/__tests__/setupTests.js'
26+
),
27+
});

scripts/jest/preprocessor.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ const babelOptions = {
4343

4444
// This optimization is important for extremely performance-sensitive (e.g. React source).
4545
// It's okay to disable it for tests.
46-
[require.resolve('@babel/plugin-transform-block-scoping'), {throwIfClosureRequired: false}],
46+
[
47+
require.resolve('@babel/plugin-transform-block-scoping'),
48+
{throwIfClosureRequired: false},
49+
],
4750
],
4851
retainLines: true,
4952
};

yarn.lock

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,6 +1879,7 @@ array.prototype.find@^2.0.1:
18791879
arrify@^1.0.1:
18801880
version "1.0.1"
18811881
resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d"
1882+
integrity sha1-iYUI2iIm84DfkEcoRWhJwVAaSw0=
18821883

18831884
arrify@^2.0.1:
18841885
version "2.0.1"
@@ -3513,6 +3514,11 @@ [email protected]:
35133514
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.0.tgz#beb437e7022b3b6d49019d088665303ebe9c14ba"
35143515
integrity sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg==
35153516

3517+
cookie@^0.3.1:
3518+
version "0.3.1"
3519+
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.3.1.tgz#e7e0a1f9ef43b4c8ba925c5c5a96e806d16873bb"
3520+
integrity sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=
3521+
35163522
copy-concurrently@^1.0.0:
35173523
version "1.0.5"
35183524
resolved "https://registry.yarnpkg.com/copy-concurrently/-/copy-concurrently-1.0.5.tgz#92297398cae34937fcafd6ec8139c18051f0b5e0"
@@ -7215,7 +7221,7 @@ jest-config@^23.6.0:
72157221
micromatch "^2.3.11"
72167222
pretty-format "^23.6.0"
72177223

7218-
jest-diff@^23.0.1, jest-diff@^23.6.0:
7224+
jest-diff@^23, jest-diff@^23.6.0:
72197225
version "23.6.0"
72207226
resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-23.6.0.tgz#1500f3f16e850bb3d71233408089be099f610c7d"
72217227
integrity sha512-Gz9l5Ov+X3aL5L37IT+8hoCUsof1CVYBb2QEkOupK64XyRR3h+uRpYIm97K7sY8diFxowR8pIGEdyfMKTixo3g==
@@ -7536,7 +7542,7 @@ jest-worker@^24.6.0:
75367542
merge-stream "^1.0.1"
75377543
supports-color "^6.1.0"
75387544

7539-
jest@^23.1.0:
7545+
jest@^23:
75407546
version "23.6.0"
75417547
resolved "https://registry.yarnpkg.com/jest/-/jest-23.6.0.tgz#ad5835e923ebf6e19e7a1d7529a432edfee7813d"
75427548
integrity sha512-lWzcd+HSiqeuxyhG+EnZds6iO3Y3ZEnMrfZq/OTGvF/C+Z4fPMCdhWTGSAiO2Oym9rbEXfwddHhh6jqrTF3+Lw==
@@ -7941,6 +7947,13 @@ [email protected], loader-utils@^1.0.2, loader-utils@^1.1.0, loader-utils@^1.2.
79417947
emojis-list "^2.0.0"
79427948
json5 "^1.0.1"
79437949

7950+
local-storage-fallback@^4.1.1:
7951+
version "4.1.1"
7952+
resolved "https://registry.yarnpkg.com/local-storage-fallback/-/local-storage-fallback-4.1.1.tgz#6dc964635c8e9ab6d522d7d88538f465b62bd161"
7953+
integrity sha512-Ka4rem1FOgvIaPMokxXTP8DgW8gjfAQSdJC8TGrplDug7vh3b0PZnY/9euG3O3cIGAvJLp33mMU6MJ13x6S+Pw==
7954+
dependencies:
7955+
cookie "^0.3.1"
7956+
79447957
locate-path@^2.0.0:
79457958
version "2.0.0"
79467959
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-2.0.0.tgz#2b568b265eec944c6d9c0de9c3dbbbca0354cd8e"

0 commit comments

Comments
 (0)