Skip to content

Commit

Permalink
Re-org beforeValue and testCase helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed May 12, 2022
1 parent 09bf749 commit 313ff52
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
CachedBeaconStateAltair,
beforeProcessEpoch,
} from "../../../../src";
import {getNetworkCachedState} from "../../../utils/testFileCache";
import {beforeValue, LazyValue} from "../../util";
import {getNetworkCachedState, beforeValue, LazyValue} from "../../../utils";
import {StateEpoch} from "../../types";
import {altairState} from "../../params";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
CachedBeaconStatePhase0,
beforeProcessEpoch,
} from "../../../../src";
import {getNetworkCachedState} from "../../../utils/testFileCache";
import {beforeValue, LazyValue} from "../../util";
import {getNetworkCachedState, beforeValue, LazyValue} from "../../../utils";
import {processParticipationRecordUpdates} from "../../../../src/phase0/epoch/processParticipationRecordUpdates";
import {StateEpoch} from "../../types";
import {phase0State} from "../../params";
Expand Down
38 changes: 0 additions & 38 deletions packages/beacon-state-transition/test/perf/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,41 +439,3 @@ export function generateTestCachedBeaconStateOnlyValidators({
index2pubkey,
});
}

const initialValue = null;
export type LazyValue<T> = {value: T};

/**
* Register a callback to compute a value in the before() block of mocha tests
* ```ts
* const state = beforeValue(() => getState())
* it("test", () => {
* doTest(state.value)
* })
* ```
*/
export function beforeValue<T>(fn: () => T | Promise<T>, timeout?: number): LazyValue<T> {
let value: T = (initialValue as unknown) as T;

before(async function () {
this.timeout(timeout ?? 300_000);
value = await fn();
});

return new Proxy<{value: T}>(
{value},
{
get: function (target, prop) {
if (prop === "value") {
if (value === initialValue) {
throw Error("beforeValue has not yet run the before() block");
} else {
return value;
}
} else {
return undefined;
}
},
}
);
}
36 changes: 36 additions & 0 deletions packages/beacon-state-transition/test/utils/beforeValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export type LazyValue<T> = {value: T};

/**
* Register a callback to compute a value in the before() block of mocha tests
* ```ts
* const state = beforeValue(() => getState())
* it("test", () => {
* doTest(state.value)
* })
* ```
*/
export function beforeValue<T>(fn: () => T | Promise<T>, timeout?: number): LazyValue<T> {
let value: T = (null as unknown) as T;

before(async function () {
this.timeout(timeout ?? 300_000);
value = await fn();
});

return new Proxy<{value: T}>(
{value},
{
get: function (target, prop) {
if (prop === "value") {
if (value === null) {
throw Error("beforeValue has not yet run the before() block");
} else {
return value;
}
} else {
return undefined;
}
},
}
);
}
2 changes: 2 additions & 0 deletions packages/beacon-state-transition/test/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./beforeValue";
export * from "./testFileCache";
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
beforeValue,
getNetworkCachedState,
getNetworkCachedBlock,
} from "@chainsafe/lodestar-beacon-state-transition/test/perf/util";
} from "@chainsafe/lodestar-beacon-state-transition/test/utils";
import {rangeSyncTest} from "@chainsafe/lodestar-beacon-state-transition/test/perf/params";
import {config} from "@chainsafe/lodestar-config/default";
import {SAFE_SLOTS_TO_IMPORT_OPTIMISTICALLY, SLOTS_PER_EPOCH} from "@chainsafe/lodestar-params";
Expand Down

0 comments on commit 313ff52

Please sign in to comment.