Commit db48a015 authored by David Sveningsson's avatar David Sveningsson

fix(config): better error when loading missing transformer from plugin

parent c9ad0808
Pipeline #97109614 passed with stages
in 9 minutes and 4 seconds
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`config transformSource() should throw error when trying to load named transform from plugin without any 1`] = `"Failed to load transformer \\"mock-plugin-notransform:named\\": Plugin does not expose any transformer"`;
exports[`config transformSource() should throw error when trying to load unnamed transform from plugin without any 1`] = `"Failed to load transformer \\"mock-plugin-notransform\\": Plugin does not expose any transformer"`;
exports[`config transformSource() should throw sane error when transformer fails 1`] = `"When transforming \\"/path/to/test.foo\\": Failed to frobnicate a baz"`;
exports[`config transformSource() should throw sane error when transformer fails to load 1`] = `"Failed to load transformer \\"missing-transformer\\""`;
......@@ -529,6 +529,28 @@ describe("config", () => {
});
expect(() => config.init()).toThrowErrorMatchingSnapshot();
});
it("should throw error when trying to load unnamed transform from plugin without any", () => {
jest.mock("mock-plugin-notransform", () => ({}), { virtual: true });
const config = Config.fromObject({
plugins: ["mock-plugin-notransform"],
transform: {
"^.*\\.foo$": "mock-plugin-notransform",
},
});
expect(() => config.init()).toThrowErrorMatchingSnapshot();
});
it("should throw error when trying to load named transform from plugin without any", () => {
jest.mock("mock-plugin-notransform", () => ({}), { virtual: true });
const config = Config.fromObject({
plugins: ["mock-plugin-notransform"],
transform: {
"^.*\\.foo$": "mock-plugin-notransform:named",
},
});
expect(() => config.init()).toThrowErrorMatchingSnapshot();
});
});
describe("transformFilename()", () => {
......
......@@ -425,6 +425,9 @@ export class Config {
if (match) {
const [, pluginName, key] = match;
const plugin = this.plugins.find(cur => cur.name === pluginName);
if (!plugin.transformer) {
throw new ConfigError(`Plugin does not expose any transformer`);
}
if (typeof plugin.transformer === "function") {
throw new ConfigError(
`Transformer "${name}" refers to named transformer but plugin exposes only unnamed, use "${pluginName}" instead.`
......@@ -441,6 +444,9 @@ export class Config {
/* try to match an unnamed transformer from plugin */
const plugin = this.plugins.find(cur => cur.name === name);
if (plugin) {
if (!plugin.transformer) {
throw new ConfigError(`Plugin does not expose any transformer`);
}
if (typeof plugin.transformer !== "function") {
throw new ConfigError(
`Transformer "${name}" refers to unnamed transformer but plugin exposes only named.`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment