Skip to content

Commit

Permalink
Merge pull request #3477 from rkilpadi/main
Browse files Browse the repository at this point in the history
Improve error message for missing type parameter
  • Loading branch information
jodeleeuw authored Jan 7, 2025
2 parents a3db2a0 + ff5f67b commit 9d8597f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/large-flies-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"jspsych": patch
---

Add informative error message when a trial is missing the `type` parameter
10 changes: 10 additions & 0 deletions packages/jspsych/src/timeline/Trial.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
7 changes: 6 additions & 1 deletion packages/jspsych/src/timeline/Trial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 9d8597f

Please sign in to comment.