user17508887
user17508887

Reputation: 57

How to import file from general path in cypress?

I have a file called 'contents' in cypress. in my test file I want to import this file.

I do the following :

import contents from 'C:/Users/asus/cypress/support/contents';

But I want to make it more general path (not contain my own path). I tried to import it inside the index file in cypress.

As follow:

import './contents'

But it is not working by this way. Cypress will throw an error that contents is not defined.

Upvotes: 1

Views: 6463

Answers (2)

Krupal Vaghasiya
Krupal Vaghasiya

Reputation: 550

First, you have to export that file

const contents = {
    // Your data
}
export { contents };

You can import files like this way

import { contents } from '../support/contents.js'

If ../support/contents.js is not working Please use ../../ to get the root directory of your system like ../../support/contents.js. Please replace your file extension with .js.

Upvotes: 1

Sebastiano Schwarz
Sebastiano Schwarz

Reputation: 1146

Not knowing the data type and assuming that the exact location of the file doesn't matter, I would recommend moving the file to the /fixtures folder and importing it as recommended in the Cypress docs here. So you can use a relative path for the import in your test like:

import contents from '../fixtures/contents';

You can also find an example for importing a JSON file in this answer.

Upvotes: 2

Related Questions