From 88174ca717e55a26d275b4d022c1004d7df0e2d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Thu, 14 Nov 2024 02:39:42 -0800 Subject: [PATCH] Add support for focused and skipped tests in React Native integration tests (#47559) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/47559 Changelog: [internal] Adds support for focused and skipped tests and describe blocks: * `fdescribe` / `describe.only` * `xdescribe` / `describe.skip` * `fit` / `it.only` / `test.only` * `xit` / `it.skip` / `xtest` / `test.skip` Reviewed By: rshest Differential Revision: D65769325 --- jest/integration/runner/index.js | 3 +- jest/integration/runtime/setup.js | 116 +++++++++++++++++++++++------- 2 files changed, 94 insertions(+), 25 deletions(-) diff --git a/jest/integration/runner/index.js b/jest/integration/runner/index.js index 41153e701d24d0..5779cdeaf57f10 100644 --- a/jest/integration/runner/index.js +++ b/jest/integration/runner/index.js @@ -180,7 +180,8 @@ module.exports = async function runTest( .length, numFailingTests: testResults.filter(test => test.status === 'failed') .length, - numPendingTests: 0, + numPendingTests: testResults.filter(test => test.status === 'pending') + .length, numTodoTests: 0, skipped: false, testResults, diff --git a/jest/integration/runtime/setup.js b/jest/integration/runtime/setup.js index 9d0c57bf7bb50a..47ea4114e0956f 100644 --- a/jest/integration/runtime/setup.js +++ b/jest/integration/runtime/setup.js @@ -42,18 +42,75 @@ const tests: Array<{ const ancestorTitles: Array = []; -global.describe = (title: string, implementation: () => mixed) => { +const globalModifiers: Array<'focused' | 'skipped'> = []; + +const globalDescribe = (global.describe = ( + title: string, + implementation: () => mixed, +) => { ancestorTitles.push(title); implementation(); ancestorTitles.pop(); +}); + +const globalIt = + (global.it = + global.test = + (title: string, implementation: () => mixed) => + tests.push({ + title, + implementation, + ancestorTitles: ancestorTitles.slice(), + isFocused: + globalModifiers.length > 0 && + globalModifiers[globalModifiers.length - 1] === 'focused', + isSkipped: + globalModifiers.length > 0 && + globalModifiers[globalModifiers.length - 1] === 'skipped', + })); + +// $FlowExpectedError[prop-missing] +global.fdescribe = global.describe.only = ( + title: string, + implementation: () => mixed, +) => { + globalModifiers.push('focused'); + globalDescribe(title, implementation); + globalModifiers.pop(); }; -global.it = (title: string, implementation: () => mixed) => - tests.push({ - title, - implementation, - ancestorTitles: ancestorTitles.slice(), - }); +// $FlowExpectedError[prop-missing] +global.it.only = + global.fit = + // $FlowExpectedError[prop-missing] + global.test.only = + (title: string, implementation: () => mixed) => { + globalModifiers.push('focused'); + globalIt(title, implementation); + globalModifiers.pop(); + }; + +// $FlowExpectedError[prop-missing] +global.xdescribe = global.describe.skip = ( + title: string, + implementation: () => mixed, +) => { + globalModifiers.push('skipped'); + globalDescribe(title, implementation); + globalModifiers.pop(); +}; + +// $FlowExpectedError[prop-missing] +global.it.skip = + global.xit = + // $FlowExpectedError[prop-missing] + global.test.skip = + global.xtest = + (title: string, implementation: () => mixed) => { + globalModifiers.push('skipped'); + globalIt(title, implementation); + globalModifiers.pop(); + }; // flowlint unsafe-getters-setters:off @@ -138,29 +195,40 @@ function runWithGuard(fn: () => void) { } function executeTests() { - for (const test of tests) { - let status; - let error; - - const start = Date.now(); - - try { - test.implementation(); - status = 'passed'; - } catch (e) { - error = e; - status = 'failed'; - } + const hasFocusedTests = tests.some(test => test.isFocused); - test.result = { + for (const test of tests) { + const result: TestResult = { title: test.title, fullName: [...test.ancestorTitles, test.title].join(' '), ancestorTitles: test.ancestorTitles, - status, - duration: Date.now() - start, - failureMessages: status === 'failed' && error ? [error.message] : [], + status: 'pending', + duration: 0, + failureMessages: [], numPassingAsserts: 0, }; + + test.result = result; + + if (!test.isSkipped && (!hasFocusedTests || test.isFocused)) { + let status; + let error; + + const start = Date.now(); + + try { + test.implementation(); + status = 'passed'; + } catch (e) { + error = e; + status = 'failed'; + } + + result.status = status; + result.duration = Date.now() - start; + result.failureMessages = + status === 'failed' && error ? [error.message] : []; + } } reportTestSuiteResult({