Home / Function/ createLogAssertion() — react Function Reference

createLogAssertion() — react Function Reference

Architecture documentation for the createLogAssertion() function in consoleMock.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  05187c53_aa67_18f7_b65b_4debec66866c["createLogAssertion()"]
  9565063b_3ea2_9cbf_86d6_3ee1e66bf4cc["consoleMock.js"]
  05187c53_aa67_18f7_b65b_4debec66866c -->|defined in| 9565063b_3ea2_9cbf_86d6_3ee1e66bf4cc
  da0fb4bf_312b_2b6a_a7bb_db7c57da0c1b["normalizeExpectedMessage()"]
  05187c53_aa67_18f7_b65b_4debec66866c -->|calls| da0fb4bf_312b_2b6a_a7bb_db7c57da0c1b
  0fe2f8cd_c150_9d2c_71e5_070117635df6["normalizeCodeLocInfo()"]
  05187c53_aa67_18f7_b65b_4debec66866c -->|calls| 0fe2f8cd_c150_9d2c_71e5_070117635df6
  4a201017_c5d4_ab2f_492d_01639fabcea7["isLikelyAComponentStack()"]
  05187c53_aa67_18f7_b65b_4debec66866c -->|calls| 4a201017_c5d4_ab2f_492d_01639fabcea7
  a656865c_2f8d_4b54_0873_30f9ede0eb80["isLikelyAnErrorStackTrace()"]
  05187c53_aa67_18f7_b65b_4debec66866c -->|calls| a656865c_2f8d_4b54_0873_30f9ede0eb80
  style 05187c53_aa67_18f7_b65b_4debec66866c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/internal-test-utils/consoleMock.js lines 246–559

export function createLogAssertion(
  consoleMethod,
  matcherName,
  clearObservedErrors,
) {
  function logName() {
    switch (consoleMethod) {
      case 'log':
        return 'log';
      case 'error':
        return 'error';
      case 'warn':
        return 'warning';
    }
  }

  return function assertConsoleLog(expectedMessages, options = {}) {
    if (__DEV__) {
      // eslint-disable-next-line no-inner-declarations
      function throwFormattedError(message) {
        const error = new Error(
          `${chalk.dim(matcherName)}(${chalk.red(
            'expected',
          )})\n\n${message.trim()}`,
        );
        Error.captureStackTrace(error, assertConsoleLog);
        throw error;
      }

      // Warn about incorrect usage first arg.
      if (!Array.isArray(expectedMessages)) {
        throwFormattedError(
          `Expected messages should be an array of strings ` +
            `but was given type "${typeof expectedMessages}".`,
        );
      }

      // Warn about incorrect usage second arg.
      if (options != null) {
        if (typeof options !== 'object' || Array.isArray(options)) {
          throwFormattedError(
            `The second argument should be an object. ` +
              'Did you forget to wrap the messages into an array?',
          );
        }
      }

      const observedLogs = clearObservedErrors();
      const receivedLogs = [];
      const missingExpectedLogs = Array.from(expectedMessages);

      const unexpectedLogs = [];
      const unexpectedMissingErrorStack = [];
      const unexpectedIncludingErrorStack = [];
      const logsMismatchingFormat = [];
      const logsWithExtraComponentStack = [];
      const stackTracePlaceholderMisuses = [];

      // Loop over all the observed logs to determine:
      //   - Which expected logs are missing
      //   - Which received logs are unexpected
      //   - Which logs have a component stack
      //   - Which logs have the wrong format
      //   - Which logs have extra stacks
      for (let index = 0; index < observedLogs.length; index++) {
        const log = observedLogs[index];
        const [format, ...args] = log;
        const message = util.format(format, ...args);

        // Ignore uncaught errors reported by jsdom
        // and React addendums because they're too noisy.
        if (shouldIgnoreConsoleError(format, args)) {
          return;
        }

        let expectedMessage;
        const expectedMessageOrArray = expectedMessages[index];
        if (typeof expectedMessageOrArray === 'string') {
          expectedMessage = normalizeExpectedMessage(expectedMessageOrArray);
        } else if (expectedMessageOrArray != null) {
          throwFormattedError(

Domain

Subdomains

Frequently Asked Questions

What does createLogAssertion() do?
createLogAssertion() is a function in the react codebase, defined in packages/internal-test-utils/consoleMock.js.
Where is createLogAssertion() defined?
createLogAssertion() is defined in packages/internal-test-utils/consoleMock.js at line 246.
What does createLogAssertion() call?
createLogAssertion() calls 4 function(s): isLikelyAComponentStack, isLikelyAnErrorStackTrace, normalizeCodeLocInfo, normalizeExpectedMessage.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free