Skip to content

Commit

Permalink
Add a typescript test (not just a compile test) for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasetter committed Jan 1, 2024
1 parent 3e392db commit 440191f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
1 change: 0 additions & 1 deletion crates/typescript-tests/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module.exports = {
// TODO: migrate all test files and remove this
testPathIgnorePatterns: [
".*/src/custom_section.ts$",
".*/src/enums.ts$",
".*/src/getters_setters.ts$",
".*/src/inspectable.ts$",
".*/src/memory.ts$",
Expand Down
41 changes: 28 additions & 13 deletions crates/typescript-tests/src/enums.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import * as wbg from '../pkg/typescript_tests';

const a1: wbg.Foo = wbg.Foo.A;
const a2: wbg.Foo.A = wbg.Foo.A;
const a3: wbg.Foo.A = 1;
const b1: wbg.Foo = wbg.Foo.B;
const b2: wbg.Foo.B = wbg.Foo.B;
const b3: wbg.Foo.B = 3;

const fn_expects_enum: (_: wbg.Foo) => void = wbg.fn_expects_enum;
const fn_returns_enum: () => wbg.Foo = wbg.fn_returns_enum;
const fn_expects_option_enum: (_?: wbg.Foo) => void = wbg.fn_expects_option_enum;
const fn_returns_option_enum: () => wbg.Foo | undefined = wbg.fn_returns_option_enum;
import * as wbg from "../pkg/typescript_tests";
import { expect, jest, test } from "@jest/globals";

test("construction", () => {
const a1: wbg.Foo = wbg.Foo.A;
const a2: wbg.Foo.A = wbg.Foo.A;
expect(a1).toStrictEqual(a2);
const a3: wbg.Foo.A = 1;
expect(a1).toStrictEqual(a3);

const b1: wbg.Foo = wbg.Foo.B;
const b2: wbg.Foo.B = wbg.Foo.B;
expect(b1).toStrictEqual(b2);
const b3: wbg.Foo.B = 3;
expect(b1).toStrictEqual(b3);
expect(() => { return a1 as wbg.Foo != b1 }).toBeTruthy();
});

test("function calls", () => {
const fn_expects_enum: (_: wbg.Foo) => void = wbg.fn_expects_enum;
const fn_returns_enum: () => wbg.Foo = wbg.fn_returns_enum;
const fn_expects_option_enum: (_?: wbg.Foo) => void = wbg.fn_expects_option_enum;
const fn_returns_option_enum: () => wbg.Foo | undefined = wbg.fn_returns_option_enum;

fn_expects_enum(wbg.Foo.B);
expect(fn_returns_enum()).toStrictEqual(wbg.Foo.A);
expect(fn_returns_option_enum()).toStrictEqual(wbg.Foo.A);
});

0 comments on commit 440191f

Please sign in to comment.