Skip to content

Commit

Permalink
refactor: small fixes (#429)
Browse files Browse the repository at this point in the history
* refactor: fix typo

* refactor: improve test coverage
  • Loading branch information
mdjastrzebski authored Dec 4, 2023
1 parent ce92794 commit 1205605
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { measureFunctionInternal } from '../measure-function';
import { resetHasShownFlagsOutput } from '../output';

// Exponentially slow function
function fib(n: number): number {
Expand Down Expand Up @@ -30,7 +31,7 @@ test('measureFunctionInternal runs specified number of times', () => {
expect(results.stdevCount).toBe(0);
});

test('measureFunctionInternal applies dropsWorst option', () => {
test('measureFunctionInternal applies "warmupRuns" option', () => {
const fn = jest.fn(() => fib(5));
const results = measureFunctionInternal(fn, { runs: 10, warmupRuns: 1 });

Expand All @@ -41,3 +42,24 @@ test('measureFunctionInternal applies dropsWorst option', () => {
expect(results.meanCount).toBe(1);
expect(results.stdevCount).toBe(0);
});

const errorsToIgnore = ['❌ Measure code is running under incorrect Node.js configuration.'];
const realConsole = jest.requireActual('console') as Console;

beforeEach(() => {
jest.spyOn(realConsole, 'error').mockImplementation((message) => {
if (!errorsToIgnore.some((error) => message.includes(error))) {
realConsole.error(message);
}
});
});

test('measureFunctionInternal should log error when running under incorrect node flags', () => {
resetHasShownFlagsOutput();
const results = measureFunctionInternal(jest.fn(), { runs: 1 });

expect(results.runs).toBe(1);
expect(realConsole.error).toHaveBeenCalledWith(`❌ Measure code is running under incorrect Node.js configuration.
Performance test code should be run in Jest with certain Node.js flags to increase measurements stability.
Make sure you use the Reassure CLI and run it using "reassure" command.`);
});
46 changes: 29 additions & 17 deletions packages/reassure-measure/src/__tests__/measure-render.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,34 @@ beforeEach(() => {

test('measureRender run test given number of times', async () => {
const scenario = jest.fn(() => Promise.resolve(null));
const result = await measureRender(<View />, { runs: 20, scenario });
expect(result.runs).toBe(20);
expect(result.durations).toHaveLength(20);
expect(result.counts).toHaveLength(20);
expect(result.meanCount).toBe(1);
expect(result.stdevCount).toBe(0);
const results = await measureRender(<View />, { runs: 20, scenario });
expect(results.runs).toBe(20);
expect(results.durations).toHaveLength(20);
expect(results.counts).toHaveLength(20);
expect(results.meanCount).toBe(1);
expect(results.stdevCount).toBe(0);

// Test is actually run 21 times = 20 runs + 1 warmup runs
expect(scenario).toHaveBeenCalledTimes(21);
});

test('measureRender applies "warmupRuns" option', async () => {
const scenario = jest.fn(() => Promise.resolve(null));
const results = await measureRender(<View />, { runs: 10, scenario });

expect(scenario).toHaveBeenCalledTimes(11);
expect(results.runs).toBe(10);
expect(results.durations).toHaveLength(10);
expect(results.counts).toHaveLength(10);
expect(results.meanCount).toBe(1);
expect(results.stdevCount).toBe(0);
});

test('measureRender should log error when running under incorrect node flags', async () => {
resetHasShownFlagsOutput();
const result = await measureRender(<View />, { runs: 1 });
const results = await measureRender(<View />, { runs: 1 });

expect(result.runs).toBe(1);
expect(results.runs).toBe(1);
expect(realConsole.error).toHaveBeenCalledWith(`❌ Measure code is running under incorrect Node.js configuration.
Performance test code should be run in Jest with certain Node.js flags to increase measurements stability.
Make sure you use the Reassure CLI and run it using "reassure" command.`);
Expand All @@ -41,15 +53,15 @@ function IgnoreChildren(_: React.PropsWithChildren<{}>) {
return <View />;
}

test('measureRender does not meassure wrapper', async () => {
const result = await measureRender(<View />, { wrapper: IgnoreChildren });
expect(result.runs).toBe(10);
expect(result.durations).toHaveLength(10);
expect(result.counts).toHaveLength(10);
expect(result.meanDuration).toBe(0);
expect(result.meanCount).toBe(0);
expect(result.stdevDuration).toBe(0);
expect(result.stdevCount).toBe(0);
test('measureRender does not measure wrapper execution', async () => {
const results = await measureRender(<View />, { wrapper: IgnoreChildren });
expect(results.runs).toBe(10);
expect(results.durations).toHaveLength(10);
expect(results.counts).toHaveLength(10);
expect(results.meanDuration).toBe(0);
expect(results.meanCount).toBe(0);
expect(results.stdevDuration).toBe(0);
expect(results.stdevCount).toBe(0);
});

function Wrapper({ children }: React.PropsWithChildren<{}>) {
Expand Down
4 changes: 2 additions & 2 deletions packages/reassure-measure/src/measure-function.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { performance } from 'perf_hooks';
import { config } from './config';
import type { MeasureResults } from './types';
import { type RunResult, processRunResults } from './measure-helpers';
import { showFlagsOuputIfNeeded, writeTestStats } from './output';
import { showFlagsOutputIfNeeded, writeTestStats } from './output';

interface MeasureFunctionOptions {
runs?: number;
Expand All @@ -20,7 +20,7 @@ export function measureFunctionInternal(fn: () => void, options?: MeasureFunctio
const runs = options?.runs ?? config.runs;
const warmupRuns = options?.warmupRuns ?? config.warmupRuns;

showFlagsOuputIfNeeded();
showFlagsOutputIfNeeded();

const runResults: RunResult[] = [];
for (let i = 0; i < runs + warmupRuns; i += 1) {
Expand Down
4 changes: 2 additions & 2 deletions packages/reassure-measure/src/measure-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { logger } from '@callstack/reassure-logger';
import { config } from './config';
import { RunResult, processRunResults } from './measure-helpers';
import { showFlagsOuputIfNeeded, writeTestStats } from './output';
import { showFlagsOutputIfNeeded, writeTestStats } from './output';
import { resolveTestingLibrary } from './testingLibrary';
import type { MeasureResults } from './types';

Expand Down Expand Up @@ -32,7 +32,7 @@ export async function measureRender(ui: React.ReactElement, options?: MeasureOpt

const { render, cleanup } = resolveTestingLibrary();

showFlagsOuputIfNeeded();
showFlagsOutputIfNeeded();

const runResults: RunResult[] = [];
let hasTooLateRender = false;
Expand Down
2 changes: 1 addition & 1 deletion packages/reassure-measure/src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function clearTestStats(outputFilePath: string = config.outputFile)

let hasShownFlagsOutput = false;

export function showFlagsOuputIfNeeded() {
export function showFlagsOutputIfNeeded() {
if (hasShownFlagsOutput) {
return;
}
Expand Down

0 comments on commit 1205605

Please sign in to comment.