/
normalize-options.spec.js
136 lines (134 loc) 路 4.99 KB
/
normalize-options.spec.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const describeSkipPublish = process.env.IS_PUBLISH ? describe.skip : describe;
describeSkipPublish("normalize options", () => {
let normalizeOptions;
beforeAll(async () => {
({
default: { default: normalizeOptions },
} = await import("../lib/normalize-options.js"));
});
(process.env.BABEL_8_BREAKING ? describe : describe.skip)("Babel 8", () => {
it("should throw on unknown options", () => {
expect(() => normalizeOptions({ throwIfNamespaces: true })).toThrowError(
"@babel/preset-react: 'throwIfNamespaces' is not a valid top-level option.\n- Did you mean 'throwIfNamespace'?",
);
});
it.each(["development", "pure", "throwIfNamespace"])(
"should throw when `%p` is not a boolean",
optionName => {
expect(() => normalizeOptions({ [optionName]: 0 })).toThrow(
`@babel/preset-react: '${optionName}' option must be a boolean.`,
);
},
);
it.each(["importSource", "pragma", "pragmaFrag", "runtime"])(
"should throw when `%p` is not a string",
optionName => {
expect(() => normalizeOptions({ [optionName]: 0 })).toThrow(
`@babel/preset-react: '${optionName}' option must be a string.`,
);
},
);
it("should throw on Babel 7 'useBuiltIns' option", () => {
expect(() => normalizeOptions({ useBuiltIns: true }))
.toThrowErrorMatchingInlineSnapshot(`
"@babel/preset-react: Since \\"useBuiltIns\\" is removed in Babel 8, you can remove it from the config.
- Babel 8 now transforms JSX spread to object spread. If you need to transpile object spread with
\`useBuiltIns: true\`, you can use the following config
{
\\"plugins\\": [
[\\"@babel/plugin-proposal-object-rest-spread\\", { \\"loose\\": true, \\"useBuiltIns\\": true }]
],
\\"presets\\": [\\"@babel/preset-react\\"]
}"
`);
});
it("should throw on Babel 7 'useSpread' option", () => {
expect(() =>
normalizeOptions({ useSpread: true }),
).toThrowErrorMatchingInlineSnapshot(
`"@babel/preset-react: Since Babel 8, an inline object with spread elements is always used, and the \\"useSpread\\" option is no longer available. Please remove it from your config."`,
);
});
it("should throw on unknown 'runtime' option", () => {
expect(() => normalizeOptions({ runtime: "classical" }))
.toThrowErrorMatchingInlineSnapshot(`
"@babel/preset-react: 'runtime' must be one of ['automatic', 'classic'] but we have 'classical'
- Did you mean 'classic'?"
`);
});
it("should not throw when options are not defined", () => {
expect(() => normalizeOptions()).not.toThrowError();
});
it("default values", () => {
expect(normalizeOptions({})).toMatchInlineSnapshot(`
Object {
"development": false,
"importSource": "react",
"pragma": undefined,
"pragmaFrag": undefined,
"pure": undefined,
"runtime": "automatic",
"throwIfNamespace": true,
}
`);
expect(normalizeOptions({ runtime: "classic" })).toMatchInlineSnapshot(`
Object {
"development": false,
"importSource": undefined,
"pragma": "React.createElement",
"pragmaFrag": "React.Fragment",
"pure": undefined,
"runtime": "classic",
"throwIfNamespace": true,
}
`);
});
});
(process.env.BABEL_8_BREAKING ? describe.skip : describe)("Babel 7", () => {
it("should not throw on unknown options", () => {
expect(() =>
normalizeOptions({ throwIfNamespaces: true }),
).not.toThrowError();
});
it.each(["development", "pure", "throwIfNamespace"])(
"should not throw when `%p` is not a boolean",
optionName => {
expect(() => normalizeOptions({ [optionName]: 0 })).not.toThrowError();
},
);
it.each(["importSource", "pragma", "pragmaFrag", "runtime"])(
"should throw when `%p` is not a string",
optionName => {
expect(() => normalizeOptions({ [optionName]: 0 })).not.toThrowError();
},
);
it("default values", () => {
expect(normalizeOptions({})).toMatchInlineSnapshot(`
Object {
"development": false,
"importSource": undefined,
"pragma": "React.createElement",
"pragmaFrag": "React.Fragment",
"pure": undefined,
"runtime": "classic",
"throwIfNamespace": true,
"useBuiltIns": undefined,
"useSpread": undefined,
}
`);
expect(normalizeOptions({ runtime: "automatic" })).toMatchInlineSnapshot(`
Object {
"development": false,
"importSource": undefined,
"pragma": undefined,
"pragmaFrag": undefined,
"pure": undefined,
"runtime": "automatic",
"throwIfNamespace": true,
"useBuiltIns": undefined,
"useSpread": undefined,
}
`);
});
});
});