diff --git a/.changeset/large-flies-check.md b/.changeset/large-flies-check.md new file mode 100644 index 0000000000..634373ee13 --- /dev/null +++ b/.changeset/large-flies-check.md @@ -0,0 +1,5 @@ +--- +"jspsych": patch +--- + +Add informative error message when a trial is missing the `type` parameter diff --git a/packages/jspsych/src/timeline/Trial.spec.ts b/packages/jspsych/src/timeline/Trial.spec.ts index 18d032eadc..e26e02c68c 100644 --- a/packages/jspsych/src/timeline/Trial.spec.ts +++ b/packages/jspsych/src/timeline/Trial.spec.ts @@ -36,6 +36,16 @@ describe("Trial", () => { return trial; }; + it("throws an error upon construction when the `type` parameter or plugin info object is undefined", () => { + for (const description of [{}, { type: {} }] as TrialDescription[]) { + expect( + () => new Trial(dependencies, description, timeline) + ).toThrowErrorMatchingInlineSnapshot( + "\"Plugin not recognized. Please provide a valid plugin using the 'type' parameter.\"" + ); + } + }); + describe("run()", () => { it("instantiates the corresponding plugin", async () => { const trial = createTrial({ type: TestPlugin }); diff --git a/packages/jspsych/src/timeline/Trial.ts b/packages/jspsych/src/timeline/Trial.ts index bfc300ff47..b2575240d6 100644 --- a/packages/jspsych/src/timeline/Trial.ts +++ b/packages/jspsych/src/timeline/Trial.ts @@ -35,7 +35,12 @@ export class Trial extends TimelineNode { this.trialObject = deepCopy(description); this.pluginClass = this.getParameterValue("type", { evaluateFunctions: false }); - this.pluginInfo = this.pluginClass["info"]; + this.pluginInfo = this.pluginClass?.["info"]; + if (!this.pluginInfo) { + throw new Error( + "Plugin not recognized. Please provide a valid plugin using the 'type' parameter." + ); + } if (!("version" in this.pluginInfo) && !("data" in this.pluginInfo)) { console.warn(