Commit 3aec3f3f authored by David Sveningsson's avatar David Sveningsson

fix(config): handle exceptions from loading plugin

fixes #55
parent a9d9c8ee
Pipeline #101039575 passed with stages
in 9 minutes and 19 seconds
......@@ -312,13 +312,20 @@ export class Config {
private loadPlugins(plugins: string[]): Plugin[] {
return plugins.map((moduleName: string) => {
// eslint-disable-next-line security/detect-non-literal-require
const plugin = require(moduleName.replace(
"<rootDir>",
this.rootDir
)) as Plugin;
plugin.name = plugin.name || moduleName;
return plugin;
try {
// eslint-disable-next-line security/detect-non-literal-require
const plugin = require(moduleName.replace(
"<rootDir>",
this.rootDir
)) as Plugin;
plugin.name = plugin.name || moduleName;
return plugin;
} catch (err) {
throw new ConfigError(
`Failed to load plugin "${moduleName}": ${err}`,
err
);
}
});
}
......
import { Config } from "../config";
import { ConfigError } from "../config/error";
import { Source } from "../context";
import { Engine } from "../engine";
import { EventHandler } from "../event";
......@@ -28,6 +29,15 @@ describe("Plugin", () => {
mockPlugin = require("mock-plugin");
});
it("should throw ConfigError when loading plugin fails", () => {
expect.assertions(2);
const loadConfig = (): void => {
Config.fromObject({ plugins: ["missing-plugin"] });
};
expect(loadConfig).toThrow(ConfigError);
expect(loadConfig).toThrow(/Failed to load plugin "missing-plugin":/);
});
describe("name", () => {
it("should use plugin name if set", () => {
expect.assertions(1);
......
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