Skip to content

Commit 7e690eb

Browse files
authored
Migrate more api/node tests to Vitest (#10110)
These are the simplest tests to convert. As of this PR 12 out of 58 tests have been migrated.
1 parent 61d40cf commit 7e690eb

File tree

5 files changed

+205
-168
lines changed

5 files changed

+205
-168
lines changed

api/node/__test__/globals.spec.mts

Lines changed: 124 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,103 @@
11
// Copyright © SixtyFPS GmbH <[email protected]>
22
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
33

4-
import test from "ava";
4+
import { test, expect } from "vitest";
55

66
import { private_api } from "../dist/index.js";
77

8-
test("get/set global properties", (t) => {
8+
test("get/set global properties", () => {
99
const compiler = new private_api.ComponentCompiler();
1010
const definition = compiler.buildFromSource(
1111
`
1212
export global Global { in-out property <string> name: "Initial"; }
1313
export component App {}`,
1414
"",
1515
);
16-
t.not(definition.App, null);
16+
expect(definition.App).not.toBeNull();
1717

1818
const instance = definition.App!.create();
19-
t.not(instance, null);
19+
expect(instance).not.toBeNull();
2020

21-
t.is(instance!.getGlobalProperty("Global", "name"), "Initial");
21+
expect(instance!.getGlobalProperty("Global", "name")).toBe("Initial");
2222

2323
instance!.setGlobalProperty("Global", "name", "Hello");
24-
t.is(instance!.getGlobalProperty("Global", "name"), "Hello");
24+
expect(instance!.getGlobalProperty("Global", "name")).toBe("Hello");
2525

26-
t.throws(
27-
() => {
26+
{
27+
let thrownError: any;
28+
try {
2829
instance!.getGlobalProperty("MyGlobal", "name");
29-
},
30-
{
31-
code: "GenericFailure",
32-
message: "Global MyGlobal not found",
33-
},
34-
);
30+
} catch (error) {
31+
thrownError = error;
32+
}
33+
expect(thrownError).toBeDefined();
34+
expect(thrownError.code).toBe("GenericFailure");
35+
expect(thrownError.message).toBe("Global MyGlobal not found");
36+
}
3537

36-
t.throws(
37-
() => {
38+
{
39+
let thrownError: any;
40+
try {
3841
instance!.setGlobalProperty("MyGlobal", "name", "hello");
39-
},
40-
{
41-
code: "GenericFailure",
42-
message: "Global MyGlobal not found",
43-
},
44-
);
42+
} catch (error) {
43+
thrownError = error;
44+
}
45+
expect(thrownError).toBeDefined();
46+
expect(thrownError.code).toBe("GenericFailure");
47+
expect(thrownError.message).toBe("Global MyGlobal not found");
48+
}
4549

46-
t.throws(
47-
() => {
50+
{
51+
let thrownError: any;
52+
try {
4853
instance!.getGlobalProperty("Global", "age");
49-
},
50-
{
51-
code: "GenericFailure",
52-
message: "no such property",
53-
},
54-
);
54+
} catch (error) {
55+
thrownError = error;
56+
}
57+
expect(thrownError).toBeDefined();
58+
expect(thrownError.code).toBe("GenericFailure");
59+
expect(thrownError.message).toBe("no such property");
60+
}
5561

56-
t.throws(
57-
() => {
62+
{
63+
let thrownError: any;
64+
try {
5865
instance!.setGlobalProperty("Global", "age", 42);
59-
},
60-
{
61-
code: "GenericFailure",
62-
message: "Property age of global Global not found in the component",
63-
},
64-
);
65-
66-
t.throws(
67-
() => {
66+
} catch (error) {
67+
thrownError = error;
68+
}
69+
expect(thrownError).toBeDefined();
70+
expect(thrownError.code).toBe("GenericFailure");
71+
expect(thrownError.message).toBe(
72+
"Property age of global Global not found in the component",
73+
);
74+
}
75+
{
76+
let thrownError: any;
77+
try {
6878
instance!.setGlobalProperty("Global", "name", 42);
69-
},
70-
{
71-
code: "InvalidArg",
72-
message: "expect String, got: Number",
73-
},
74-
);
79+
} catch (error) {
80+
thrownError = error;
81+
}
82+
expect(thrownError).toBeDefined();
83+
expect(thrownError.code).toBe("InvalidArg");
84+
expect(thrownError.message).toBe("expect String, got: Number");
85+
}
7586

76-
t.throws(
77-
() => {
87+
{
88+
let thrownError: any;
89+
try {
7890
instance!.setGlobalProperty("Global", "name", { blah: "foo" });
79-
},
80-
{
81-
code: "InvalidArg",
82-
message: "expect String, got: Object",
83-
},
84-
);
91+
} catch (error) {
92+
thrownError = error;
93+
}
94+
expect(thrownError).toBeDefined();
95+
expect(thrownError.code).toBe("InvalidArg");
96+
expect(thrownError.message).toBe("expect String, got: Object");
97+
}
8598
});
8699

87-
test("invoke global callback", (t) => {
100+
test("invoke global callback", () => {
88101
const compiler = new private_api.ComponentCompiler();
89102
const definition = compiler.buildFromSource(
90103
`
@@ -112,30 +125,34 @@ test("invoke global callback", (t) => {
112125
`,
113126
"",
114127
);
115-
t.not(definition.App, null);
128+
expect(definition.App).not.toBeNull();
116129

117130
const instance = definition.App!.create();
118-
t.not(instance, null);
131+
expect(instance).not.toBeNull();
119132

120-
t.throws(
121-
() => {
133+
{
134+
let thrownError: any;
135+
try {
122136
instance!.setGlobalCallback("MyGlobal", "great", () => {});
123-
},
124-
{
125-
code: "GenericFailure",
126-
message: "Global MyGlobal not found",
127-
},
128-
);
137+
} catch (error) {
138+
thrownError = error;
139+
}
140+
expect(thrownError).toBeDefined();
141+
expect(thrownError.code).toBe("GenericFailure");
142+
expect(thrownError.message).toBe("Global MyGlobal not found");
143+
}
129144

130-
t.throws(
131-
() => {
145+
{
146+
let thrownError: any;
147+
try {
132148
instance!.invokeGlobal("MyGlobal", "great", []);
133-
},
134-
{
135-
code: "GenericFailure",
136-
message: "Global MyGlobal not found",
137-
},
138-
);
149+
} catch (error) {
150+
thrownError = error;
151+
}
152+
expect(thrownError).toBeDefined();
153+
expect(thrownError.code).toBe("GenericFailure");
154+
expect(thrownError.message).toBe("Global MyGlobal not found");
155+
}
139156

140157
let speakTest: string;
141158
instance!.setGlobalCallback(
@@ -147,25 +164,33 @@ test("invoke global callback", (t) => {
147164
},
148165
);
149166

150-
t.throws(
151-
() => {
167+
{
168+
let thrownError: any;
169+
try {
152170
instance!.setGlobalCallback("Global", "bye", () => {});
153-
},
154-
{
155-
code: "GenericFailure",
156-
message: "Callback bye of global Global not found in the component",
157-
},
158-
);
171+
} catch (error) {
172+
thrownError = error;
173+
}
174+
expect(thrownError).toBeDefined();
175+
expect(thrownError.code).toBe("GenericFailure");
176+
expect(thrownError.message).toBe(
177+
"Callback bye of global Global not found in the component",
178+
);
179+
}
159180

160-
t.throws(
161-
() => {
181+
{
182+
let thrownError: any;
183+
try {
162184
instance!.invokeGlobal("Global", "bye", []);
163-
},
164-
{
165-
code: "GenericFailure",
166-
message: "Callback bye of global Global not found in the component",
167-
},
168-
);
185+
} catch (error) {
186+
thrownError = error;
187+
}
188+
expect(thrownError).toBeDefined();
189+
expect(thrownError.code).toBe("GenericFailure");
190+
expect(thrownError.message).toBe(
191+
"Callback bye of global Global not found in the component",
192+
);
193+
}
169194

170195
instance!.invokeGlobal("Global", "great", [
171196
"simon",
@@ -174,20 +199,24 @@ test("invoke global callback", (t) => {
174199
"tobias",
175200
"florian",
176201
]);
177-
t.deepEqual(speakTest, "hello simon, olivier, auri, tobias and florian");
202+
expect(speakTest).toStrictEqual(
203+
"hello simon, olivier, auri, tobias and florian",
204+
);
178205

179206
instance!.setGlobalCallback("Global", "great-person", (p: any) => {
180207
speakTest = "hello " + p.name;
181208
});
182209

183210
instance!.invokeGlobal("Global", "great-person", [{ name: "simon" }]);
184-
t.deepEqual(speakTest, "hello simon");
211+
expect(speakTest).toStrictEqual("hello simon");
185212

186213
instance!.invokeGlobal("Global", "great-person", [{ hello: "simon" }]);
187-
t.deepEqual(speakTest, "hello ");
214+
expect(speakTest).toStrictEqual("hello ");
188215

189-
t.deepEqual(instance!.invokeGlobal("Global", "get-string", []), "string");
190-
t.deepEqual(instance!.invokeGlobal("Global", "person", []), {
216+
expect(instance!.invokeGlobal("Global", "get-string", [])).toStrictEqual(
217+
"string",
218+
);
219+
expect(instance!.invokeGlobal("Global", "person", [])).toStrictEqual({
191220
name: "florian",
192221
});
193222
});

api/node/__test__/models.spec.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright © SixtyFPS GmbH <[email protected]>
22
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
33

4-
import test from "ava";
4+
import { test, expect } from "vitest";
55
import * as path from "node:path";
66
import { fileURLToPath } from "node:url";
77

@@ -14,7 +14,7 @@ import {
1414
Model,
1515
} from "../dist/index.js";
1616

17-
test("MapModel notify rowChanged", (t) => {
17+
test("MapModel notify rowChanged", () => {
1818
const source = `
1919
export component App {
2020
@@ -60,5 +60,5 @@ test("MapModel notify rowChanged", (t) => {
6060

6161
private_api.send_mouse_click(instance, 5, 5);
6262

63-
t.is(instance.changed_items, "Goffart, OlivierHausmann, Simon");
63+
expect(instance.changed_items).toBe("Goffart, OlivierHausmann, Simon");
6464
});

0 commit comments

Comments
 (0)