Skip to content

Commit fed5812

Browse files
author
Kent C. Dodds
committed
refactor ok to truthy and notOk to falsy
1 parent c4ec8e7 commit fed5812

9 files changed

+41
-29
lines changed

index.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,18 @@ export interface AssertContext {
9696
/**
9797
* Assert that value is truthy.
9898
*/
99-
ok(value: any, message?: string): void;
99+
truthy(value: any, message?: string): void;
100100
/**
101101
* Assert that value is falsy.
102102
*/
103+
falsy(value: any, message?: string): void;
104+
/**
105+
* DEPRECATED, use `truthy`. Assert that value is truthy.
106+
*/
107+
ok(value: any, message?: string): void;
108+
/**
109+
* DEPRECATED, use `falsy`. Assert that value is falsy.
110+
*/
103111
notOk(value: any, message?: string): void;
104112
/**
105113
* Assert that value is true.

lib/assert.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ x.fail = function (msg) {
3636
test(false, create(false, false, 'fail', msg, x.fail));
3737
};
3838

39-
x.ok = function (val, msg) {
40-
test(val, create(val, true, '==', msg, x.ok));
39+
x.truthy = function (val, msg) {
40+
test(val, create(val, true, '==', msg, x.truthy));
4141
};
4242

43-
x.notOk = function (val, msg) {
44-
test(!val, create(val, false, '==', msg, x.notOk));
43+
x.falsy = function (val, msg) {
44+
test(!val, create(val, false, '==', msg, x.falsy));
4545
};
4646

4747
x.true = function (val, msg) {
@@ -142,6 +142,8 @@ x.ifError = x.error = function (err, msg) {
142142
* deprecated APIs
143143
*/
144144
x.doesNotThrow = util.deprecate(x.notThrows, getDeprecationNotice('doesNotThrow()', 'notThrows()'));
145+
x.ok = util.deprecate(x.truthy, getDeprecationNotice('ok()', 'truthy()'));
146+
x.notOk = util.deprecate(x.falsy, getDeprecationNotice('notOk()', 'falsy()'));
145147
x.same = util.deprecate(x.deepEqual, getDeprecationNotice('same()', 'deepEqual()'));
146148
x.notSame = util.deprecate(x.notDeepEqual, getDeprecationNotice('notSame()', 'notDeepEqual()'));
147149

lib/enhance-assert.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ module.exports = enhanceAssert;
22
module.exports.formatter = formatter;
33

44
module.exports.PATTERNS = [
5-
't.ok(value, [message])',
6-
't.notOk(value, [message])',
5+
't.truthy(value, [message])',
6+
't.falsy(value, [message])',
77
't.true(value, [message])',
88
't.false(value, [message])',
99
't.is(value, expected, [message])',
@@ -12,6 +12,8 @@ module.exports.PATTERNS = [
1212
't.notDeepEqual(value, expected, [message])',
1313
't.regex(contents, regex, [message])',
1414
// deprecated apis
15+
't.ok(value, [message])',
16+
't.notOk(value, [message])',
1517
't.same(value, expected, [message])',
1618
't.notSame(value, expected, [message])'
1719
];

readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ Assertions are mixed into the [execution object](#t) provided to each test callb
739739

740740
```js
741741
test(t => {
742-
t.ok('unicorn'); // assertion
742+
t.truthy('unicorn'); // assertion
743743
});
744744
```
745745

@@ -753,11 +753,11 @@ Passing assertion.
753753

754754
Failing assertion.
755755

756-
### `.ok(value, [message])`
756+
### `.truthy(value, [message])`
757757

758758
Assert that `value` is truthy.
759759

760-
### `.notOk(value, [message])`
760+
### `.falsy(value, [message])`
761761

762762
Assert that `value` is falsy.
763763

@@ -843,14 +843,14 @@ test(t => {
843843
const a = /foo/;
844844
const b = 'bar';
845845
const c = 'baz';
846-
t.ok(a.test(b) || b === c);
846+
t.truthy(a.test(b) || b === c);
847847
});
848848
```
849849

850850
Will output:
851851

852852
```
853-
t.ok(a.test(b) || b === c)
853+
t.truthy(a.test(b) || b === c)
854854
| | | |
855855
| "bar" "bar" "baz"
856856
false

test/api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,12 @@ test('power-assert support', function (t) {
553553

554554
t.match(
555555
api.errors[0].error.message,
556-
/t\.ok\(a === 'bar'\)\s*\n\s+\|\s*\n\s+"foo"/m
556+
/t\.truthy\(a === 'bar'\)\s*\n\s+\|\s*\n\s+"foo"/m
557557
);
558558

559559
t.match(
560560
api.errors[1].error.message,
561-
/with message\s+t\.ok\(a === 'foo', 'with message'\)\s*\n\s+\|\s*\n\s+"bar"/m
561+
/with message\s+t\.truthy\(a === 'foo', 'with message'\)\s*\n\s+\|\s*\n\s+"bar"/m
562562
);
563563
});
564564
});

test/assert.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,29 @@ test('.fail()', function (t) {
1919
t.end();
2020
});
2121

22-
test('.ok()', function (t) {
22+
test('.truthy()', function (t) {
2323
t.throws(function () {
24-
assert.ok(0);
25-
assert.ok(false);
24+
assert.truthy(0);
25+
assert.truthy(false);
2626
});
2727

2828
t.doesNotThrow(function () {
29-
assert.ok(1);
30-
assert.ok(true);
29+
assert.truthy(1);
30+
assert.truthy(true);
3131
});
3232

3333
t.end();
3434
});
3535

36-
test('.notOk()', function (t) {
36+
test('.falsy()', function (t) {
3737
t.throws(function () {
38-
assert.notOk(1);
39-
assert.notOk(true);
38+
assert.falsy(1);
39+
assert.falsy(true);
4040
});
4141

4242
t.doesNotThrow(function () {
43-
assert.notOk(0);
44-
assert.notOk(false);
43+
assert.falsy(0);
44+
assert.falsy(false);
4545
});
4646

4747
t.end();

test/fixture/one-pass-one-fail.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import test from '../../';
22

33
test('this is a passing test', t => {
4-
t.ok(true);
4+
t.truthy(true);
55
});
66

77
test('this is a failing test', t => {
8-
t.ok(false);
8+
t.truthy(false);
99
});

test/fixture/power-assert.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import test from '../../';
33
test.serial(t => {
44
const a = 'foo';
55

6-
t.ok(a === 'bar');
6+
t.truthy(a === 'bar');
77
});
88

99
test.serial(t => {
1010
const a = 'bar';
1111

12-
t.ok(a === 'foo', 'with message');
12+
t.truthy(a === 'foo', 'with message');
1313
});

test/fixture/slow-exit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test.cb('long running', t => {
2020
}, {alwaysLast: true});
2121

2222
setTimeout(() => {
23-
t.ok(true);
23+
t.truthy(true);
2424
t.end();
2525
});
2626

0 commit comments

Comments
 (0)