| 1 |
import _normalizeOptions from "../lib/normalize-options.js"; |
| 2 |
const normalizeOptions = _normalizeOptions.default || _normalizeOptions; |
| 3 |
|
| 4 |
describe("normalize options", () => { |
| 5 |
(process.env.BABEL_8_BREAKING ? describe : describe.skip)("Babel 8", () => { |
| 6 |
it("should throw on unknown options", () => { |
| 7 |
expect(() => normalizeOptions({ throwIfNamespaces: true })).toThrow( |
| 8 |
"@babel/preset-react: 'throwIfNamespaces' is not a valid top-level option.\n- Did you mean 'throwIfNamespace'?", |
| 9 |
); |
| 10 |
}); |
| 11 |
it.each(["development", "pure", "throwIfNamespace"])( |
| 12 |
"should throw when `%p` is not a boolean", |
| 13 |
optionName => { |
| 14 |
expect(() => normalizeOptions({ [optionName]: 0 })).toThrow( |
| 15 |
`@babel/preset-react: '${optionName}' option must be a boolean.`, |
| 16 |
); |
| 17 |
}, |
| 18 |
); |
| 19 |
it.each(["importSource", "pragma", "pragmaFrag", "runtime"])( |
| 20 |
"should throw when `%p` is not a string", |
| 21 |
optionName => { |
| 22 |
expect(() => normalizeOptions({ [optionName]: 0 })).toThrow( |
| 23 |
`@babel/preset-react: '${optionName}' option must be a string.`, |
| 24 |
); |
| 25 |
}, |
| 26 |
); |
| 27 |
it("should throw on Babel 7 'useBuiltIns' option", () => { |
| 28 |
expect(() => normalizeOptions({ useBuiltIns: true })) |
| 29 |
.toThrowErrorMatchingInlineSnapshot(` |
| 30 |
"@babel/preset-react: Since \\"useBuiltIns\\" is removed in Babel 8, you can remove it from the config. |
| 31 |
- Babel 8 now transforms JSX spread to object spread. If you need to transpile object spread with |
| 32 |
\`useBuiltIns: true\`, you can use the following config |
| 33 |
{ |
| 34 |
\\"plugins\\": [ |
| 35 |
[\\"@babel/plugin-proposal-object-rest-spread\\", { \\"loose\\": true, \\"useBuiltIns\\": true }] |
| 36 |
], |
| 37 |
\\"presets\\": [\\"@babel/preset-react\\"] |
| 38 |
}" |
| 39 |
`); |
| 40 |
}); |
| 41 |
it("should throw on Babel 7 'useSpread' option", () => { |
| 42 |
expect(() => |
| 43 |
normalizeOptions({ useSpread: true }), |
| 44 |
).toThrowErrorMatchingInlineSnapshot( |
| 45 |
`"@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."`, |
| 46 |
); |
| 47 |
}); |
| 48 |
it("should throw on unknown 'runtime' option", () => { |
| 49 |
expect(() => normalizeOptions({ runtime: "classical" })) |
| 50 |
.toThrowErrorMatchingInlineSnapshot(` |
| 51 |
"@babel/preset-react: 'runtime' must be one of ['automatic', 'classic'] but we have 'classical' |
| 52 |
- Did you mean 'classic'?" |
| 53 |
`); |
| 54 |
}); |
| 55 |
it("should not throw when options are not defined", () => { |
| 56 |
expect(() => normalizeOptions()).not.toThrow(); |
| 57 |
}); |
| 58 |
it("default values", () => { |
| 59 |
expect(normalizeOptions({})).toMatchInlineSnapshot(` |
| 60 |
Object { |
| 61 |
"development": false, |
| 62 |
"importSource": "react", |
| 63 |
"pragma": undefined, |
| 64 |
"pragmaFrag": undefined, |
| 65 |
"pure": undefined, |
| 66 |
"runtime": "automatic", |
| 67 |
"throwIfNamespace": true, |
| 68 |
} |
| 69 |
`); |
| 70 |
expect(normalizeOptions({ runtime: "classic" })).toMatchInlineSnapshot(` |
| 71 |
Object { |
| 72 |
"development": false, |
| 73 |
"importSource": undefined, |
| 74 |
"pragma": "React.createElement", |
| 75 |
"pragmaFrag": "React.Fragment", |
| 76 |
"pure": undefined, |
| 77 |
"runtime": "classic", |
| 78 |
"throwIfNamespace": true, |
| 79 |
} |
| 80 |
`); |
| 81 |
}); |
| 82 |
}); |
| 83 |
(process.env.BABEL_8_BREAKING ? describe.skip : describe)("Babel 7", () => { |
| 84 |
it("should not throw on unknown options", () => { |
| 85 |
expect(() => normalizeOptions({ throwIfNamespaces: true })).not.toThrow(); |
| 86 |
}); |
| 87 |
it.each(["development", "pure", "throwIfNamespace"])( |
| 88 |
"should not throw when `%p` is not a boolean", |
| 89 |
optionName => { |
| 90 |
expect(() => normalizeOptions({ [optionName]: 0 })).not.toThrow(); |
| 91 |
}, |
| 92 |
); |
| 93 |
it.each(["importSource", "pragma", "pragmaFrag", "runtime"])( |
| 94 |
"should throw when `%p` is not a string", |
| 95 |
optionName => { |
| 96 |
expect(() => normalizeOptions({ [optionName]: 0 })).not.toThrow(); |
| 97 |
}, |
| 98 |
); |
| 99 |
it("default values", () => { |
| 100 |
expect(normalizeOptions({})).toMatchInlineSnapshot(` |
| 101 |
Object { |
| 102 |
"development": false, |
| 103 |
"importSource": undefined, |
| 104 |
"pragma": "React.createElement", |
| 105 |
"pragmaFrag": "React.Fragment", |
| 106 |
"pure": undefined, |
| 107 |
"runtime": "classic", |
| 108 |
"throwIfNamespace": true, |
| 109 |
"useBuiltIns": undefined, |
| 110 |
"useSpread": undefined, |
| 111 |
} |
| 112 |
`); |
| 113 |
expect(normalizeOptions({ runtime: "automatic" })).toMatchInlineSnapshot(` |
| 114 |
Object { |
| 115 |
"development": false, |
| 116 |
"importSource": undefined, |
| 117 |
"pragma": undefined, |
| 118 |
"pragmaFrag": undefined, |
| 119 |
"pure": undefined, |
| 120 |
"runtime": "automatic", |
| 121 |
"throwIfNamespace": true, |
| 122 |
"useBuiltIns": undefined, |
| 123 |
"useSpread": undefined, |
| 124 |
} |
| 125 |
`); |
| 126 |
}); |
| 127 |
}); |
| 128 |
}); |