Skip to content

Commit b05564c

Browse files
committed
added TAP style assertions and TAP reporter, updated README and example tests
1 parent 616cfcd commit b05564c

File tree

3 files changed

+256
-64
lines changed

3 files changed

+256
-64
lines changed

README.md

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
## Features
1212

13-
- fast, lightweight, minimal code (3.5kb minified and gzipped)
13+
- fast, lightweight, minimal code (~4kb minified and gzipped)
1414
- very little setup, a fairly complete solution
1515
- includes assertions, test harness, test runner, test reporter
1616
- flexible syntax for writing your tests
@@ -144,6 +144,7 @@ Where `foo` can be any of these methods:
144144
- `assert.strictEquals()` - uses the `===` comparison
145145
- `assert.deepEquals()` - uses the `react-fast-compare` deep equals comparison
146146
- `assert.isType()` - uses a `typeof` comparison
147+
- `assert.throwsError()` - checks if something throws an Error
147148
- `assert.isImmutable()` - checks if something is a Boolean, Number, String, Null
148149
- `assert.isMutable()` - checks if something is an Object, Array, Function, Class, Map, or Set
149150
- `assert.isReactElement()` - checks object contains `$$typeof = REACT_ELEMENT_TYPE`
@@ -166,6 +167,16 @@ test("test using assert, description here", () => {
166167
var obj1 = { name: "dan", age: 22, stats: { s: 10, b: 20, c: 31 } }
167168
var obj2 = { name: "bob", age: 21, stats: { s: 10, b: 20, c: 30 } }
168169
assert.deepEquals("obj1 should equal obj2", obj1, obj2)
170+
171+
assert.truthy("string '' is truthy", "")
172+
assert.truthy("string 'foo' is truthy", "foo")
173+
assert.falsey("0 is falsey", 0)
174+
assert.falsey("1 is falsey", 1)
175+
assert.falsey("null is falsey", null, true)
176+
assert.isMutable("Object is mutable", 10)
177+
assert.isImmutable("Number is immutable", { foo: 99 })
178+
assert.throwsError("Throws an error", "" + new Error())
179+
assert.throwsError("Throws an error", new Error())
169180
})
170181

171182
// run the tests
@@ -212,6 +223,40 @@ test("test using assert, object syntax", () => {
212223
run()
213224
```
214225

226+
### Using `t`
227+
228+
Usage:
229+
230+
```js
231+
t.foo(actual [, expected])
232+
```
233+
234+
Where `foo` can be any of these methods:
235+
236+
- `t.ok()` - checks if something is truthy
237+
- `t.notOk()` - checks if something is falsey
238+
- `t.equal()` - uses the `==` comparison
239+
- `t.strictEqual()` - uses the `===` comparison
240+
- `t.deepEqual()` - uses the `react-fast-compare` deep equals comparison
241+
- `t.throws()` - checks if something throws an error
242+
243+
(Note the difference to `tap` and `tape` - there's no `msg` as the third parameter)
244+
245+
246+
```js
247+
/* using TAP style assertions */
248+
249+
test("test using t", () => {
250+
t.equal(1, 1)
251+
t.strictEqual(1, "1")
252+
t.deepEqual([1, 2, 3], [1, 2, 3])
253+
t.deepEqual([1, 2, 3], [1, 3, 4])
254+
t.deepEqual({ one: 1, foo: "baz" }, { one: 1, foo: "bar" })
255+
t.throws(new Error())
256+
t.throws("I'm just a String")
257+
})
258+
```
259+
215260
## Running your tests
216261

217262
Now you're ready to run your tests:
@@ -316,6 +361,18 @@ The test results output will be indented appropriately, like so:
316361
<img src="https://i.imgur.com/bfHl4tO.png" alt="grouped and indented test results" />
317362
</p>
318363

364+
## TAP output
365+
366+
If you need your test results printed in [TAP](https://testanything.org/tap-version-13-specification.html) format, then use `--format=tap` or `tea.reportFormat = 'tap'`.
367+
368+
The TAP format is machine-readable, and you can pipe the results to other programs, to process or prettify it.
369+
370+
Here are some TAP prettifiers that work OK with the TAP output of `tea`:
371+
372+
- [tap-difflet])(https://github.com/namuol/tap-difflet) - prettifier for TAP formatted test results, works with `tea`
373+
- [tap-diff])(https://github.com/axross/tap-diff) - prettifier for TAP formatted test results, works with `tea`
374+
- [tap-nyan])(https://github.com/calvinmetcalf/tap-nyan) - prettifier for TAP formatted test results, works with `tea`
375+
319376
## Integration tests
320377

321378
### Running in a real browser

examples/simple-tests.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,16 @@ test("test using expect, description here", () => {
3939
//
4040
// tests using the 'assert' assertion method
4141
test("test using ASSERT, description here", () => {
42-
assert.isType("isType: demo.app is should be a function", demoApp.add, "function")
43-
assert.strictEquals("strictEquals: 1 + 1 should equal 2", demoApp.add(1, 1), 2)
42+
assert.isType(
43+
"isType: demo.app is should be a function",
44+
demoApp.add,
45+
"function"
46+
)
47+
assert.strictEquals(
48+
"strictEquals: 1 + 1 should equal 2",
49+
demoApp.add(1, 1),
50+
2
51+
)
4452
assert.deepEquals(
4553
"deepEquals: obj1 should equal simple obj1",
4654
{ foo: "bar" },
@@ -50,15 +58,16 @@ test("test using ASSERT, description here", () => {
5058
var obj2 = { name: "bob", age: 21, stats: { s: 10, b: 20, c: 30 } }
5159
assert.deepEquals("deepEquals: obj1 should equal obj2", obj1, obj2)
5260

53-
assert.truthy("string '' is truthy", '')
61+
assert.truthy("string '' is truthy", "")
5462
assert.truthy("string 'foo' is truthy", "foo")
5563
assert.falsey("0 is falsey", 0)
5664
assert.falsey("1 is falsey", 1)
5765
assert.falsey("null is falsey", null, true)
5866
assert.isMutable("Object is mutable", 10)
59-
assert.isImmutable("Number is immutable", {foo: 99})
67+
assert.isImmutable("Number is immutable", { foo: 99 })
68+
assert.throwsError("Throws an error", "" + new Error())
69+
assert.throwsError("Throws an error", new Error())
6070
})
61-
6271
//
6372
// tests using 'assert' object syntax assertion method
6473
test("test using assert, object syntax", () => {
@@ -83,4 +92,15 @@ test("test using assert, object syntax", () => {
8392
})
8493
})
8594

95+
// using TAP style assertions (but no `msg` as 3rd param to t() ..)
96+
test("test using t", () => {
97+
t.equal(1, 1)
98+
t.strictEqual(1, "1")
99+
t.deepEqual([1, 2, 3], [1, 2, 3])
100+
t.deepEqual([1, 2, 3], [1, 3, 4])
101+
t.deepEqual({ one: 1, foo: "baz" }, { one: 1, foo: "bar" })
102+
t.throws(new Error())
103+
t.throws("I'm just a String")
104+
})
105+
86106
run()

0 commit comments

Comments
 (0)