Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] How to load testing data based on the environment? #7

Open
mareru opened this issue Sep 4, 2024 · 0 comments
Open

[QUESTION] How to load testing data based on the environment? #7

mareru opened this issue Sep 4, 2024 · 0 comments
Assignees
Labels
help wanted Extra attention is needed pending question Further information is requested

Comments

@mareru
Copy link

mareru commented Sep 4, 2024

🐞 Describe the question:
I am wondering how to do the test data mapping based on the environment? For example, in your spec book-with-fixture-and-api-isolated-auth.spec.ts I would like to have userData in a folder data/qa/user-data and also data/staging/user-data and load the test data for users based on the environment.

I have something like this:

function getDataFilePath(fileName: string): string {
  const env = process.env.testEnvironment || 'uat';
  return `data/${env}/${fileName}.ts`;
}

async function importDataFile<T = object>(filePath: string): Promise<T> {
  const response = await import(`../${filePath}`);
  return response.default as T;
}
export type TestData = {
  loginData: any;
  categoryData: any;
  paymentData: any;
  messagesData: any;
  shipmentData: any;
  urlPartData: any;
  productData: any;
};

export const getTestData = async function (): Promise<TestData> {
  const loginFilePath = getDataFilePath('login');
  const categoryFilePath = getDataFilePath('category');
  const paymentFilePath = getDataFilePath('payment');
  const messagesFilePath = getDataFilePath('messages');
  const shipmentFilePath = getDataFilePath('shipment');
  const urlPartDataFilePath = getDataFilePath('url-part');
  const productDataFilePath = getDataFilePath('product');
  return {
    loginData: await importDataFile(loginFilePath),
    categoryData: await importDataFile(categoryFilePath),
    paymentData: await importDataFile(paymentFilePath),
    messagesData: await importDataFile(messagesFilePath),
    shipmentData: await importDataFile(shipmentFilePath),
    urlPartData: await importDataFile(urlPartDataFilePath),
    productData: await importDataFile(productDataFilePath),
  };
};

and then in the test I just use testData.loginData.username or whatever I have in those data. Different envs can have different data.
But this way I am loosing the intellisens that you have in your example with userData.
Do you have recommendations on how to achieve/do this better?

@mareru mareru added help wanted Extra attention is needed pending question Further information is requested labels Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed pending question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants