Skip to content

Commit 32eabfb

Browse files
lpotthastKeats
andcommitted
Add wasm tests using was-bindgen-test and wasm-pack (#346)
* Add wasm tests using was-bindgen-test and wasm-pack * Remove custom wasm feature * Add #[wasm_bindgen_test] to remaining tests * Try to fix CI --------- Co-authored-by: Vincent Prouillet <github@vincentprouillet.com>
1 parent 4e8ca23 commit 32eabfb

File tree

13 files changed

+150
-49
lines changed

13 files changed

+150
-49
lines changed

.github/workflows/ci.yml

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@ on:
55
- master
66
pull_request:
77

8-
98
jobs:
109
style:
1110
name: Format
1211
runs-on: ubuntu-latest
1312
steps:
14-
- uses: actions/checkout@v3
15-
- name: Install Rust
16-
uses: dtolnay/rust-toolchain@stable
17-
with:
18-
components: rustfmt
19-
- name: Check format
20-
run: cargo fmt --check
13+
- uses: actions/checkout@v3
14+
- name: Install Rust
15+
uses: dtolnay/rust-toolchain@stable
16+
with:
17+
components: rustfmt
18+
- name: Check format
19+
run: cargo fmt --check
2120

2221
clippy:
2322
name: Clippy
@@ -36,42 +35,49 @@ jobs:
3635
matrix:
3736
build: [pinned, stable, nightly]
3837
include:
39-
- build: pinned
40-
os: ubuntu-20.04
41-
rust: 1.67.0
42-
- build: stable
43-
os: ubuntu-20.04
44-
rust: stable
45-
- build: nightly
46-
os: ubuntu-20.04
47-
rust: nightly
38+
- build: pinned
39+
os: ubuntu-20.04
40+
rust: 1.67.0
41+
- build: stable
42+
os: ubuntu-20.04
43+
rust: stable
44+
- build: nightly
45+
os: ubuntu-20.04
46+
rust: nightly
47+
steps:
48+
- uses: actions/checkout@v3
49+
- name: Install Rust
50+
uses: dtolnay/rust-toolchain@master
51+
with:
52+
toolchain: ${{ matrix.rust }}
53+
54+
- name: Build System Info
55+
run: rustc --version
56+
57+
- name: Run tests default features
58+
run: cargo test
59+
60+
- name: Run tests no features
61+
run: cargo test --no-default-features
62+
63+
wasm:
64+
name: Run tests in wasm
65+
runs-on: ubuntu-latest
4866
steps:
49-
- uses: actions/checkout@v3
50-
- name: Install Rust
51-
uses: dtolnay/rust-toolchain@master
52-
with:
53-
toolchain: ${{ matrix.rust }}
67+
- uses: actions/checkout@v3
68+
- name: Install Rust
69+
uses: dtolnay/rust-toolchain@stable
70+
with:
71+
targets: wasm32-unknown-unknown
72+
73+
- uses: actions/setup-node@v4
5474

55-
- name: Build System Info
56-
run: rustc --version
75+
- name: Install wasm-pack
76+
run: cargo install wasm-pack
5777

58-
- name: Run tests default features
59-
run: cargo test
78+
- name: Run tests default features
79+
run: wasm-pack test --node
6080

61-
- name: Run tests no features
62-
run: cargo test --no-default-features
81+
- name: Run tests no features
82+
run: wasm-pack test --node --no-default-features
6383

64-
# wasm:
65-
# name: Run tests in wasm
66-
# runs-on: ubuntu-latest
67-
# steps:
68-
# - uses: actions/checkout@v3
69-
# - name: Install Rust
70-
# uses: dtolnay/rust-toolchain@stable
71-
# with:
72-
# targets: wasm32-unknown-unknown
73-
# - name: Run tests default features
74-
# run: cargo test --target wasm32-unknown-unknown
75-
#
76-
# - name: Run tests no features
77-
# run: cargo test --no-default-features --target wasm32-unknown-unknown

Cargo.toml

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,44 @@ homepage = "https://github.com/Keats/jsonwebtoken"
99
repository = "https://github.com/Keats/jsonwebtoken"
1010
keywords = ["jwt", "api", "token", "jwk"]
1111
edition = "2021"
12-
include = ["src/**/*", "benches/**/*", "tests/**/*", "LICENSE", "README.md", "CHANGELOG.md"]
12+
include = [
13+
"src/**/*",
14+
"benches/**/*",
15+
"tests/**/*",
16+
"LICENSE",
17+
"README.md",
18+
"CHANGELOG.md",
19+
]
1320
rust-version = "1.67.0"
1421

1522
[dependencies]
1623
serde_json = "1.0"
17-
serde = {version = "1.0", features = ["derive"] }
24+
serde = { version = "1.0", features = ["derive"] }
1825
base64 = "0.21.0"
1926
# For PEM decoding
20-
pem = {version = "3", optional = true}
21-
simple_asn1 = {version = "0.6", optional = true}
27+
pem = { version = "3", optional = true }
28+
simple_asn1 = { version = "0.6", optional = true }
2229

2330
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
2431
ring = { version = "0.17.4", features = ["std"] }
2532

26-
2733
[target.'cfg(target_arch = "wasm32")'.dependencies]
2834
js-sys = "0.3"
2935
ring = { version = "0.17.4", features = ["std", "wasm32_unknown_unknown_js"] }
3036

3137
[dev-dependencies]
38+
wasm-bindgen-test = "0.3.1"
39+
40+
[target.'cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))'.dev-dependencies]
3241
# For the custom time example
3342
time = "0.3"
3443
criterion = "0.4"
3544

45+
[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dev-dependencies]
46+
# For the custom time example
47+
time = { version = "0.3", features = ["wasm-bindgen"] }
48+
criterion = { version = "0.4", default-features = false }
49+
3650
[features]
3751
default = ["use_pem"]
3852
use_pem = ["pem", "simple_asn1"]

examples/custom_header.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header,
77
struct Claims {
88
sub: String,
99
company: String,
10-
exp: usize,
10+
exp: u64,
1111
}
1212

1313
fn main() {

examples/validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct Claims {
77
aud: String,
88
sub: String,
99
company: String,
10-
exp: usize,
10+
exp: u64,
1111
}
1212

1313
fn main() {

src/algorithms.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,12 @@ impl Algorithm {
8484

8585
#[cfg(test)]
8686
mod tests {
87+
use wasm_bindgen_test::wasm_bindgen_test;
88+
8789
use super::*;
8890

8991
#[test]
92+
#[wasm_bindgen_test]
9093
fn generate_algorithm_enum_from_str() {
9194
assert!(Algorithm::from_str("HS256").is_ok());
9295
assert!(Algorithm::from_str("HS384").is_ok());

src/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,12 @@ impl From<ErrorKind> for Error {
179179

180180
#[cfg(test)]
181181
mod tests {
182+
use wasm_bindgen_test::wasm_bindgen_test;
183+
182184
use super::*;
183185

184186
#[test]
187+
#[wasm_bindgen_test]
185188
fn test_error_rendering() {
186189
assert_eq!(
187190
"InvalidAlgorithmName",

src/jwk.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,10 @@ mod tests {
439439
use crate::serialization::b64_encode;
440440
use crate::Algorithm;
441441
use serde_json::json;
442+
use wasm_bindgen_test::wasm_bindgen_test;
442443

443444
#[test]
445+
#[wasm_bindgen_test]
444446
fn check_hs256() {
445447
let key = b64_encode("abcdefghijklmnopqrstuvwxyz012345");
446448
let jwks_json = json!({

0 commit comments

Comments
 (0)