diff --git a/crates/typescript-tests/Cargo.toml b/crates/typescript-tests/Cargo.toml index 96030d8e26c..92465c4b14e 100644 --- a/crates/typescript-tests/Cargo.toml +++ b/crates/typescript-tests/Cargo.toml @@ -10,6 +10,8 @@ wasm-bindgen = { path = '../..' } wasm-bindgen-futures = { path = '../futures' } web-sys = { path = '../web-sys', features = [ 'HtmlElement', 'Node', 'Document' ] } js-sys = { path = '../js-sys' } +serde = { version = "1.0", features = ["derive"] } +serde-wasm-bindgen = "0.6" [lib] crate-type = ['cdylib'] diff --git a/crates/typescript-tests/jest.config.cjs b/crates/typescript-tests/jest.config.cjs new file mode 100644 index 00000000000..c63e1456977 --- /dev/null +++ b/crates/typescript-tests/jest.config.cjs @@ -0,0 +1,17 @@ +/** @type {import('ts-jest').JestConfigWithTsJest} */ +module.exports = { + preset: "ts-jest/presets/default-esm", + testEnvironment: 'node', + extensionsToTreatAsEsm: [".ts"], + verbose: true, + // TODO: match all test files + testMatch: ['**/src/simple_struct.ts', '**/src/typescript_type.ts'], + injectGlobals: false, + globals: { + 'ts-jest': + { + useESM: true, + isolatedModules: true + } + } +}; diff --git a/crates/typescript-tests/no_modules.tsconfig.json b/crates/typescript-tests/no_modules.tsconfig.json index e3688b7d156..9e2cc53e7ec 100644 --- a/crates/typescript-tests/no_modules.tsconfig.json +++ b/crates/typescript-tests/no_modules.tsconfig.json @@ -4,6 +4,7 @@ "noImplicitAny": true, "sourceMap": true, "outDir": "dist_no_modules", + "moduleResolution":"node" }, "include": [ "pkg/no_modules/*.d.ts", diff --git a/crates/typescript-tests/package.json b/crates/typescript-tests/package.json index a1fb1297991..017eca094a6 100644 --- a/crates/typescript-tests/package.json +++ b/crates/typescript-tests/package.json @@ -1,8 +1,12 @@ { "scripts": { - "tsc": "tsc" + "tsc": "tsc", + "test": "NODE_OPTIONS=--experimental-vm-modules jest --config ./jest.config.cjs" }, "devDependencies": { - "typescript": "^3.3.3333" - } + "@types/jest": "^29.5.8", + "ts-jest": "^29.1.1", + "typescript": "^5.2.2" + }, + "type": "module" } diff --git a/crates/typescript-tests/run.sh b/crates/typescript-tests/run.sh index f971f251bf2..5a3605e8300 100755 --- a/crates/typescript-tests/run.sh +++ b/crates/typescript-tests/run.sh @@ -57,3 +57,5 @@ cd .. # Then try to build the typescript in the src_no_modules folder against the pkg/no_modules build. npm run tsc -- -p no_modules.tsconfig.json + +npm test diff --git a/crates/typescript-tests/src/simple_struct.ts b/crates/typescript-tests/src/simple_struct.ts index 4927cd1a4a4..d7a4c9deb82 100644 --- a/crates/typescript-tests/src/simple_struct.ts +++ b/crates/typescript-tests/src/simple_struct.ts @@ -1,9 +1,20 @@ -import * as wbg from '../pkg/typescript_tests'; +import * as wbg from "../pkg/typescript_tests"; +import { expect, jest, test } from "@jest/globals"; -const a = new wbg.A(); -wbg.A.other(); -a.foo(); -a.free(); -const b: boolean = a.ret_bool() -a.take_bool(b); -a.take_many(b, 1, 2); +test("member function (void) -> void", () => { + const a = new wbg.A(); + wbg.A.other(); + a.foo(); + a.free(); + expect(() => { + a.ret_bool(); + }).toThrow(/null pointer passed to rust/); +}); + +test("function with parameters", () => { + const a = new wbg.A(); + const b: boolean = a.ret_bool(); + expect(b).toStrictEqual(true); + a.take_bool(b); + a.take_many(b, 1, 2); +}); diff --git a/crates/typescript-tests/src/typescript_type.rs b/crates/typescript-tests/src/typescript_type.rs index 5bd7dc541b3..9ddc9d86890 100644 --- a/crates/typescript-tests/src/typescript_type.rs +++ b/crates/typescript-tests/src/typescript_type.rs @@ -1,3 +1,4 @@ +use serde::{Deserialize, Serialize}; use wasm_bindgen::prelude::*; #[wasm_bindgen(typescript_custom_section)] @@ -16,7 +17,7 @@ extern "C" { } #[wasm_bindgen] -#[derive(Default)] +#[derive(Default, Serialize, Deserialize)] pub struct TextStyle { pub bold: bool, pub italic: bool, @@ -26,13 +27,12 @@ pub struct TextStyle { #[wasm_bindgen] impl TextStyle { #[wasm_bindgen(constructor)] - pub fn new(_i: ITextStyle) -> TextStyle { - // parse JsValue - TextStyle::default() + pub fn new(i: ITextStyle) -> TextStyle { + let js_value: JsValue = i.into(); + serde_wasm_bindgen::from_value(js_value).unwrap() } - pub fn optional_new(_i: Option) -> TextStyle { - // parse JsValue - TextStyle::default() + pub fn optional_new(i: Option) -> TextStyle { + i.map(Self::new).unwrap_or_default() } } diff --git a/crates/typescript-tests/src/typescript_type.ts b/crates/typescript-tests/src/typescript_type.ts index a05ada63bd9..9d9bb0339f0 100644 --- a/crates/typescript-tests/src/typescript_type.ts +++ b/crates/typescript-tests/src/typescript_type.ts @@ -1,9 +1,30 @@ -import * as wbg from '../pkg/typescript_tests'; +import * as wbg from "../pkg/typescript_tests"; +import { expect, jest, test } from "@jest/globals"; -const style: wbg.TextStyle = new wbg.TextStyle({ - bold: true, - italic: true, - size: 42, +test("constructor", () => { + const style: wbg.TextStyle = new wbg.TextStyle({ + bold: true, + italic: false, + size: 42, + }); + + expect(style.bold).toStrictEqual(true); + expect(style.italic).toStrictEqual(false); + expect(style.size).toStrictEqual(42); }); -const optional_style: wbg.TextStyle = wbg.TextStyle.optional_new(); +test("optional parameter constructor", () => { + const default_constructed: wbg.TextStyle = wbg.TextStyle.optional_new(); + expect(default_constructed.bold).toStrictEqual(false); + expect(default_constructed.italic).toStrictEqual(false); + expect(default_constructed.size).toStrictEqual(0); + + const optional_style: wbg.TextStyle = wbg.TextStyle.optional_new({ + italic: true, + bold: false, + size: 0, + }); + expect(optional_style.bold).toStrictEqual(false); + expect(optional_style.italic).toStrictEqual(true); + expect(optional_style.size).toStrictEqual(0); +});