Skip to content

Commit f2f46e6

Browse files
Merge pull request #385 from swiftwasm/pr-27e488a720e90fb55c7473457a955ceb0c2f46fa
Add public `init(message:)` to `JSException`
2 parents 3674e57 + d794213 commit f2f46e6

File tree

4 files changed

+72
-69
lines changed

4 files changed

+72
-69
lines changed

Sources/JavaScriptKit/JSException.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,12 @@ public struct JSException: Error, Equatable, CustomStringConvertible {
4646
self.stack = nil
4747
}
4848
}
49+
50+
/// Initializes a new JavaScript `Error` instance with a message and prepare it to be thrown.
51+
///
52+
/// - Parameters:
53+
/// - message: The message to throw.
54+
public init(message: String) {
55+
self.init(JSError(message: message).jsValue)
56+
}
4957
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import XCTest
2+
import JavaScriptKit
3+
4+
class JSExceptionTests: XCTestCase {
5+
private func eval(_ code: String) -> JSValue {
6+
return JSObject.global.eval!(code)
7+
}
8+
9+
func testThrowingMethodCalls() {
10+
let context = eval(
11+
"""
12+
(() => ({
13+
func1: () => { throw new Error(); },
14+
func2: () => { throw 'String Error'; },
15+
func3: () => { throw 3.0; },
16+
}))()
17+
"""
18+
).object!
19+
20+
// MARK: Throwing method calls
21+
XCTAssertThrowsError(try context.throwing.func1!()) { error in
22+
XCTAssertTrue(error is JSException)
23+
let errorObject = JSError(from: (error as! JSException).thrownValue)
24+
XCTAssertNotNil(errorObject)
25+
}
26+
27+
XCTAssertThrowsError(try context.throwing.func2!()) { error in
28+
XCTAssertTrue(error is JSException)
29+
let thrownValue = (error as! JSException).thrownValue
30+
XCTAssertEqual(thrownValue.string, "String Error")
31+
}
32+
33+
XCTAssertThrowsError(try context.throwing.func3!()) { error in
34+
XCTAssertTrue(error is JSException)
35+
let thrownValue = (error as! JSException).thrownValue
36+
XCTAssertEqual(thrownValue.number, 3.0)
37+
}
38+
}
39+
40+
func testThrowingUnboundFunctionCalls() {
41+
let jsThrowError = eval("() => { throw new Error(); }")
42+
XCTAssertThrowsError(try jsThrowError.function!.throws()) { error in
43+
XCTAssertTrue(error is JSException)
44+
let errorObject = JSError(from: (error as! JSException).thrownValue)
45+
XCTAssertNotNil(errorObject)
46+
}
47+
}
48+
49+
func testThrowingConstructorCalls() {
50+
let Animal = JSObject.global.Animal.function!
51+
XCTAssertNoThrow(try Animal.throws.new("Tama", 3, true))
52+
XCTAssertThrowsError(try Animal.throws.new("Tama", -3, true)) { error in
53+
XCTAssertTrue(error is JSException)
54+
let errorObject = JSError(from: (error as! JSException).thrownValue)
55+
XCTAssertNotNil(errorObject)
56+
}
57+
}
58+
59+
func testInitWithMessage() {
60+
let message = "THIS IS AN ERROR MESSAGE"
61+
let exception = JSException(message: message)
62+
XCTAssertTrue(exception.description.contains(message))
63+
}
64+
}

Tests/JavaScriptKitTests/JavaScriptKitTests.swift

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -423,64 +423,6 @@ class JavaScriptKitTests: XCTestCase {
423423
globalObject1.prop_1.nested_prop = originalProp1
424424
}
425425

426-
func testException() {
427-
// ```js
428-
// global.globalObject1 = {
429-
// ...
430-
// prop_9: {
431-
// func1: function () {
432-
// throw new Error();
433-
// },
434-
// func2: function () {
435-
// throw "String Error";
436-
// },
437-
// func3: function () {
438-
// throw 3.0
439-
// },
440-
// },
441-
// ...
442-
// }
443-
// ```
444-
//
445-
let globalObject1 = JSObject.global.globalObject1
446-
let prop_9: JSValue = globalObject1.prop_9
447-
448-
// MARK: Throwing method calls
449-
XCTAssertThrowsError(try prop_9.object!.throwing.func1!()) { error in
450-
XCTAssertTrue(error is JSException)
451-
let errorObject = JSError(from: (error as! JSException).thrownValue)
452-
XCTAssertNotNil(errorObject)
453-
}
454-
455-
XCTAssertThrowsError(try prop_9.object!.throwing.func2!()) { error in
456-
XCTAssertTrue(error is JSException)
457-
let thrownValue = (error as! JSException).thrownValue
458-
XCTAssertEqual(thrownValue.string, "String Error")
459-
}
460-
461-
XCTAssertThrowsError(try prop_9.object!.throwing.func3!()) { error in
462-
XCTAssertTrue(error is JSException)
463-
let thrownValue = (error as! JSException).thrownValue
464-
XCTAssertEqual(thrownValue.number, 3.0)
465-
}
466-
467-
// MARK: Simple function calls
468-
XCTAssertThrowsError(try prop_9.func1.function!.throws()) { error in
469-
XCTAssertTrue(error is JSException)
470-
let errorObject = JSError(from: (error as! JSException).thrownValue)
471-
XCTAssertNotNil(errorObject)
472-
}
473-
474-
// MARK: Throwing constructor call
475-
let Animal = JSObject.global.Animal.function!
476-
XCTAssertNoThrow(try Animal.throws.new("Tama", 3, true))
477-
XCTAssertThrowsError(try Animal.throws.new("Tama", -3, true)) { error in
478-
XCTAssertTrue(error is JSException)
479-
let errorObject = JSError(from: (error as! JSException).thrownValue)
480-
XCTAssertNotNil(errorObject)
481-
}
482-
}
483-
484426
func testSymbols() {
485427
let symbol1 = JSSymbol("abc")
486428
let symbol2 = JSSymbol("abc")

Tests/prelude.mjs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,6 @@ function setupTestGlobals(global) {
159159
},
160160
prop_7: 3.14,
161161
prop_8: [0, , 2, 3, , , 6],
162-
prop_9: {
163-
func1: function () {
164-
throw new Error();
165-
},
166-
func2: function () {
167-
throw "String Error";
168-
},
169-
func3: function () {
170-
throw 3.0;
171-
},
172-
},
173162
eval_closure: function (fn) {
174163
return fn(arguments[1]);
175164
},

0 commit comments

Comments
 (0)