Skip to content

Commit

Permalink
feat(testing): Turn productsCsvPath into an optional property for tes…
Browse files Browse the repository at this point in the history
…t server initialization (#2038)
  • Loading branch information
vrosa authored Feb 14, 2023
1 parent ab806b2 commit 4c2b118
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/content/developer-guide/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ afterAll(async () => {

An explanation of the options:

* `productsCsvPath` This is a path to a CSV file containing product data. See [Product Import Format]({{< relref "importing-product-data" >}}#product-import-format). You can see [an example used in the Vendure e2e tests](https://github.com/vendure-ecommerce/vendure/blob/master/packages/core/e2e/fixtures/e2e-products-full.csv) to get an idea of how it works. To start with you can just copy this file directly and use it as-is.
* `productsCsvPath` This is a path to an optional CSV file containing product data. See [Product Import Format]({{< relref "importing-product-data" >}}#product-import-format). You can see [an example used in the Vendure e2e tests](https://github.com/vendure-ecommerce/vendure/blob/master/packages/core/e2e/fixtures/e2e-products-full.csv) to get an idea of how it works. To start with you can just copy this file directly and use it as-is.
* `initialData` This is an object which defines how other non-product data (Collections, ShippingMethods, Countries etc.) is populated. See [Initial Data Format]({{< relref "importing-product-data" >}}#initial-data). You can [copy this example from the Vendure e2e tests](https://github.com/vendure-ecommerce/vendure/blob/master/e2e-common/e2e-initial-data.ts)
* `customerCount` Specifies the number of fake Customers to create. Defaults to 10 if not specified.

Expand Down
15 changes: 13 additions & 2 deletions packages/testing/src/data-population/populate-for-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,22 @@ export async function populateForTesting<T extends INestApplicationContext>(
return app;
}

async function populateProducts(app: INestApplicationContext, productsCsvPath: string, logging: boolean) {
async function populateProducts(
app: INestApplicationContext,
productsCsvPath: string | undefined,
logging: boolean,
) {
if (!productsCsvPath) {
if (logging) {
console.log(`\nNo product data provided, skipping product import`);
}
return;
}

const importResult = await importProductsFromCsv(app, productsCsvPath, LanguageCode.en);
if (importResult.errors && importResult.errors.length) {
console.log(`${importResult.errors.length} errors encountered when importing product data:`);
await console.log(importResult.errors.join('\n'));
console.log(importResult.errors.join('\n'));
}

if (logging) {
Expand Down
4 changes: 2 additions & 2 deletions packages/testing/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export type Mutable<T> = { -readonly [K in keyof T]: T[K] };
export interface TestServerOptions {
/**
* @description
* The path to a CSV file containing product data to import.
* The path to an optional CSV file containing product data to import.
*/
productsCsvPath: string;
productsCsvPath?: string;
/**
* @description
* An object containing non-product data which is used to populate the database.
Expand Down

0 comments on commit 4c2b118

Please sign in to comment.