forked from babel/babel
/
tsLiteralType.js
52 lines (48 loc) · 1.85 KB
/
tsLiteralType.js
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
import * as t from "../../..";
describe("builders", function () {
describe("typescript", function () {
describe("tsLiteralType", function () {
it("accept unary expression", function () {
expect(
t.tsLiteralType(t.unaryExpression("-", t.numericLiteral(1))),
).toMatchSnapshot();
expect(
t.tsLiteralType(t.unaryExpression("-", t.bigIntLiteral("123456789"))),
).toMatchSnapshot();
});
it("throws with non-numeric argument", function () {
expect(() => {
t.tsLiteralType(t.unaryExpression("-", t.stringLiteral(1)));
}).toThrow("Property value expected type of string but got number");
expect(() => {
t.tsLiteralType(t.unaryExpression("-", t.objectExpression([])));
}).toThrow(
'Property argument of UnaryExpression expected node to be of a type ["NumericLiteral","BigIntLiteral"] but instead got "ObjectExpression"',
);
});
});
it("throws with bad operator", function () {
expect(() => {
t.tsLiteralType(t.unaryExpression("+", t.numericLiteral(1)));
}).toThrow(
'Property operator expected value to be one of ["-"] but got "+"',
);
// should check operator first since it appears first
expect(() => {
t.tsLiteralType(t.unaryExpression("+", t.objectExpression([])));
}).toThrow(
'Property operator expected value to be one of ["-"] but got "+"',
);
expect(() => {
t.tsLiteralType(t.unaryExpression("~", t.numericLiteral(1)));
}).toThrow(
'Property operator expected value to be one of ["-"] but got "~"',
);
expect(() => {
t.tsLiteralType(t.unaryExpression("void", t.numericLiteral(1)));
}).toThrow(
'Property operator expected value to be one of ["-"] but got "void"',
);
});
});
});