Skip to content

Commit

Permalink
[skip ci] Add support for (before|after)(Each|All) methods (facebook#…
Browse files Browse the repository at this point in the history
…48820)

Summary:

Changelog: [Internal]
Add capability to setup / teardown tests

Differential Revision: D68454176
  • Loading branch information
andrewdacenko authored and facebook-github-bot committed Jan 23, 2025
1 parent dab9b3b commit 944dfa0
Show file tree
Hide file tree
Showing 5 changed files with 452 additions and 31 deletions.
265 changes: 234 additions & 31 deletions packages/react-native-fantom/runtime/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import type {SnapshotConfig, TestSnapshotResults} from './snapshotContext';
import expect from './expect';
import {createMockFunction} from './mocks';
import {setupSnapshotConfig, snapshotContext} from './snapshotContext';
import nullthrows from 'nullthrows';
import NativeFantom from 'react-native/src/private/specs/modules/NativeFantom';

export type TestCaseResult = {
ancestorTitles: Array<string>,
title: string,
ancestorTitles: Array<string>,
fullName: string,
status: 'passed' | 'failed' | 'pending',
duration: number,
Expand All @@ -40,43 +39,114 @@ export type TestSuiteResult =
},
};

const tests: Array<{
type FocusState = {
focused: boolean,
skipped: boolean,
};

type Spec = {
...FocusState,
title: string,
ancestorTitles: Array<string>,
context: Context,
implementation: () => mixed,
isFocused: boolean,
isSkipped: boolean,
result?: TestCaseResult,
}> = [];
};

const ancestorTitles: Array<string> = [];
type Suite = Spec | Context;

type Hook = () => void;

type RootContext = {
...FocusState,
afterAllHooks: Hook[],
afterEachHooks: Hook[],
beforeAllHooks: Hook[],
beforeEachHooks: Hook[],
children: Array<Suite>,
};

type Context =
| RootContext
| {
...RootContext,
title: string,
context: Context,
};

let currentContext: Context = {
beforeAllHooks: [],
beforeEachHooks: [],
afterAllHooks: [],
afterEachHooks: [],
children: [],
focused: false,
skipped: false,
};

const globalModifiers: Array<'focused' | 'skipped'> = [];

const globalDescribe = (global.describe = (
title: string,
implementation: () => mixed,
) => {
ancestorTitles.push(title);
const parentContext = currentContext;
const {focused, skipped} = getFocusState();
const childContext: Context = {
title,
context: parentContext,
afterAllHooks: [],
afterEachHooks: [],
beforeAllHooks: [],
beforeEachHooks: [],
children: [],
focused: focused,
skipped: skipped,
};
currentContext.children.push(childContext);
currentContext = childContext;
implementation();
ancestorTitles.pop();
currentContext = parentContext;
});

global.afterAll = (implementation: () => void) => {
currentContext.afterAllHooks.push(implementation);
};

global.afterEach = (implementation: () => void) => {
currentContext.afterEachHooks.push(implementation);
};

global.beforeAll = (implementation: () => void) => {
currentContext.beforeAllHooks.push(implementation);
};

global.beforeEach = (implementation: () => void) => {
currentContext.beforeEachHooks.push(implementation);
};

function getFocusState(): {focused: boolean, skipped: boolean} {
const focused =
globalModifiers.length > 0 &&
globalModifiers[globalModifiers.length - 1] === 'focused';
const skipped =
globalModifiers.length > 0 &&
globalModifiers[globalModifiers.length - 1] === 'skipped';
return {focused, skipped};
}

const globalIt =
(global.it =
global.test =
(title: string, implementation: () => mixed) =>
tests.push({
(title: string, implementation: () => mixed) => {
const {focused, skipped} = getFocusState();
currentContext.children.push({
title,
context: currentContext,
implementation,
ancestorTitles: ancestorTitles.slice(),
isFocused:
globalModifiers.length > 0 &&
globalModifiers[globalModifiers.length - 1] === 'focused',
isSkipped:
globalModifiers.length > 0 &&
globalModifiers[globalModifiers.length - 1] === 'skipped',
}));
focused: focused,
skipped: skipped,
});
});

// $FlowExpectedError[prop-missing]
global.fdescribe = global.describe.only = (
Expand Down Expand Up @@ -142,32 +212,138 @@ function runWithGuard(fn: () => void) {
}
}

const focusCache = new Map<Suite, boolean>();

function isFocusedSuite(suite: Suite): boolean {
const cached = focusCache.get(suite);
if (cached != null) {
return cached;
}

if (isSkipped(suite)) {
focusCache.set(suite, false);
return false;
}

if ('children' in suite) {
const hasFocused = suite.children.some(isFocusedSuite);
focusCache.set(suite, hasFocused);
return hasFocused;
}

focusCache.set(suite, suite.focused);
return suite.focused;
}

const skippedCache = new Map<Suite, boolean>();
function isSkipped(suite: Suite): boolean {
const cached = skippedCache.get(suite);
if (cached != null) {
return cached;
}

if (suite.skipped) {
skippedCache.set(suite, true);
return true;
}

if ('context' in suite) {
const skipped = isSkipped(suite.context);
skippedCache.set(suite, skipped);
return skipped;
}

skippedCache.set(suite, false);
return false;
}

function getContextTitle(context: Context): string[] {
if (context.context == null) {
return [];
}

const titles = [context.title];
if (context.context) {
titles.push(...getContextTitle(context.context));
}
return titles.reverse();
}

function getSuiteFocusState(suite: Suite): FocusState {
if (suite.focused) {
return {focused: true, skipped: false};
}

if (suite.skipped) {
return {focused: false, skipped: true};
}

if ('context' in suite) {
return getSuiteFocusState(suite.context);
}

return {focused: false, skipped: false};
}

function invokeHooks(
context: Context,
hookType: 'beforeEachHooks' | 'afterEachHooks',
) {
const contextStack = [];
let current: ?Context = context;
while (current != null) {
if (hookType === 'beforeEachHooks') {
contextStack.unshift(current);
} else {
contextStack.push(current);
}
current = current.context;
}

for (const c of contextStack) {
for (const hook of c[hookType]) {
hook();
}
}
}

function executeTests() {
const hasFocusedTests = tests.some(test => test.isFocused);
const isFocusedRun = isFocusedSuite(currentContext);

const results: Array<TestCaseResult> = [];
executeSuite(currentContext);
reportTestSuiteResult({
testResults: results,
});

for (const test of tests) {
function executeSpec(spec: Spec) {
const ancestorTitles = getContextTitle(spec.context);
const result: TestCaseResult = {
title: test.title,
fullName: [...test.ancestorTitles, test.title].join(' '),
ancestorTitles: test.ancestorTitles,
title: spec.title,
ancestorTitles,
fullName: [...ancestorTitles, spec.title].join(' '),
status: 'pending',
duration: 0,
failureMessages: [],
numPassingAsserts: 0,
snapshotResults: {},
};

test.result = result;
spec.result = result;
snapshotContext.setTargetTest(result.fullName);

if (!test.isSkipped && (!hasFocusedTests || test.isFocused)) {
const isFocusedSpec = isFocusedSuite(spec);
if (!isSkipped(spec) && (isFocusedSpec || !isFocusedRun)) {
let status;
let error;

const start = Date.now();

try {
test.implementation();
invokeHooks(spec.context, 'beforeEachHooks');
spec.implementation();
invokeHooks(spec.context, 'afterEachHooks');

status = 'passed';
} catch (e) {
error = e;
Expand All @@ -183,11 +359,38 @@ function executeTests() {

result.snapshotResults = snapshotContext.getSnapshotResults();
}
results.push(result);
}

reportTestSuiteResult({
testResults: tests.map(test => nullthrows(test.result)),
});
function executeContext(context: Context) {
const isFocusedContext = isFocusedSuite(context);
const shouldRunHooks =
!isSkipped(context) && (isFocusedContext || !isFocusedRun);

if (shouldRunHooks) {
for (const beforeAllHook of context.beforeAllHooks) {
beforeAllHook();
}
}

for (const child of context.children) {
executeSuite(child);
}

if (shouldRunHooks) {
for (const afterAllHook of context.afterAllHooks) {
afterAllHook();
}
}
}

function executeSuite(suite: Suite) {
if ('children' in suite) {
executeContext(suite);
} else {
executeSpec(suite);
}
}
}

function reportTestSuiteResult(testSuiteResult: TestSuiteResult): void {
Expand Down
Loading

0 comments on commit 944dfa0

Please sign in to comment.