npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@chzky/cest

v0.5.6

Published

`@chzky/cest` 旨在提供一个简洁直观的 API,用于编写和组织你的测试。它支持异步测试、跳过测试、以及按顺序执行的步骤测试,并提供了一套断言工具来帮助你验证代码行为。

Readme

@chzky/cest : 一个简易的测试框架

@chzky/cest 旨在提供一个简洁直观的 API,用于编写和组织你的测试。它支持异步测试、跳过测试、以及按顺序执行的步骤测试,并提供了一套断言工具来帮助你验证代码行为。

特性

  • 简洁的 API: 易于上手,语法直观。
  • 异步测试支持: 原生支持 async/await
  • 步骤测试: 允许你将测试拆分成多个有序的步骤,并支持前一个步骤的结果传递给下一个步骤。
  • 断言库: 提供了一套常用的断言函数,包括相等性检查、错误抛出检查、匹配检查等。
  • 可跳过测试: 轻松地跳过不需要执行的测试。
  • 控制台输出: 清晰的测试结果输出。

安装

npm install @chzky/cest
# 或者
yarn add @chzky/cest
# 或者
pnpm add @chzky/cest

使用

基本测试

使用 Cest 函数来定义你的测试。

import { Cest, assert } from '@chzky/cest';

Cest('我的第一个测试', () => {
  assert(1 + 1 === 2, '1加1应该等于2');
  assert.equal({ a: 1 }, { a: 1 }, '对象应该相等');
});

Cest('异步操作测试', async () => {
  const result = await Promise.resolve(42);
  assert.equal(result, 42, '异步结果应该为42');
});

跳过测试

你可以使用 skip 属性来跳过一个测试。

import { Cest } from '@chzky/cest';

Cest({ name: '这个测试会被跳过', skip: true }, () => {
  // 这段代码不会执行
  console.log('我不会被打印出来');
});

步骤测试 (Cester.step)

对于需要按顺序执行的测试步骤,你可以使用 Cesterstep 方法。每个 step 都可以选择接收前一个 step 的返回值。

import { Cest, assert } from '@chzky/cest';

Cest('计算器测试').step('初始化为0', () => {
  return 0;
}).step('加5', (prevResult) => {
  assert.equal(prevResult, 0, '初始值应为0');
  return prevResult + 5;
}).step('乘以2', (prevResult) => {
  assert.equal(prevResult, 5, '加5后应为5');
  return prevResult * 2;
}).step('最终结果为10', (prevResult) => {
  assert.equal(prevResult, 10, '最终结果应为10');
}).run(); // 运行步骤测试

你也可以直接使用 Cest(name) 返回的 Cester 实例来定义单一测试:

import { Cest, assert } from '@chzky/cest';

const myTests = Cest('我的模块测试');

myTests.test(() => {
  assert(true, '这是一个通过的测试');
});

myTests.skip(() => {
  console.log('这个测试被跳过了');
});

断言 (assert)

@chzky/cest 提供了一个 assert 对象,包含多种断言方法:

  • assert(condition, message): 最基本的断言,当 conditionfalse 时抛出错误。
  • assert.every(...conditions): 断言所有条件都为 true
  • assert.some(...conditions): 断言至少一个条件为 true
  • assert.reverse(condition, message): 反向断言,当 conditiontrue 时抛出错误。
  • assert.throw(fn, instanceOf): 断言 fn 函数会抛出错误。如果提供了 instanceOf,则断言抛出的错误是指定类型的实例。
  • assert.sequence(message): 返回一个函数,用于断言一系列操作按预期顺序执行。
    const checkOrder = assert.sequence('顺序错误');
    checkOrder(1); // 第一次调用期望1
    checkOrder(2); // 第二次调用期望2
  • assert.equal(actual, expected): 断言 actualexpected 深度相等。支持基本类型、对象、数组、Map、Set、Date、RegExp、URL等。
  • assert.unreachable(message): 断言此行代码永远不应被执行到。
  • assert.when(condition, fn): 如果 conditiontrue,则执行 fn
  • assert.match(value, pattern): 断言 value 匹配 patternpattern 可以是字符串(value 包含 pattern)或正则表达式(value 匹配 pattern)。

全栈堆栈跟踪 (fullstack)

默认情况下,当测试失败时,@chzky/cest 只会显示导致失败的堆栈行。如果你需要完整的堆栈跟踪,可以设置 fullstack: true

import { Cest, assert } from '@chzky/cest';

Cest({ name: '需要完整堆栈的错误测试', fullstack: true }, () => {
  throw new Error('这是一个自定义错误'); // 会显示完整的堆栈
});

启用/禁用测试运行

@chzky/cest 提供了一个全局开关,可以临时禁用所有测试的运行。这在某些生产环境或特定场景下可能很有用。

import { Cest, enable, close } from '@chzky/cest';

// 默认情况下,测试是启用的

Cest('这个测试会运行', () => {
  console.log('我运行了');
});

close(); // 禁用所有测试

Cest('这个测试不会运行', () => {
  console.log('我不会运行');
});

enable(); // 重新启用所有测试

Cest('这个测试会再次运行', () => {
  console.log('我再次运行了');
});