Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Commit d0a46ce

Browse files
author
TATSUNO Yasuhiro
authored
Merge pull request #48 from exoego/assert
Overhaul assert module
2 parents 85c9338 + e646aa0 commit d0a46ce

File tree

3 files changed

+49
-39
lines changed

3 files changed

+49
-39
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The following core Node.js modules (v8.7.0+) have been implemented:
2020

2121
| Node Module | v10 & v12 support |
2222
| ------------------------------------------------------------ | ------------------ |
23-
| [assert](https://nodejs.org/api/assert.html) | |
23+
| [assert](https://nodejs.org/api/assert.html) | :heavy_check_mark: |
2424
| [buffer](https://nodejs.org/api/buffer.html) | :heavy_check_mark: |
2525
| [child_process](https://nodejs.org/api/child_process.html) | :heavy_check_mark: |
2626
| [cluster](https://nodejs.org/api/cluster.html) | |
Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package io.scalajs.nodejs
22

3+
import com.thoughtworks.enableIf
34
import io.scalajs.nodejs.events.IEventEmitter
45

56
import scala.scalajs.js
67
import scala.scalajs.js.annotation.JSImport
8+
import scala.scalajs.js.|
79

810
/**
911
* The assert module provides a simple set of assertion tests that can be used to test invariants. The module is
@@ -23,13 +25,7 @@ trait Assert extends IEventEmitter {
2325
*/
2426
def apply(expression: js.Any, message: String = js.native): Unit = js.native
2527

26-
/**
27-
* Tests for deep equality between the actual and expected parameters. Primitive values are compared with the equal
28-
* comparison operator ( == ). Only enumerable "own" properties are considered. The deepEqual() implementation does
29-
* not test object prototypes, attached symbols, or non-enumerable properties. This can lead to some potentially
30-
* surprising results.
31-
* @example assert.deepEqual(actual, expected[, message])
32-
*/
28+
@deprecated("Use assert.deepStrictEqual() instead.", "stability 0")
3329
def deepEqual(actual: js.Any, expected: js.Any, message: String = js.native): Unit = js.native
3430

3531
/**
@@ -39,14 +35,10 @@ trait Assert extends IEventEmitter {
3935
*/
4036
def deepStrictEqual(actual: js.Any, expected: js.Any, message: String): Unit = js.native
4137

42-
/**
43-
* Asserts that the function block does not throw an error. See assert.throws() for more details.
44-
* When assert.doesNotThrow() is called, it will immediately call the block function. If an error is thrown
45-
* and it is the same type as that specified by the error parameter, then an AssertionError is thrown. If the
46-
* error is of a different type, or if the error parameter is undefined, the error is propagated back to the caller.
47-
* @example assert.doesNotThrow(block[, error][, message])
48-
*/
49-
def doesNotThrow(block: js.Function, error: js.Any, message: String): Unit = js.native
38+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
39+
def doesNotReject(asyncFn: js.Function | js.Promise[_],
40+
error: js.RegExp | js.Function = js.native,
41+
message: String = js.native): Unit = js.native
5042

5143
/**
5244
* Asserts that the function block does not throw an error. See assert.throws() for more details.
@@ -55,28 +47,26 @@ trait Assert extends IEventEmitter {
5547
* error is of a different type, or if the error parameter is undefined, the error is propagated back to the caller.
5648
* @example assert.doesNotThrow(block[, error][, message])
5749
*/
58-
def doesNotThrow(block: js.Function, message: String): Unit = js.native
50+
def doesNotThrow(block: js.Function, error: js.RegExp | js.Function = js.native, message: String = js.native): Unit =
51+
js.native
5952

6053
/**
61-
* Asserts that the function block does not throw an error. See assert.throws() for more details.
62-
* When assert.doesNotThrow() is called, it will immediately call the block function. If an error is thrown
63-
* and it is the same type as that specified by the error parameter, then an AssertionError is thrown. If the
64-
* error is of a different type, or if the error parameter is undefined, the error is propagated back to the caller.
65-
* @example assert.doesNotThrow(block[, error][, message])
54+
* @see https://nodejs.org/api/assert.html#assert_assert_equal_actual_expected_message
6655
*/
67-
def doesNotThrow(block: js.Function, error: js.Any = js.native): Unit = js.native
56+
@deprecated("Use assert.strictEqual() instead.", "stability 0")
57+
def equal(actual: js.Any, expected: js.Any, message: String = js.native): Unit = js.native
6858

6959
/**
70-
* Tests shallow, coercive equality between the actual and expected parameters using the equal comparison operator ( == ).
71-
* @example assert.equal(actual, expected[, message])
60+
* @see https://nodejs.org/api/assert.html#assert_assert_fail_message
7261
*/
73-
def equal(actual: js.Any, expected: js.Any, message: String = js.native): Unit = js.native
62+
def fail(message: String): Unit = js.native
7463

7564
/**
76-
* Throws an AssertionError. If message is falsy, the error message is set as the values of actual and expected
77-
* separated by the provided operator. Otherwise, the error message is the value of message.
78-
* @example assert.fail(actual, expected, message, operator)
65+
* @see https://nodejs.org/api/assert.html#assert_assert_fail_message
7966
*/
67+
def fail(message: js.Error): Unit = js.native
68+
69+
@deprecated("Use assert.fail([message]) or other assert functions instead.", "Node.js v10.0.0")
8070
def fail(actual: js.Any, expected: js.Any, message: String, operator: String): Unit = js.native
8171

8272
/**
@@ -85,10 +75,7 @@ trait Assert extends IEventEmitter {
8575
*/
8676
def ifError(value: js.Any): Unit = js.native
8777

88-
/**
89-
* Tests for any deep inequality. Opposite of assert.deepEqual().
90-
* @example assert.notDeepEqual(actual, expected[, message])
91-
*/
78+
@deprecated("Use assert.notDeepStrictEqual() instead.", "stability 0")
9279
def notDeepEqual(actual: js.Any, expected: js.Any, message: String = js.native): Unit = js.native
9380

9481
/**
@@ -97,10 +84,7 @@ trait Assert extends IEventEmitter {
9784
*/
9885
def notDeepStrictEqual(actual: js.Any, expected: js.Any, message: String = js.native): Unit = js.native
9986

100-
/**
101-
* Tests shallow, coercive inequality with the not equal comparison operator ( != ).
102-
* @example assert.notEqual(actual, expected[, message])
103-
*/
87+
@deprecated("Use assert.notStrictEqual() instead.", "stability 0")
10488
def notEqual(actual: js.Any, expected: js.Any, message: String = js.native): Unit = js.native
10589

10690
/**
@@ -127,13 +111,22 @@ trait Assert extends IEventEmitter {
127111
* of the message parameter. If the message parameter is undefined, a default error message is assigned.
128112
* @example assert.throws(block[, error][, message])
129113
*/
130-
def throws(block: js.Function, error: js.Any, message: String = js.native): Unit = js.native
114+
def throws(block: js.Function,
115+
error: js.RegExp | js.Function | js.Object | Error,
116+
message: String = js.native): Unit = js.native
131117

118+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
119+
def rejects(asyncFn: js.Function | js.Promise[_],
120+
error: js.RegExp | js.Function | js.Object | Error = js.native,
121+
message: String = js.native): Unit = js.native
132122
}
133123

134124
/**
135125
* Assert Singleton
136126
*/
137127
@js.native
138128
@JSImport("assert", JSImport.Namespace)
139-
object Assert extends Assert
129+
object Assert extends Assert {
130+
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
131+
val strict: Assert = js.native
132+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package nodejs.assertion
2+
3+
import org.scalatest.FunSpec
4+
import io.scalajs.nodejs.{ Assert => NodeAssert }
5+
6+
import scala.scalajs.js
7+
8+
class AssertTest extends FunSpec {
9+
it("have strict from v9.9.0") {
10+
assert(NodeAssert.strict !== js.undefined)
11+
}
12+
13+
it("have rejects/doesNotReject from v10.0.0") {
14+
assert(NodeAssert.strict.rejects _ !== js.undefined)
15+
assert(NodeAssert.strict.doesNotReject _ !== js.undefined)
16+
}
17+
}

0 commit comments

Comments
 (0)