Home > Blockchain >  Import Custom Functions Into Cypress Tests Error
Import Custom Functions Into Cypress Tests Error

Time:01-29

Problem: When importing custom functions into Cypress, I believe it cannot find the module subjects.

Subjects Array:

const subjects = [
  "Accounting",
  "Art",
  "Biology",
  "Business"
];
export default subjects;

Code Example:

import { subjects } from "../../../../src/data/subjects.js";
const subject = subjects[Math.floor(Math.random() * subjects.length)];
console.log(subject);

Error: Cannot read properties of undefined (reading 'length')

Note:

  1. I've used Visual Studio Code's gui to obtain folder location.
  2. My tsconfig.json in the cypress folder does not have a baseUrl key/value pair
  3. My tsconfig.json in the root folder does have a baseUrl: "./src", but vs code shows an errors when subjects is referenced like from "src/data/subjects.js"

CodePudding user response:

The exporting side of things uses the default export, so the import would be without the brackets.

import subjects from "../../../../src/data/subjects.js";

or change the export to be a named export

export const subjects = [
  "Accounting",
  "Art",
  "Biology",
  "Business"
];
  • Related