Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 349cf5a

Browse files
authored
Experimental test helper: it.experimental (facebook#17149)
Special version of Jest's `it` for experimental tests. Tests marked as experimental will run **both** stable and experimental modes. In experimental mode, they work the same as the normal Jest methods. In stable mode, they are **expected to fail**. This means we can detect when a test previously marked as experimental can be un-marked when the feature becomes stable. It also reduces the chances that we accidentally add experimental APIs to the stable builds before we intend. I added corresponding methods for the focus and skip APIs: - `fit` -> `fit.experimental` - `it.only` -> `it.only.experimental` or `it.experimental.only` - `xit` -> `xit.experimental` - `it.skip` -> `it.skip.experimental` or `it.experimental.skip` Since `it` is an alias of `test`, `test.experimental` works, too.
1 parent edc234c commit 349cf5a

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

packages/react-dom/src/__tests__/ReactUpdates-test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,8 +1292,9 @@ describe('ReactUpdates', () => {
12921292
expect(ops).toEqual(['Foo', 'Bar', 'Baz']);
12931293
});
12941294

1295-
if (__EXPERIMENTAL__) {
1296-
it('delays sync updates inside hidden subtrees in Concurrent Mode', () => {
1295+
it.experimental(
1296+
'delays sync updates inside hidden subtrees in Concurrent Mode',
1297+
() => {
12971298
const container = document.createElement('div');
12981299

12991300
function Baz() {
@@ -1368,8 +1369,8 @@ describe('ReactUpdates', () => {
13681369
expect(Scheduler).toFlushAndYield(['Bar']);
13691370
}
13701371
expect(hiddenDiv.innerHTML).toBe('<p>bar 1</p>');
1371-
});
1372-
}
1372+
},
1373+
);
13731374

13741375
it('can render ridiculously large number of roots without triggering infinite update loop error', () => {
13751376
class Foo extends React.Component {

scripts/jest/setupTests.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,5 +205,58 @@ if (process.env.REACT_CLASS_EQUIVALENCE_TEST) {
205205
global.Error = ErrorProxy;
206206
}
207207

208+
const expectExperimentalToFail = async callback => {
209+
if (callback.length > 0) {
210+
throw Error(
211+
'Experimental test helpers do not support `done` callback. Return a ' +
212+
'promise instead.'
213+
);
214+
}
215+
try {
216+
const maybePromise = callback();
217+
if (
218+
maybePromise !== undefined &&
219+
maybePromise !== null &&
220+
typeof maybePromise.then === 'function'
221+
) {
222+
await maybePromise;
223+
}
224+
} catch (error) {
225+
// Failed as expected
226+
return;
227+
}
228+
throw Error(
229+
'Tests marked experimental are expected to fail, but this one passed.'
230+
);
231+
};
232+
233+
const it = global.it;
234+
const fit = global.fit;
235+
const xit = global.xit;
236+
if (__EXPERIMENTAL__) {
237+
it.experimental = it;
238+
fit.experimental = it.only.experimental = it.experimental.only = fit;
239+
xit.experimental = it.skip.experimental = it.experimental.skip = xit;
240+
} else {
241+
it.experimental = (message, callback) => {
242+
it(`[EXPERIMENTAL, SHOULD FAIL] ${message}`, () =>
243+
expectExperimentalToFail(callback));
244+
};
245+
fit.experimental = it.only.experimental = it.experimental.only = (
246+
message,
247+
callback
248+
) => {
249+
fit(`[EXPERIMENTAL, SHOULD FAIL] ${message}`, () =>
250+
expectExperimentalToFail(callback));
251+
};
252+
xit.experimental = it.skip.experimental = it.experimental.skip = (
253+
message,
254+
callback
255+
) => {
256+
xit(`[EXPERIMENTAL, SHOULD FAIL] ${message}`, () =>
257+
expectExperimentalToFail(callback));
258+
};
259+
}
260+
208261
require('jasmine-check').install();
209262
}

0 commit comments

Comments
 (0)