testing

作者: contentstack

How to run and extend tests — Mocha unit, sanity API, Jest TypeScript; env and mocks.

npx skills add https://github.com/contentstack/contentstack-management-javascript --skill testing

Testing – Contentstack Management JavaScript SDK

When to use

  • Running npm run test:unit, sanity, or Jest.
  • Adding or changing tests under test/unit/ or test/sanity-check/.
  • Configuring env vars for live CMA sanity runs.

Instructions

Commands (from package.json)

GoalCommand
Lint (pre-test hook)npm run lint
Default npm testRuns test:api then test:unitconfirm test:api exists in your branch; else use test:unit
Unit onlynpm run test:unit — Mocha test/unit/index.js, nyc
Unit JSON reportnpm run test:unit:report:json
Sanity + coveragenpm run test:sanity-test
Sanity, no nycnpm run test:sanity-nocov
TypeScript / Jestnpm run test:typescript

Always npm run build before sanity if dist/node/contentstack-management.js must match lib/ (sanity imports the built artifact).

Unit tests (test/unit/)

  • Mocha + Chai + @babel/register; 30s timeout per script.
  • HTTP: mock with Nock, axios-mock-adapter, or stubs — no live CMA.
  • Suites are required from test/unit/index.js — add new *-test.js files and register them there.

Sanity tests (test/sanity-check/)

  • Live CMA calls; orchestrated by test/sanity-check/sanity.js.
  • Authoritative env list: test/sanity-check/utility/testSetup.js (and sanity.js).

Environment variables

Required (core) — setup fails without: EMAIL, PASSWORD, HOST, ORGANIZATION.

Suite-specific (tests skip or degrade when missing):

AreaVariablesNotes
OAuthCLIENT_ID, APP_ID, REDIRECT_URIoauth-test.js
2FATFA_EMAIL, TFA_PASSWORDuser-test.js
MFAMFA_SECRETuser-test.js
Team / shareMEMBER_EMAILteam-test.js, stack-test.js
DAM 2.0DAM_2_0_ENABLED=trueentry-test.js

Optional: PERSONALIZE_HOST, DELETE_DYNAMIC_RESOURCES (defaults in setup). Set HOST to your CMA API host.

Runtime: setup assigns API_KEY, AUTHTOKEN, MANAGEMENT_TOKEN, etc. — do not commit those.

Jest (test/typescript/)

  • jest.config.js: ts-jest, coverage under coverage/.

Hygiene

  • No committed only / skip for CI-needed tests.
  • Never commit real tokens in tests or snapshots.

来自 contentstack 的更多技能

brand-kit-assistant
contentstack
就Contentstack Brand Kit的概念、设置、治理及品牌化AI生成向用户提供建议。将API相关任务路由到正确的Brand Kit功能或…
official
cms-assets
contentstack
为开发人员提供关于在Contentstack中组织、交付和转换资产的建议。涵盖文件夹结构、Image Delivery API转换、发布……
official
cms-branches-aliases
contentstack
指导开发者使用Contentstack分支进行隔离的内容开发,并使用别名实现零停机的内容部署。涵盖分支策略、…
official
cms-data-modeling-best-practices
contentstack
指导开发者在Contentstack中使用最简单可复用的结构来建模内容。该技能解释了何时使用内容类型、引用、全局…
official
cms-entries
contentstack
为开发者提供关于查询、本地化、版本管理、发布和构建Contentstack条目的建议,以实现高效交付。重点关注CDA的使用、引用……
official
cms-environments-publishing
contentstack
为开发人员提供关于配置环境、发布内容、使用交付和预览令牌、利用Sync API以及了解CDN和…的建议
official
cms-live-preview-visual-builder-support-assistant
contentstack
诊断并指导Contentstack Live Preview和Visual Builder的实施。追踪预览上下文,识别损坏的契约,并推荐…
official
cms-localization
contentstack
Advise developers on Contentstack localization: language setup, fallback chains, localized vs unlocalized entries, non-localizable fields, and multi-locale…
official