| 1 |
import { loadOptions as loadOptionsOrig } from "../lib/index.js"; |
| 2 |
import { join, dirname } from "path"; |
| 3 |
import { fileURLToPath } from "url"; |
| 4 |
|
| 5 |
const cwd = dirname(fileURLToPath(import.meta.url)); |
| 6 |
|
| 7 |
function loadOptions(opts) { |
| 8 |
return loadOptionsOrig({ cwd, ...opts }); |
| 9 |
} |
| 10 |
|
| 11 |
function withTargets(targets) { |
| 12 |
return loadOptions({ targets }); |
| 13 |
} |
| 14 |
|
| 15 |
describe("targets", () => { |
| 16 |
it("throws if invalid type", () => { |
| 17 |
expect(() => withTargets(2)).toThrow( |
| 18 |
".targets must be a string, an array of strings or an object", |
| 19 |
); |
| 20 |
|
| 21 |
expect(() => withTargets([2])).toThrow( |
| 22 |
".targets must be a string, an array of strings or an object", |
| 23 |
); |
| 24 |
|
| 25 |
expect(() => withTargets([{}])).toThrow( |
| 26 |
".targets must be a string, an array of strings or an object", |
| 27 |
); |
| 28 |
|
| 29 |
expect(() => withTargets([])).not.toThrow(); |
| 30 |
expect(() => withTargets({})).not.toThrow(); |
| 31 |
}); |
| 32 |
|
| 33 |
it("throws if invalid target", () => { |
| 34 |
expect(() => withTargets({ uglify: "2.3" })).toThrow( |
| 35 |
/\.targets\["uglify"\] is not a valid target/, |
| 36 |
); |
| 37 |
|
| 38 |
expect(() => withTargets({ foo: "bar" })).toThrow( |
| 39 |
/\.targets\["foo"\] is not a valid target/, |
| 40 |
); |
| 41 |
|
| 42 |
expect(() => withTargets({ firefox: 71 })).not.toThrow(); |
| 43 |
}); |
| 44 |
|
| 45 |
it("throws if invalid version", () => { |
| 46 |
expect(() => withTargets({ node: 10.1 /* or 10.10? */ })).toThrow( |
| 47 |
`.targets["node"] must be a string or an integer number`, |
| 48 |
); |
| 49 |
|
| 50 |
expect(() => withTargets({ node: true })).toThrow( |
| 51 |
`.targets["node"] must be a string or an integer number`, |
| 52 |
); |
| 53 |
|
| 54 |
expect(() => withTargets({ node: "10.1" })).not.toThrow(); |
| 55 |
|
| 56 |
expect(() => withTargets({ node: "current" })).not.toThrow(); |
| 57 |
}); |
| 58 |
|
| 59 |
it("esmodules", () => { |
| 60 |
expect(() => withTargets({ esmodules: "7" })).toThrow( |
| 61 |
`.targets["esmodules"] must be a boolean, or undefined`, |
| 62 |
); |
| 63 |
|
| 64 |
expect(() => withTargets({ esmodules: false })).not.toThrow(); |
| 65 |
expect(() => withTargets({ esmodules: true })).not.toThrow(); |
| 66 |
}); |
| 67 |
|
| 68 |
it("browsers", () => { |
| 69 |
expect(() => withTargets({ browsers: 2 })).toThrow( |
| 70 |
`.targets["browsers"] must be undefined, a string or an array of strings`, |
| 71 |
); |
| 72 |
|
| 73 |
expect(() => withTargets({ browsers: [2] })).toThrow( |
| 74 |
`.targets["browsers"] must be undefined, a string or an array of strings`, |
| 75 |
); |
| 76 |
|
| 77 |
expect(() => withTargets({ browsers: {} })).toThrow( |
| 78 |
`.targets["browsers"] must be undefined, a string or an array of strings`, |
| 79 |
); |
| 80 |
|
| 81 |
expect(() => withTargets({ browsers: [] })).not.toThrow(); |
| 82 |
}); |
| 83 |
}); |
| 84 |
|
| 85 |
describe("browserslist", () => { |
| 86 |
it("loads .browserslistrc by default", () => { |
| 87 |
expect( |
| 88 |
loadOptions({ |
| 89 |
cwd: join(cwd, "fixtures", "targets"), |
| 90 |
}).targets, |
| 91 |
).toEqual({ chrome: "80.0.0" }); |
| 92 |
}); |
| 93 |
|
| 94 |
it("loads .browserslistrc relative to the root", () => { |
| 95 |
expect( |
| 96 |
loadOptions({ |
| 97 |
cwd: join(cwd, "fixtures", "targets"), |
| 98 |
filename: "./node_modules/dep/test.js", |
| 99 |
}).targets, |
| 100 |
).toEqual({ chrome: "80.0.0" }); |
| 101 |
}); |
| 102 |
|
| 103 |
describe("browserslistConfigFile", () => { |
| 104 |
it("can disable config loading", () => { |
| 105 |
expect( |
| 106 |
loadOptions({ |
| 107 |
cwd: join(cwd, "fixtures", "targets"), |
| 108 |
browserslistConfigFile: false, |
| 109 |
}).targets, |
| 110 |
).toEqual({}); |
| 111 |
}); |
| 112 |
|
| 113 |
it("can specify a custom file", () => { |
| 114 |
expect( |
| 115 |
loadOptions({ |
| 116 |
cwd: join(cwd, "fixtures", "targets"), |
| 117 |
browserslistConfigFile: "./.browserslistrc-firefox", |
| 118 |
}).targets, |
| 119 |
).toEqual({ firefox: "74.0.0" }); |
| 120 |
}); |
| 121 |
|
| 122 |
it("is relative to the cwd even if specifying 'root'", () => { |
| 123 |
expect( |
| 124 |
loadOptions({ |
| 125 |
cwd: join(cwd, "fixtures", "targets"), |
| 126 |
root: "..", |
| 127 |
filename: "./nested/test.js", |
| 128 |
browserslistConfigFile: "./.browserslistrc-firefox", |
| 129 |
}).targets, |
| 130 |
).toEqual({ firefox: "74.0.0" }); |
| 131 |
}); |
| 132 |
|
| 133 |
it("is relative to the config files that defines it", () => { |
| 134 |
expect( |
| 135 |
loadOptions({ |
| 136 |
cwd: join(cwd, "fixtures", "targets"), |
| 137 |
filename: "./node_modules/dep/test.js", |
| 138 |
babelrcRoots: ["./node_modules/dep/"], |
| 139 |
}).targets, |
| 140 |
).toEqual({ edge: "14.0.0" }); |
| 141 |
}); |
| 142 |
}); |
| 143 |
|
| 144 |
describe("browserslistEnv", () => { |
| 145 |
it("is forwarded to browserslist", () => { |
| 146 |
expect( |
| 147 |
loadOptions({ |
| 148 |
cwd: join(cwd, "fixtures", "targets"), |
| 149 |
browserslistEnv: "browserslist-loading-test", |
| 150 |
}).targets, |
| 151 |
).toEqual({ chrome: "70.0.0" }); |
| 152 |
}); |
| 153 |
}); |
| 154 |
|
| 155 |
it("esmodules and browsers are intersected", () => { |
| 156 |
expect( |
| 157 |
withTargets({ |
| 158 |
esmodules: true, |
| 159 |
browsers: "chrome >= 80, firefox >= 30", |
| 160 |
}).targets, |
| 161 |
).toEqual({ chrome: "80.0.0", firefox: "60.0.0" }); |
| 162 |
}); |
| 163 |
}); |