Home > Back-end >  How to follow ES6 way of receiving constants into test file
How to follow ES6 way of receiving constants into test file

Time:01-03

Currently I am following ES5 way of receiving constants into my spec file. I would like to change that into ES6 style. could someone please suggest the best way to do that ?

// constants.js

 module.exports = Object.freeze({
      firstNameInput: "Cypress",
      lastNameInput: "Test",
      ms: 2000,
      tableId: 1,
      userEmail: "[email protected]",
      displayName: "Test Account",
    });

// test.spec.js file

let constants = require("../../support/constants");

const tableId = constants.tableId;
const userEmail = constants.userEmail;
const displayName = constants.displayName;

CodePudding user response:

Nothing more required than

import constants from "../../support/constants"

Don't need to change the constants file, neither the way to refer to the variables.

No need to import * as constants ..., the above already does same thing.

CodePudding user response:

Update the constants.js file to use the export syntax instead of module.exports, like this:

export const firstNameInput = "Cypress";
export const lastNameInput = "Test";
...

Then in your test.spec.js file you could import the constants like this:

import { firstNameInput, lastNameInput, ... } from "../../support/constants";

  • Related