Skip to content

Commit cfe3dc2

Browse files
authored
Add a test runner to two of the typescript tests to actually run them (#3704)
* Add jest for simple_struct.ts, this causes it to fail * fix the test, add another failing one * Fix the typescript_type test * do not use jest global injection * add TODO for the rest of the tests
1 parent def9147 commit cfe3dc2

8 files changed

+82
-24
lines changed

crates/typescript-tests/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ wasm-bindgen = { path = '../..' }
1010
wasm-bindgen-futures = { path = '../futures' }
1111
web-sys = { path = '../web-sys', features = [ 'HtmlElement', 'Node', 'Document' ] }
1212
js-sys = { path = '../js-sys' }
13+
serde = { version = "1.0", features = ["derive"] }
14+
serde-wasm-bindgen = "0.6"
1315

1416
[lib]
1517
crate-type = ['cdylib']
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: "ts-jest/presets/default-esm",
4+
testEnvironment: 'node',
5+
extensionsToTreatAsEsm: [".ts"],
6+
verbose: true,
7+
// TODO: match all test files
8+
testMatch: ['**/src/simple_struct.ts', '**/src/typescript_type.ts'],
9+
injectGlobals: false,
10+
globals: {
11+
'ts-jest':
12+
{
13+
useESM: true,
14+
isolatedModules: true
15+
}
16+
}
17+
};

crates/typescript-tests/no_modules.tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"noImplicitAny": true,
55
"sourceMap": true,
66
"outDir": "dist_no_modules",
7+
"moduleResolution":"node"
78
},
89
"include": [
910
"pkg/no_modules/*.d.ts",

crates/typescript-tests/package.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
{
22
"scripts": {
3-
"tsc": "tsc"
3+
"tsc": "tsc",
4+
"test": "NODE_OPTIONS=--experimental-vm-modules jest --config ./jest.config.cjs"
45
},
56
"devDependencies": {
6-
"typescript": "^3.3.3333"
7-
}
7+
"@types/jest": "^29.5.8",
8+
"ts-jest": "^29.1.1",
9+
"typescript": "^5.2.2"
10+
},
11+
"type": "module"
812
}

crates/typescript-tests/run.sh

+2
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,5 @@ cd ..
5757

5858
# Then try to build the typescript in the src_no_modules folder against the pkg/no_modules build.
5959
npm run tsc -- -p no_modules.tsconfig.json
60+
61+
npm test
+19-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
import * as wbg from '../pkg/typescript_tests';
1+
import * as wbg from "../pkg/typescript_tests";
2+
import { expect, jest, test } from "@jest/globals";
23

3-
const a = new wbg.A();
4-
wbg.A.other();
5-
a.foo();
6-
a.free();
7-
const b: boolean = a.ret_bool()
8-
a.take_bool(b);
9-
a.take_many(b, 1, 2);
4+
test("member function (void) -> void", () => {
5+
const a = new wbg.A();
6+
wbg.A.other();
7+
a.foo();
8+
a.free();
9+
expect(() => {
10+
a.ret_bool();
11+
}).toThrow(/null pointer passed to rust/);
12+
});
13+
14+
test("function with parameters", () => {
15+
const a = new wbg.A();
16+
const b: boolean = a.ret_bool();
17+
expect(b).toStrictEqual(true);
18+
a.take_bool(b);
19+
a.take_many(b, 1, 2);
20+
});

crates/typescript-tests/src/typescript_type.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use serde::{Deserialize, Serialize};
12
use wasm_bindgen::prelude::*;
23

34
#[wasm_bindgen(typescript_custom_section)]
@@ -16,7 +17,7 @@ extern "C" {
1617
}
1718

1819
#[wasm_bindgen]
19-
#[derive(Default)]
20+
#[derive(Default, Serialize, Deserialize)]
2021
pub struct TextStyle {
2122
pub bold: bool,
2223
pub italic: bool,
@@ -26,13 +27,12 @@ pub struct TextStyle {
2627
#[wasm_bindgen]
2728
impl TextStyle {
2829
#[wasm_bindgen(constructor)]
29-
pub fn new(_i: ITextStyle) -> TextStyle {
30-
// parse JsValue
31-
TextStyle::default()
30+
pub fn new(i: ITextStyle) -> TextStyle {
31+
let js_value: JsValue = i.into();
32+
serde_wasm_bindgen::from_value(js_value).unwrap()
3233
}
3334

34-
pub fn optional_new(_i: Option<ITextStyle>) -> TextStyle {
35-
// parse JsValue
36-
TextStyle::default()
35+
pub fn optional_new(i: Option<ITextStyle>) -> TextStyle {
36+
i.map(Self::new).unwrap_or_default()
3737
}
3838
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1-
import * as wbg from '../pkg/typescript_tests';
1+
import * as wbg from "../pkg/typescript_tests";
2+
import { expect, jest, test } from "@jest/globals";
23

3-
const style: wbg.TextStyle = new wbg.TextStyle({
4-
bold: true,
5-
italic: true,
6-
size: 42,
4+
test("constructor", () => {
5+
const style: wbg.TextStyle = new wbg.TextStyle({
6+
bold: true,
7+
italic: false,
8+
size: 42,
9+
});
10+
11+
expect(style.bold).toStrictEqual(true);
12+
expect(style.italic).toStrictEqual(false);
13+
expect(style.size).toStrictEqual(42);
714
});
815

9-
const optional_style: wbg.TextStyle = wbg.TextStyle.optional_new();
16+
test("optional parameter constructor", () => {
17+
const default_constructed: wbg.TextStyle = wbg.TextStyle.optional_new();
18+
expect(default_constructed.bold).toStrictEqual(false);
19+
expect(default_constructed.italic).toStrictEqual(false);
20+
expect(default_constructed.size).toStrictEqual(0);
21+
22+
const optional_style: wbg.TextStyle = wbg.TextStyle.optional_new({
23+
italic: true,
24+
bold: false,
25+
size: 0,
26+
});
27+
expect(optional_style.bold).toStrictEqual(false);
28+
expect(optional_style.italic).toStrictEqual(true);
29+
expect(optional_style.size).toStrictEqual(0);
30+
});

0 commit comments

Comments
 (0)