Skip to content

Commit a7f50eb

Browse files
Kent C. Doddsjamestalmage
authored andcommitted
rename t.ok() to t.truthy() and t.notOk() to t.falsy()
* refactor `ok` to `truthy` and `notOk` to `falsy` * update tests to be more explicit * update docs to use a better assertion api * realign power-assert output * quick typo fix * update assertions
1 parent bcda753 commit a7f50eb

File tree

10 files changed

+45
-33
lines changed

10 files changed

+45
-33
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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ Assertions are mixed into the [execution object](#t) provided to each test callb
741741

742742
```js
743743
test(t => {
744-
t.ok('unicorn'); // assertion
744+
t.truthy('unicorn'); // assertion
745745
});
746746
```
747747

@@ -755,11 +755,11 @@ Passing assertion.
755755

756756
Failing assertion.
757757

758-
### `.ok(value, [message])`
758+
### `.truthy(value, [message])`
759759

760760
Assert that `value` is truthy.
761761

762-
### `.notOk(value, [message])`
762+
### `.falsy(value, [message])`
763763

764764
Assert that `value` is falsy.
765765

@@ -832,7 +832,7 @@ const c = 'baz';
832832
require('assert').ok(a.test(b) || b === c);
833833
```
834834

835-
If you paste that into a Node REPL it'l return:
835+
If you paste that into a Node REPL it'll return:
836836

837837
```
838838
AssertionError: false == true
@@ -845,16 +845,16 @@ test(t => {
845845
const a = /foo/;
846846
const b = 'bar';
847847
const c = 'baz';
848-
t.ok(a.test(b) || b === c);
848+
t.true(a.test(b) || b === c);
849849
});
850850
```
851851

852852
Will output:
853853

854854
```
855-
t.ok(a.test(b) || b === c)
856-
| | | |
857-
| "bar" "bar" "baz"
855+
t.true(a.test(b) || b === c)
856+
| | | |
857+
| "bar" "bar" "baz"
858858
false
859859
```
860860

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\.true\(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\.true\(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/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ test('throwing a anonymous function will report the function to the console', fu
120120
test('log failed tests', function (t) {
121121
execCli('fixture/one-pass-one-fail.js', function (err, stdout, stderr) {
122122
t.ok(err);
123-
t.match(stderr, /false == true/);
123+
t.match(stderr, /failed via t.fail\(\)/);
124124
t.end();
125125
});
126126
});

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.pass();
55
});
66

77
test('this is a failing test', t => {
8-
t.ok(false);
8+
t.fail();
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.true(a === 'bar');
77
});
88

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

12-
t.ok(a === 'foo', 'with message');
12+
t.true(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.pass();
2424
t.end();
2525
});
2626

0 commit comments

Comments
 (0)