Skip to content

Commit

Permalink
Optional filename when preset uses fn test/include/exclude (#14954)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 21, 2022
1 parent 53e9057 commit 0cc190c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/babel-core/src/config/full.ts
Expand Up @@ -406,11 +406,17 @@ const instantiatePlugin = makeWeakCache(function* (
return new Plugin(plugin, options, alias, externalDependencies);
});

const needsFilename = (val: unknown) => val && typeof val !== "function";

const validateIfOptionNeedsFilename = (
options: ValidatedOptions,
descriptor: UnloadedDescriptor,
): void => {
if (options.test || options.include || options.exclude) {
if (
needsFilename(options.test) ||
needsFilename(options.include) ||
needsFilename(options.exclude)
) {
const formattedPresetName = descriptor.name
? `"${descriptor.name}"`
: "/* your preset */";
Expand Down
90 changes: 90 additions & 0 deletions packages/babel-core/test/config-chain.js
Expand Up @@ -236,6 +236,96 @@ describe("buildConfigChain", function () {
expect(opts.comments).toBeUndefined();
});
});

describe("filename requirement", () => {
const BASE_OPTS = {
cwd: fixture("nonexistant-fake"),
babelrc: false,
configFile: false,
};

describe("in config", () => {
it("requires filename if string", () => {
expect(() =>
loadOptions({
...BASE_OPTS,
test: fixture("nonexistant-fake"),
}),
).toThrow(/no filename was passed/);
});

it("requires filename if RegExp", () => {
expect(() =>
loadOptions({
...BASE_OPTS,
test: /file/,
}),
).toThrow(/no filename was passed/);
});

it("does not require filename if function", () => {
const mock = jest.fn().mockReturnValue(true);

expect(() =>
loadOptions({
...BASE_OPTS,
test: mock,
}),
).not.toThrow();
expect(mock).toHaveBeenCalledWith(undefined, expect.anything());

expect(() =>
loadOptions({
...BASE_OPTS,
filename: "some-filename",
test: mock,
}),
).not.toThrow();
expect(mock.mock.calls[1][0].endsWith("some-filename")).toBe(true);
});
});

describe("in preset", () => {
it("requires filename if string", () => {
expect(() =>
loadOptions({
...BASE_OPTS,
presets: [() => ({ test: fixture("nonexistant-fake") })],
}),
).toThrow(/requires a filename/);
});

it("requires filename if RegExp", () => {
expect(() =>
loadOptions({
...BASE_OPTS,
presets: [() => ({ test: /file/ })],
}),
).toThrow(/requires a filename/);
});

it("does not require filename if function", () => {
const mock = jest.fn().mockReturnValue(true);

expect(() =>
loadOptions({
...BASE_OPTS,
presets: [() => ({ test: mock })],
}),
).not.toThrow();
expect(mock).toHaveBeenCalledWith(undefined, expect.anything());

expect(() =>
loadOptions({
...BASE_OPTS,
filename: "some-filename",
presets: [() => ({ test: mock })],
}),
).not.toThrow();
expect(mock.mock.calls[1][0].endsWith("some-filename")).toBe(true);
});
});
});
});

describe("include", () => {
Expand Down

0 comments on commit 0cc190c

Please sign in to comment.