Skip to main content

Import

import { loggerMock } from "@prismatic-io/spectral/dist/testing";

Signature

export const loggerMock = (): ActionLogger
loggerMock creates an ActionLogger where each log-level method is a jest/vitest spy backed by the matching console method. The metric method is a plain console.log call (not a spy).
You rarely need to call loggerMock directly. Both invoke and invokeTrigger create one automatically and return it as loggerMock in the result. Call loggerMock directly only when you need to inject a logger into a context you build yourself.

Return value

Returns an ActionLogger object.
trace
spy (console.trace)
required
Spy wrapping console.trace.
debug
spy (console.debug)
required
Spy wrapping console.debug.
info
spy (console.info)
required
Spy wrapping console.info.
log
spy (console.log)
required
Spy wrapping console.log.
warn
spy (console.warn)
required
Spy wrapping console.warn.
error
spy (console.error)
required
Spy wrapping console.error.
metric
function (console.log)
Plain console.log call. Not a spy — cannot be asserted against.

Implementation

import { spyOn } from "jest-mock";

export const loggerMock = (): ActionLogger => ({
  metric: console.log as ActionLoggerFunction,
  trace: spyOn(console, "trace") as unknown as ActionLoggerFunction,
  debug: spyOn(console, "debug") as unknown as ActionLoggerFunction,
  info: spyOn(console, "info") as unknown as ActionLoggerFunction,
  log: spyOn(console, "log") as unknown as ActionLoggerFunction,
  warn: spyOn(console, "warn") as unknown as ActionLoggerFunction,
  error: spyOn(console, "error") as unknown as ActionLoggerFunction,
});

Examples

import { action, input } from "@prismatic-io/spectral";
import { invoke } from "@prismatic-io/spectral/dist/testing";
import { describe, expect, it } from "vitest";

const myAction = action({
  display: { label: "My Action", description: "" },
  inputs: {
    name: input({ label: "Name", type: "string" }),
  },
  perform: async (context, { name }) => {
    context.logger.info(`Processing request for ${name}`);
    context.logger.warn("This is a warning");
    return { data: `Hello, ${name}` };
  },
});

describe("myAction", () => {
  it("logs the correct messages", async () => {
    const { loggerMock } = await invoke(myAction, { name: "Alice" });

    expect(loggerMock.info).toHaveBeenCalledWith("Processing request for Alice");
    expect(loggerMock.warn).toHaveBeenCalledWith("This is a warning");
    expect(loggerMock.error).not.toHaveBeenCalled();
  });
});
The spy functions are created with jest-mock’s spyOn, which is compatible with both Jest and Vitest. Use any standard expect(spy).toHaveBeenCalled* matcher.