Skip to content

Commit 114a4a1

Browse files
authored
Add a typescript test (not just a compile test) for enums (#3740)
1 parent e9cb333 commit 114a4a1

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

crates/typescript-tests/jest.config.cjs

+15-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,21 @@ module.exports = {
44
testEnvironment: 'node',
55
extensionsToTreatAsEsm: [".ts"],
66
verbose: true,
7-
// TODO: match all test files
8-
testMatch: ['**/src/simple_struct.ts', '**/src/typescript_type.ts'],
7+
testMatch: ['**/src/*.ts'],
8+
// TODO: migrate all test files and remove this
9+
testPathIgnorePatterns: [
10+
".*/src/custom_section.ts$",
11+
".*/src/getters_setters.ts$",
12+
".*/src/inspectable.ts$",
13+
".*/src/memory.ts$",
14+
".*/src/omit_definition.ts$",
15+
".*/src/optional_fields.ts$",
16+
".*/src/opt_args_and_ret.ts$",
17+
".*/src/simple_async_fn.ts$",
18+
".*/src/simple_fn.ts$",
19+
".*/src/web_sys.ts$",
20+
".*/src/usize.ts$"
21+
],
922
injectGlobals: false,
1023
globals: {
1124
'ts-jest':

crates/typescript-tests/src/enums.ts

+28-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1-
import * as wbg from '../pkg/typescript_tests';
2-
3-
const a1: wbg.Foo = wbg.Foo.A;
4-
const a2: wbg.Foo.A = wbg.Foo.A;
5-
const a3: wbg.Foo.A = 1;
6-
const b1: wbg.Foo = wbg.Foo.B;
7-
const b2: wbg.Foo.B = wbg.Foo.B;
8-
const b3: wbg.Foo.B = 3;
9-
10-
const fn_expects_enum: (_: wbg.Foo) => void = wbg.fn_expects_enum;
11-
const fn_returns_enum: () => wbg.Foo = wbg.fn_returns_enum;
12-
const fn_expects_option_enum: (_?: wbg.Foo) => void = wbg.fn_expects_option_enum;
13-
const fn_returns_option_enum: () => wbg.Foo | undefined = wbg.fn_returns_option_enum;
1+
import * as wbg from "../pkg/typescript_tests";
2+
import { expect, jest, test } from "@jest/globals";
3+
4+
test("construction", () => {
5+
const a1: wbg.Foo = wbg.Foo.A;
6+
const a2: wbg.Foo.A = wbg.Foo.A;
7+
expect(a1).toStrictEqual(a2);
8+
const a3: wbg.Foo.A = 1;
9+
expect(a1).toStrictEqual(a3);
10+
11+
const b1: wbg.Foo = wbg.Foo.B;
12+
const b2: wbg.Foo.B = wbg.Foo.B;
13+
expect(b1).toStrictEqual(b2);
14+
const b3: wbg.Foo.B = 3;
15+
expect(b1).toStrictEqual(b3);
16+
expect(a1).not.toStrictEqual(b1);
17+
});
18+
19+
test("function calls", () => {
20+
const fn_expects_enum: (_: wbg.Foo) => void = wbg.fn_expects_enum;
21+
const fn_returns_enum: () => wbg.Foo = wbg.fn_returns_enum;
22+
const fn_expects_option_enum: (_?: wbg.Foo) => void = wbg.fn_expects_option_enum;
23+
const fn_returns_option_enum: () => wbg.Foo | undefined = wbg.fn_returns_option_enum;
24+
25+
fn_expects_enum(wbg.Foo.B);
26+
expect(fn_returns_enum()).toStrictEqual(wbg.Foo.A);
27+
expect(fn_returns_option_enum()).toStrictEqual(wbg.Foo.A);
28+
});

0 commit comments

Comments
 (0)