Test Utilities

The following built-in helpers are available directly in the test context. Many are convenience wrappers around Nodejs' fs and child_process modules, which make use of the global ROOT variable to run relative to the root of the workspace.

controlWrapper

Wraps a function in an interval to retry until it does not throw or times out.

function controlWrapper(
  cb: () => any,
  { timeout = 10_000, stepSize = 250 }
): Promise<ReturnType<cb> | null>;

The callback function must throw for the control wrapper to re-try.

const cb = async () => {
  const flakyFetch = await fetch('http://localhost:3123');
  return flakyFetch.json();
};
const result = await controlWrapper(cb);

getBashHistory

Get the .logs/.bash_history.log file contents

Safety

Throws if file does not exist, or if read permission is denied.

function getBashHistory(): Promise<string>;
const bashHistory = await getBashHistory();

getCommandOutput

Returns the output of a command called from the given path relative to the root of the workspace.

Safety

Throws if path is not a valid POSIX/DOS path, and if promisified exec throws.

function getCommandOutput(
  command: string,
  path = ''
): Promise<{ stdout: string; stderr: string } | Error>;
const { stdout, stderr } = await getCommandOutput('ls');

getCWD

Get the .logs/.cwd.log file contents

Safety

Throws if file does not exist, or if read permission is denied.

function getCWD(): Promise<string>;
const cwd = await getCWD();

getLastCommand

Get the \(n^{th}\) latest line from .logs/.bash_history.log.

Safety

Throws if file does not exist, or if read permission is denied.

function getLastCommand(n = 0): Promise<string>;
const lastCommand = await getLastCommand();

getLastCWD

Get the \(n^{th}\) latest line from .logs/.cwd.log.

Safety

Throws if file does not exist, or if read permission is denied.

function getLastCWD(n = 0): Promise<string>;
const lastCWD = await getLastCWD();

getTemp

Get the .logs/.temp.log file contents.

Safety

Throws if file does not exist, or if read permission is denied.

function getTemp(): Promise<string>;
const temp = await getTemp();

Note

Use the output of the .temp.log file at your own risk. This file is raw input from the terminal including ANSI escape codes.

Output varies depending on emulator, terminal size, order text is typed, etc. For more info, see https://github.com/freeCodeCamp/solana-curriculum/issues/159

getTerminalOutput

Get the .logs/.terminal_out.log file contents.

Safety

Throws if file does not exist, or if read permission is denied.

function getTerminalOutput(): Promise<string>;
const terminalOutput = await getTerminalOutput();

importSansCache

Import a module side-stepping Nodejs' cache - cache-busting imports.

Safety

Throws if path is not a valid POSIX/DOS path, and if the import throws.

function importSansCache(path: string): Promise<any>;
const { exportedFile } = await importSansCache(
  'learn-x-by-building-y/index.js'
);