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:
- I've used Visual Studio Code's gui to obtain folder location.
- My tsconfig.json in the cypress folder does not have a
baseUrl
key/value pair - My tsconfig.json in the root folder does have a
baseUrl: "./src"
, but vs code shows an errors whensubjects
is referenced likefrom "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"
];