Test Utilities

The test utilities are exported/global objects available in the test runner. These are referred to as "helpers", and the included helpers are exported from https://github.com/freeCodeCamp/freeCodeCampOS/blob/main/.freeCodeCamp/tooling/test-utils.js.

Many of the exported functions are convinience wrappers around Nodejs' fs and child_process modules. Specifically, they make use of the global ROOT variable to run the functions 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 __helpers.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 __helpers.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 __helpers.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 __helpers.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 __helpers.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 __helpers.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 __helpers.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 __helpers.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 __helpers.importSansCache(
  'learn-x-by-building-y/index.js'
);