| name | testing |
|---|---|
| description | How to run and extend tests — Mocha unit, sanity API, Jest TypeScript; env and mocks. |
| Goal | Command |
|---|---|
| Lint (pre-test hook) | npm run lint |
Default npm test |
Runs test:api then test:unit per package.json — confirm test:api is defined in your branch (it may be missing); otherwise use test:unit directly |
| Unit only | npm run test:unit — Mocha test/unit/index.js, nyc coverage |
| Unit JSON report | npm run test:unit:report:json |
| Sanity + coverage | npm run test:sanity-test |
| Sanity, no nyc | npm run test:sanity-nocov |
| TypeScript / Jest | npm run test:typescript |
Always npm run build before sanity if dist/node/contentstack-management.js must match lib/ (sanity imports built output).
- Mocha + Chai + @babel/register; 30s timeout per script.
- HTTP: mock with Nock, axios-mock-adapter, or stubs — no live CMA.
- Suites are
required fromtest/unit/index.js— add new*-test.jsfiles and register them there.
- Live Contentstack CMA calls; orchestrated by
test/sanity-check/sanity.js. - Authoritative list: variable names and defaults are documented in
test/sanity-check/utility/testSetup.js(and mirrored insanity.js); prefer that file when adding new env vars.
Required (core) — setup fails or cannot run without these:
EMAIL,PASSWORD,HOST,ORGANIZATION
Without them, login / stack creation / org-scoped setup does not complete.
Required for specific suites — related tests skip (or degrade) when missing:
| Area | Variables | Notes |
|---|---|---|
| OAuth | CLIENT_ID, APP_ID, REDIRECT_URI |
Used by oauth-test.js and auth flows that need app registration |
| 2FA (TFA) | TFA_EMAIL, TFA_PASSWORD |
user-test.js — skip TFA scenarios when unset |
| MFA (TOTP) | MFA_SECRET |
user-test.js — MFA login path; optional asserts still run without it in some cases |
| Team / stack share | MEMBER_EMAIL |
team-test.js, stack-test.js — avoids mutating the admin user; share tests skip if unset |
| DAM 2.0 | DAM_2_0_ENABLED=true |
entry-test.js — asset/DAM 2.0 block gated on this flag |
Optional / config (defaults exist; override when needed):
PERSONALIZE_HOST— defaults topersonalize-api.contentstack.comin setupDELETE_DYNAMIC_RESOURCES— defaults to deleting dynamic stack/Personalize resources; set tofalseto keep them for debugging
Regions / hosts: point HOST at the API host for the stack you are testing (e.g. regional or custom CMA host), consistent with testSetup.js.
Runtime (not for .env check-in): after setup, testSetup assigns process.env.API_KEY, AUTHTOKEN, MANAGEMENT_TOKEN, PERSONALIZE_PROJECT_UID, etc. Do not commit those; they are produced by the harness.
- Flow: setup creates stack, management token, and Personalize project where applicable; teardown reads
DELETE_DYNAMIC_RESOURCES. ReadtestSetup.jsbefore adding destructive or org-wide tests.
jest.config.js:ts-jest, coverage tocoverage/.- Use for TypeScript-first checks; keep in sync with public types if asserting SDK shape.
- No committed
only/skipon tests meant for CI. - Never commit real tokens in tests or snapshots.