Skip to content

Commit

Permalink
Remove duplicated logic of error tests (#6358)
Browse files Browse the repository at this point in the history
## Summary

Remove the logic behind `WARN` and `FAILING` test decorators.

## Test plan
  • Loading branch information
Latropos authored Aug 12, 2024
1 parent 9a33d13 commit dda25d6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@ const valueRegistry = testRunner.getValueRegistry();

type DescribeFunction = (name: string, buildSuite: BuildFunction) => void;
type TestFunction = (name: string, buildTest: BuildFunction) => void;
type TestFunctionWithWarning = (name: string, warningMessage: string, buildTest: BuildFunction) => void;
type TestEachFunction = <T>(
examples: Array<T>,
) => (name: string, testCase: (example: T, index: number) => void | Promise<void>) => void;
type TestEachFunctionWithWarning = <T>(
examples: Array<T>,
) => (name: string, expectedWarning: string, testCase: (example: T, index: number) => void | Promise<void>) => void;
type DecoratedTestFunction = TestFunction & { each: TestEachFunction };
type DecoratedTestFunctionWithWarning = TestFunctionWithWarning & { each: TestEachFunctionWithWarning };

const describeBasic = (name: string, buildSuite: BuildFunction) => {
testRunner.describe(name, buildSuite, null);
Expand Down Expand Up @@ -54,32 +49,12 @@ const testOnly: DecoratedTestFunction = (name: string, testCase: BuildFunction)
testOnly.each = <T>(examples: Array<T>) => {
return testRunner.testEach(examples, TestDecorator.ONLY);
};
const testFailing: DecoratedTestFunctionWithWarning = (
name: string,
expectedWarning: string,
testCase: BuildFunction,
) => {
testRunner.test(name, testCase, TestDecorator.FAILING, expectedWarning);
};
testFailing.each = <T>(examples: Array<T>) => {
return testRunner.testEachErrorMsg(examples, TestDecorator.FAILING);
};
const testWarn: DecoratedTestFunctionWithWarning = (name: string, expectedWarning: string, testCase: BuildFunction) => {
testRunner.test(name, testCase, TestDecorator.WARN, expectedWarning);
};
testWarn.each = <T>(examples: Array<T>) => {
return testRunner.testEachErrorMsg(examples, TestDecorator.WARN);
};

type TestType = DecoratedTestFunction &
Record<TestDecorator.SKIP | TestDecorator.ONLY, DecoratedTestFunction> &
Record<TestDecorator.FAILING | TestDecorator.WARN, DecoratedTestFunctionWithWarning>;
type TestType = DecoratedTestFunction & Record<TestDecorator.SKIP | TestDecorator.ONLY, DecoratedTestFunction>;

export const test = <TestType>testBasic;
test.skip = testSkip;
test.only = testOnly;
test.failing = testFailing;
test.warn = testWarn;

export function beforeAll(job: () => void) {
testRunner.beforeAll(job);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export class TestRunner {
private _currentTestCase: TestCase | null = null;
private _renderHook: (component: ReactElement<Component> | null) => void = () => {};
private _includesOnly: boolean = false;
private _syncUIRunner: SyncUIRunner = new SyncUIRunner();
private _renderLock: RenderLock = new RenderLock();
private _testSummary: TestSummaryLogger = new TestSummaryLogger();
private _windowDimensionsMocker: WindowDimensionsMocker = new WindowDimensionsMocker();
Expand Down Expand Up @@ -130,7 +129,7 @@ export class TestRunner {
});
}

public test(name: string, run: BuildFunction, decorator: TestDecorator | null, warningMessage = '') {
public test(name: string, run: BuildFunction, decorator: TestDecorator | null) {
assertTestSuite(this._currentTestSuite);
if (decorator === TestDecorator.ONLY) {
this._includesOnly = true;
Expand All @@ -143,26 +142,9 @@ export class TestRunner {
errors: [],
skip: decorator === TestDecorator.SKIP || this._currentTestSuite.decorator === DescribeDecorator.SKIP,
decorator,
warningMessage,
});
}

public testEachErrorMsg<T>(examples: Array<T>, decorator: TestDecorator) {
return (name: string, expectedWarning: string, testCase: (example: T, index: number) => void | Promise<void>) => {
examples.forEach((example, index) => {
const currentTestCase = async () => {
await testCase(example, index);
};
this.test(
formatTestName(name, example, index),
currentTestCase,
decorator,
formatTestName(expectedWarning, example, index),
);
});
};
}

public testEach<T>(examples: Array<T>, decorator: TestDecorator | null) {
return (name: string, testCase: (example: T, index: number) => void | Promise<void>) => {
examples.forEach((example, index) => {
Expand Down Expand Up @@ -277,15 +259,7 @@ export class TestRunner {
if (testSuite.beforeEach) {
await testSuite.beforeEach();
}

if (testCase.decorator === TestDecorator.FAILING || testCase.decorator === TestDecorator.WARN) {
const [restoreConsole, checkErrors] = await this.mockConsole(testCase);
await testCase.run();
await restoreConsole();
checkErrors();
} else {
await testCase.run();
}
await testCase.run();

this._testSummary.showTestCaseSummary(testCase, testSuite.nestingLevel);

Expand Down Expand Up @@ -348,54 +322,4 @@ export class TestRunner {
}, CHECK_INTERVAL);
});
}

private async mockConsole(testCase: TestCase): Promise<[() => Promise<void>, () => void]> {
const counterUI = makeMutable(0);
let counterJS = 0;
const recordedMessage = makeMutable('');

const originalError = console.error;
const originalWarning = console.warn;

const incrementJS = () => {
counterJS++;
};
const mockedConsoleFunction = (message: string) => {
'worklet';
if (_WORKLET) {
counterUI.value++;
} else {
incrementJS();
}
recordedMessage.value = message.split('\n\nThis error is located at:')[0];
};
console.error = mockedConsoleFunction;
console.warn = mockedConsoleFunction;
await this._syncUIRunner.runOnUIBlocking(() => {
'worklet';
console.error = mockedConsoleFunction;
console.warn = mockedConsoleFunction;
});

const restoreConsole = async () => {
console.error = originalError;
console.warn = originalWarning;
await this._syncUIRunner.runOnUIBlocking(() => {
'worklet';
console.error = originalError;
console.warn = originalWarning;
});
};

const checkErrors = () => {
if (testCase.decorator !== TestDecorator.WARN && testCase.decorator !== TestDecorator.FAILING) {
return;
}
const count = counterUI.value + counterJS;
this.expect(count).toBe(1);
this.expect(recordedMessage.value).toBe(testCase.warningMessage);
};

return [restoreConsole, checkErrors];
}
}
11 changes: 2 additions & 9 deletions apps/common-app/src/examples/RuntimeTests/ReJest/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ export enum DescribeDecorator {
export enum TestDecorator {
ONLY = 'only',
SKIP = 'skip',
FAILING = 'failing',
WARN = 'warn',
}

export type BuildFunction = () => void | Promise<void>;
Expand All @@ -45,13 +43,8 @@ export type TestCase = {
callsRegistry: Record<string, CallTracker>;
errors: string[];
skip?: boolean;
} & (
| {
decorator: TestDecorator.WARN | TestDecorator.FAILING;
warningMessage: string;
}
| { decorator: Exclude<TestDecorator, TestDecorator.WARN | TestDecorator.FAILING> | null }
);
decorator?: TestDecorator | null;
};

export type TestSuite = {
name: string;
Expand Down

0 comments on commit dda25d6

Please sign in to comment.