Skip to content

Commit

Permalink
Add support for focused and skipped tests in React Native integration…
Browse files Browse the repository at this point in the history
… tests (#47559)

Summary:
Pull Request resolved: #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
  • Loading branch information
rubennorte authored and facebook-github-bot committed Nov 14, 2024
1 parent 759000c commit 35e209a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 25 deletions.
3 changes: 2 additions & 1 deletion jest/integration/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,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,
Expand Down
118 changes: 94 additions & 24 deletions jest/integration/runtime/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,82 @@ const tests: Array<{
title: string,
ancestorTitles: Array<string>,
implementation: () => mixed,
isFocused: boolean,
isSkipped: boolean,
result?: TestCaseResult,
}> = [];

const ancestorTitles: Array<string> = [];

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

Expand Down Expand Up @@ -138,29 +197,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: TestCaseResult = {
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({
Expand Down

0 comments on commit 35e209a

Please sign in to comment.