forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundling.test.ts
98 lines (87 loc) · 2.32 KB
/
bundling.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import assert from "assert";
import { describe, it } from "vitest";
import { emitSchema } from "./utils.js";
describe("bundling", () => {
it("works", async () => {
const schemas = await emitSchema(
`
model Foo { }
model Bar { }
`,
{ bundleId: "test.json" }
);
const types = schemas["test.json"];
assert.strictEqual(types.$defs.Foo.$id, "Foo");
assert.strictEqual(types.$defs.Bar.$id, "Bar");
});
it("bundles non-schema dependencies by default", async () => {
const schemas = await emitSchema(
`
@jsonSchema
model test { b: Bar }
model Bar { b: Baz }
model Baz { }
`,
{},
{ emitNamespace: false }
);
assert.strictEqual(schemas["test.json"].$id, "test.json");
assert.strictEqual(schemas["test.json"].$defs.Bar.$id, "Bar.json");
assert.strictEqual(schemas["test.json"].$defs.Baz.$id, "Baz.json");
});
it("doesn't bundle non-schema dependencies when passing emitAllRefs", async () => {
const schemas = await emitSchema(
`
@jsonSchema
model test { b: Bar }
model Bar { }
`,
{ emitAllRefs: true },
{ emitNamespace: false }
);
assert.strictEqual(schemas["test.json"].$id, "test.json");
assert.strictEqual(schemas["test.json"].$defs, undefined);
assert.strictEqual(schemas["Bar.json"].$id, "Bar.json");
});
it("doesn't create duplicate defs for transitive references", async () => {
const schemas = await emitSchema(
`
model A {}
model B {
refA: A;
}
@jsonSchema
model C {
refB: B;
}
@jsonSchema
model D {
refB: B;
}
`,
{},
{ emitNamespace: false, emitTypes: ["C", "D"] }
);
const depSchemas = {
B: {
$schema: "https://json-schema.org/draft/2020-12/schema",
$id: "B.json",
type: "object",
properties: {
refA: {
$ref: "A.json",
},
},
required: ["refA"],
},
A: {
$schema: "https://json-schema.org/draft/2020-12/schema",
$id: "A.json",
type: "object",
properties: {},
},
};
assert.deepStrictEqual(schemas["C.json"].$defs, depSchemas);
assert.deepStrictEqual(schemas["D.json"].$defs, depSchemas);
});
});