Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test runner to two of the typescript tests to actually run them #3704

Merged
merged 5 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/typescript-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
17 changes: 17 additions & 0 deletions crates/typescript-tests/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -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
}
}
};
1 change: 1 addition & 0 deletions crates/typescript-tests/no_modules.tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"noImplicitAny": true,
"sourceMap": true,
"outDir": "dist_no_modules",
"moduleResolution":"node"
},
"include": [
"pkg/no_modules/*.d.ts",
Expand Down
10 changes: 7 additions & 3 deletions crates/typescript-tests/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
2 changes: 2 additions & 0 deletions crates/typescript-tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 19 additions & 8 deletions crates/typescript-tests/src/simple_struct.ts
Original file line number Diff line number Diff line change
@@ -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);
});
14 changes: 7 additions & 7 deletions crates/typescript-tests/src/typescript_type.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;

#[wasm_bindgen(typescript_custom_section)]
Expand All @@ -16,7 +17,7 @@ extern "C" {
}

#[wasm_bindgen]
#[derive(Default)]
#[derive(Default, Serialize, Deserialize)]
pub struct TextStyle {
pub bold: bool,
pub italic: bool,
Expand All @@ -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<ITextStyle>) -> TextStyle {
// parse JsValue
TextStyle::default()
pub fn optional_new(i: Option<ITextStyle>) -> TextStyle {
i.map(Self::new).unwrap_or_default()
}
}
33 changes: 27 additions & 6 deletions crates/typescript-tests/src/typescript_type.ts
Original file line number Diff line number Diff line change
@@ -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);
});