@commerce-klaus/vitest-sfcc

中文文档
1.1.2发布于 2026/10/30MITTypeScriptESM + CJS构建来源已验证近 30 天下载 2124

Cartridge-aware SFCC runtime and dependency mocking for Vitest

原始使用文档
文档目录6 节
  1. Highlights
  2. When to use this package
  3. Install
  4. Usage
  5. Documentation
  6. License

NPM version Downloads

@commerce-klaus/vitest-sfcc

Cartridge-aware SFCC runtime and dependency mocking for Vitest.

Highlights

  • Resolves dw/*, */, ~/, cartridge aliases, relative modules, and module.superModule
  • Infers cartridge order from configuration, environment, jsconfig, or site.xml
  • Filters test cartridge paths through central architecture assignments
  • Provides typed, inspectable SFCC globals and focused runtime modules and harnesses
  • Loads effective Custom API operations from api.json and OAS metadata
  • Loads effective hooks from project metadata with typed invocation for known Salesforce hooks
  • Replaces module dependencies without proxyquire
  • Exposes runtime APIs and types through @commerce-klaus/vitest-sfcc/runtime

When to use this package

Use this package when cartridge code must run in Vitest. It includes SFCC module resolution, CommonJS transformation, platform-module fallbacks, dependency mocking, and test harnesses.

Use @commerce-klaus/vite-plugin-sfcc-modules only when another Vite-based tool needs cartridge-aware module resolution without the test runtime. You do not need to install or configure that plugin alongside vitest-sfcc.

Install

pnpm add -D @commerce-klaus/vitest-sfcc vitest
npm install -D @commerce-klaus/vitest-sfcc vitest
yarn add -D @commerce-klaus/vitest-sfcc vitest
vp install -D @commerce-klaus/vitest-sfcc vitest

Usage

import sfccVitest from "@commerce-klaus/vitest-sfcc"
import { defineConfig } from "vite-plus"

export default defineConfig({
  plugins: [
    sfccVitest({
      basePath: "./cartridges",
      cartridgePath: ["app_custom", "app_storefront_base"],
    }),
  ],
})

Register a dependency replacement before dynamically importing the subject:

import { getSfccRuntime } from "@commerce-klaus/vitest-sfcc"

getSfccRuntime().mock("*/cartridge/scripts/provider", providerMock)
const subject = await import("../cartridge/scripts/subject.js")

Runtime-only APIs and types are also available from the lightweight subpath:

import { resetSfccRuntime, type SfccModule } from "@commerce-klaus/vitest-sfcc/runtime"

Load the implementation selected by Custom API metadata and cartridge precedence:

import { loadSfccCustomApi } from "@commerce-klaus/vitest-sfcc"

const api = await loadSfccCustomApi("products", "getProduct")
const result = await api.invoke({
  pathParameters: { productId: "sample-product" },
})

Load and invoke the implementation selected by hooks.json and cartridge precedence:

import { loadSfccHook } from "@commerce-klaus/vitest-sfcc"

const hook = await loadSfccHook("app.payment.authorize")
const result = hook.call("authorize", "payment-1")

Consumer projects only need to install @commerce-klaus/vitest-sfcc; its framework-independent runtime remains an internal dependency.

Documentation

See the complete configuration and testing reference.

License

MIT

版本与发布记录

@commerce-klaus/vitest-sfcc 共发布 12 个版本,首次发布于 2026/09/03;过去 12 个月发布了 12 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.2latest2026/10/3011 天前
1.1.12026/10/2912 天前
1.1.02026/10/2417 天前
1.0.42026/10/051 个月前
1.0.32026/10/011 个月前
1.0.22026/09/281 个月前

关于 @commerce-klaus/vitest-sfcc 的常见问题

@commerce-klaus/vitest-sfcc 是什么?

Cartridge-aware SFCC runtime and dependency mocking for Vitest

如何安装 @commerce-klaus/vitest-sfcc?

在项目目录运行 npm install @commerce-klaus/vitest-sfcc,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vite(>=5.0.0)、vitest(>=2.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@commerce-klaus/vitest-sfcc 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@commerce-klaus/vitest-sfcc 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@commerce-klaus/vitest-sfcc 可以商用吗?

@commerce-klaus/vitest-sfcc 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@commerce-klaus/vitest-sfcc 的下载量有多少?

根据 npm 官方下载统计,@commerce-klaus/vitest-sfcc 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,124 次,日均约 71 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@commerce-klaus/vitest-sfcc 还在维护吗?

最新版本 1.1.2 发布于 2026/10/30(11 天前);至今共发布 12 个版本,其中过去 12 个月发布了 12 个;首次发布于 2026/09/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。