Browse Source

fix(server): Fix failing e2e test with ugly hack

Michael Bromley 7 years ago
parent
commit
09e89f2dc2

+ 49 - 30
server/e2e/import.e2e-spec.ts

@@ -22,54 +22,73 @@ describe('Import resolver', () => {
     });
 
     it('imports products', async () => {
+        // TODO: waste a few more hours actually fixing this for real
+        // Forgive me this abomination of a work-around.
+        // On the inital run (as in CI), when the sqlite db has just been populated,
+        // this test will fail due to an "out of memory" exception originating from
+        // SqljsQueryRunner.ts:79:22, which is part of the findOne() operation on the
+        // Session repository called from the AuthService.validateSession() method.
+        // After several hours of fruitless hunting, I did what any desperate JavaScript
+        // developer would do, and threw in a setTimeout. Which of course "works"...
+        const timeout = process.env.CI ? 2000 : 500;
+        await new Promise(resolve => {
+            setTimeout(resolve, timeout);
+        });
+
         const csvFile = path.join(__dirname, 'fixtures', 'product-import.csv');
         const result = await client.importProducts(csvFile);
 
         expect(result.importProducts.errors).toEqual([]);
         expect(result.importProducts.importedCount).toBe(4);
 
-        const productResult = await client.query(gql`
-            query {
-                products {
-                    totalItems
-                    items {
-                        id
-                        name
-                        slug
-                        description
-                        featuredAsset {
-                            id
-                            name
-                        }
-                        assets {
+        const productResult = await client.query(
+            gql`
+                query GetProducts($options: ProductListOptions) {
+                    products(options: $options) {
+                        totalItems
+                        items {
                             id
                             name
-                        }
-                        optionGroups {
-                            id
-                            code
-                            name
-                        }
-                        variants {
-                            id
-                            name
-                            sku
-                            price
-                            taxCategory {
+                            slug
+                            description
+                            featuredAsset {
                                 id
                                 name
                             }
-                            options {
+                            assets {
+                                id
+                                name
+                            }
+                            optionGroups {
                                 id
                                 code
+                                name
+                            }
+                            variants {
+                                id
+                                name
+                                sku
+                                price
+                                taxCategory {
+                                    id
+                                    name
+                                }
+                                options {
+                                    id
+                                    code
+                                }
                             }
                         }
                     }
                 }
-            }
-        `);
+            `,
+            {
+                options: {},
+            },
+        );
 
         expect(productResult.products.totalItems).toBe(4);
         expect(productResult.products.items).toMatchSnapshot();
-    });
+        return;
+    }, 10000);
 });

+ 7 - 1
server/mock-data/simple-graphql-client.ts

@@ -127,7 +127,13 @@ export class SimpleGraphQLClient {
             curl.perform();
             curl.on('end', (statusCode, body) => {
                 curl.close();
-                resolve(JSON.parse(body).data);
+                const response = JSON.parse(body);
+                if (response.errors && response.errors.length) {
+                    const error = response.errors[0];
+                    console.log(JSON.stringify(error.extensions, null, 2));
+                    throw new Error(error.message);
+                }
+                resolve(response.data);
             });
 
             curl.on('error', err => {

+ 12 - 5
server/src/api/resolvers/import.resolver.ts

@@ -32,11 +32,18 @@ export class ImportResolver {
         }
 
         if (parsed) {
-            const result = await this.importer.importProducts(ctx, parsed);
-            return {
-                errors: [],
-                importedCount: parsed.length,
-            };
+            try {
+                const result = await this.importer.importProducts(ctx, parsed);
+                return {
+                    errors: [],
+                    importedCount: parsed.length,
+                };
+            } catch (err) {
+                return {
+                    errors: [err.message],
+                    importedCount: 0,
+                };
+            }
         } else {
             return {
                 errors: ['nothing to parse!'],