Commit 62bbbe50 authored by David Sveningsson's avatar David Sveningsson

fix(config): more helpful error when user forgot to load plugin

parent 89bdd89e
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`config transformSource() should throw error when trying to load garbage as transformer 1`] = `"Failed to load transformer \\"mock-garbage\\": Module is not a valid transformer."`;
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 helpful error when trying to load unregistered plugin as transformer 1`] = `"Failed to load transformer \\"mock-plugin-unregistered\\": Module is not a valid transformer. This looks like a plugin, did you forget to load the plugin first?"`;
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\\""`;
......@@ -643,6 +643,28 @@ describe("config", () => {
});
expect(() => config.init()).toThrowErrorMatchingSnapshot();
});
it("should throw error when trying to load garbage as transformer", () => {
jest.mock("mock-garbage", () => "foobar", { virtual: true });
const config = Config.fromObject({
transform: {
"^.*\\.foo$": "mock-garbage",
},
});
expect(() => config.init()).toThrowErrorMatchingSnapshot();
});
it("should throw helpful error when trying to load unregistered plugin as transformer", () => {
jest.mock("mock-plugin-unregistered", () => ({ transformer: {} }), {
virtual: true,
});
const config = Config.fromObject({
transform: {
"^.*\\.foo$": "mock-plugin-unregistered",
},
});
expect(() => config.init()).toThrowErrorMatchingSnapshot();
});
});
describe("transformFilename()", () => {
......
......@@ -529,7 +529,18 @@ export class Config {
/* assume transformer refers to a regular module */
// eslint-disable-next-line security/detect-non-literal-require
return require(name.replace("<rootDir>", this.rootDir));
const fn = require(name.replace("<rootDir>", this.rootDir));
if (typeof fn !== "function") {
/* this is not a proper transformer, is it a plugin exposing a transformer? */
if (fn.transformer) {
throw new ConfigError(
`Module is not a valid transformer. This looks like a plugin, did you forget to load the plugin first?`
);
} else {
throw new ConfigError(`Module is not a valid transformer.`);
}
}
return fn;
}
protected findRootDir(): string {
......
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