@bahmutov/spok

中文文档
1.3.1发布于 2026/10/14MITTypeScriptCommonJS构建来源已验证近 30 天下载 6.2万

Checks a given object against a given set of specifications to keep you from writing boilerplate tests.

原始使用文档
文档目录30 节
  1. Installation
  2. Why not just deepEqual ?
  3. Adjusting Print Details
  4. API
  5. spok
  6. spok.range
  7. spok.gt
  8. spok.ge
  9. spok.lt
  10. spok.le
  11. spok.ne
  12. spok.gtz
  13. spok.gez
  14. spok.ltz
  15. spok.lez
  16. spok.type
  17. spok.array
  18. spok.arrayElements
  19. spok.arrayElementsRange
  20. spok.number
  21. spok.string
  22. spok.oneOf
  23. spok.function
  24. spok.definedObject
  25. spok.startsWith
  26. spok.endsWith
  27. spok.test
  28. spok.defined
  29. spok.notDefined
  30. License

spok

Checks a given object against a given set of specifications to keep you from writing boilerplate tests.

var test = require('tape')
var spok = require('spok')

// this would be returned from a function you are testing
var object = {
  one: 1,
  two: 2,
  three: 3,
  four: 4,
  helloWorld: 'hello world',
  anyNum: 999,
  anotherNum: 888,
  anArray: [1, 2],
  anotherArray: [1, 2, 3],
  anObject: {},
}

// custom specification
function hasThreeElements(a) {
  return a.length === 3
}

test('my object meets the specifications', function(t) {
  spok(t, object, {
    $topic: 'spok-example',
    one: spok.ge(1),
    two: 2,
    three: spok.range(2, 4),
    four: spok.lt(5),
    helloWorld: spok.startsWith('hello'),
    anyNum: spok.type('number'),
    anotherNum: spok.number,
    anArray: spok.array,
    anotherArray: hasThreeElements,
    anObject: spok.ne(undefined),
  })
  t.end()
})

shot

Installation

npm install spok

Why not just deepEqual?

deepEqual works great for most cases, but in some cases you need more control, i.e.

  • values don't exactly match, but are in a given range
  • you want to provide a predicate to determine if a value is correct or not
  • you only want to check a subset of values contained in the object

Adjusting Print Details

By default spok prints the specification that a particular assertion satisified, i.e. satisfies: spok.range(2, 4). You can turn that off via spok.printSpec = false.

On the other hand if you want more details about the satisified spec do spok.printDescription = true instead to get spok to print things like satisfies: spok.range(2, 4) 2 <= value <= 4.

Specs and descriptions are printed in gray so you can focus on the actual values of the test output.

Table of Contents generated with DocToc

spok provides a few common specification functions. However you can write your own functions as well, just return true if specification was satisfied and false if not (see example above).

If you write a specification function that would be useful to others please add it along with a test and provide a PR.

spok.* comparison function names are derived from bash comparison operators to make them easier to remember.

API

spok

Checks the given specifications against the object.

When the tests are run the actual values are printed to verify visually while each provided specification is validated and a test failure caused if one of them fails.

Parameters

  • t Object which has assertion functions equal and deepEqual (to compare objects) - use tap, tape, assert or any other library that has those and thus is compatible
  • obj Object the object to verify the specifications against
  • specifications Object the specifications to verify

spok.range

Specififies that the given number is within the given range, i.e. min<= x <=max.

var spec = {
  x: spok.range(1, 2), // specifies that x should be >=1 and <=2
}

Parameters

spok.gt

Specififies that a number is greater than the given criteria.

var spec = {
  x: spok.gt(1), // specifies that x should be >1
}

Parameters

spok.ge

Specififies that a number is greater or equal the given criteria.

var spec = {
  x: spok.ge(1), // specifies that x should be >=1
}

Parameters

spok.lt

Specififies that a number is less than the given criteria.

var spec = {
  x: spok.lt(1), // specifies that x should be < 1
}

Parameters

spok.le

Specififies that a number is less or equal the given criteria.

var spec = {
  x: spok.le(1), // specifies that x should be <=1
}

Parameters

spok.ne

Specifies that the value is not equal another.

var spec = {
  x: spok.ne(undefined), // specifies that x should be defined
}

Parameters

  • value Any criteria

spok.gtz

Specifies that the value is greater than zero

var spec = {
  x: spok.gtz,
}

spok.gez

Specifies that the value is greater or equal zero

var spec = {
  x: spok.gez,
}

spok.ltz

Specifies that the value is less than zero

var spec = {
  x: spok.ltz,
}

spok.lez

Specifies that the value is less or equal zero

var spec = {
  x: spok.lez,
}

spok.type

Specifies that the input is of a given type.

var spec = {
  x: spok.type('number'), // specifies that x should be a Number
}

Parameters

spok.array

Specifies that the input is an array.

var spec = {
  x: spok.array, // specifies that x should be an Array
}

spok.arrayElements

Specifies that the input is an array with a specific number of elements

var spec = { x: spok.arrayElements(2) // specifies that x should be an Array with 2 elements }

Parameters

spok.arrayElementsRange

Specifies that the input is an array with a number of elements in a given range

var spec = { x: spok.arrayElementsRange(2, 4) // specifies that x should be an Array with 2-4 elements }

Parameters

  • min Number min number of elements
  • max Number max number of elements

spok.number

Specifies that the input of type number and isNaN(x) returns false.

var spec = {
  x: spok.number, // specifies that x should be a Number
}

spok.string

Specifies that the input is a string.

var spec = {
  x: spok.string  // specifies that x should be a String
}

spok.oneOf

Checks if the value is present in the list

var spec = {
  role: spok.oneOf(['intern', 'employee']),
}

spok.function

Specifies that the input is a function.

var spec = {
  x: spok.function  // specifies that x should be a function
}

spok.definedObject

Specifies that the input is an object and it is not null.

var spec = {
  x: spok.definedObject, // specifies that x is a non-null object
}

spok.startsWith

Specifies that the string starts with the specified substring.

NOTE: only available with node.js which has an ES6 startsWith function

var spec = {
  x: spok.startsWith('hello'), // specifies that x should start with 'hello'
}

Parameters

  • what String substring the given string should start with

spok.endsWith

Specifies that the string ends with the specified substring.

NOTE: only available with node.js which has an ES6 endsWith function

var spec = {
  x: spok.endsWith('hello'), // specifies that x should start with 'hello'
}

Parameters

  • what String substring the given string should start with

spok.test

Specifies that the string needs to match the given regular expression.

var spec = {
  x: spok.test(/hello$/), // specifies that x should match /hello$/
}

Parameters

  • regex RegExp regular expression against which the string is checked via test

spok.defined

Specifies that a value is defined, i.e. it is neither null nor undefined.

var spec = {
  x: spok.defined,
}

spok.notDefined

Specifies that a value is notDefined, i.e. it is either null or notDefined.

var spec = {
  x: spok.notDefined,
}

License

MIT

版本与发布记录

@bahmutov/spok 共发布 13 个版本,首次发布于 2020/04/21;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.3.1latest2026/10/1424 天前
1.3.02026/10/1424 天前
1.2.102025/08/291.2 年前
1.2.92025/08/291.2 年前
1.2.82025/08/291.2 年前
1.2.72023/07/263 年前

关于 @bahmutov/spok 的常见问题

@bahmutov/spok 是什么?

Checks a given object against a given set of specifications to keep you from writing boilerplate tests.

如何安装 @bahmutov/spok?

在项目目录运行 npm install @bahmutov/spok,或使用 pnpm add / yarn add / bun add。

@bahmutov/spok 支持 TypeScript 吗?

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

@bahmutov/spok 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bahmutov/spok 可以商用吗?

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

@bahmutov/spok 的下载量有多少?

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

@bahmutov/spok 还在维护吗?

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