import.e2e-spec.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { createTestEnvironment } from '@vendure/testing';
  2. import gql from 'graphql-tag';
  3. import path from 'path';
  4. import { dataDir, TEST_SETUP_TIMEOUT_MS, testConfig } from './config/test-config';
  5. import { initialData } from './fixtures/e2e-initial-data';
  6. describe('Import resolver', () => {
  7. const { server, adminClient } = createTestEnvironment({
  8. ...testConfig,
  9. customFields: {
  10. Product: [{ type: 'string', name: 'pageType' }],
  11. ProductVariant: [{ type: 'int', name: 'weight' }],
  12. },
  13. });
  14. beforeAll(async () => {
  15. const token = await server.init({
  16. dataDir,
  17. initialData,
  18. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-empty.csv'),
  19. customerCount: 0,
  20. });
  21. await adminClient.init();
  22. await adminClient.asSuperAdmin();
  23. }, TEST_SETUP_TIMEOUT_MS);
  24. afterAll(async () => {
  25. await server.destroy();
  26. });
  27. it('imports products', async () => {
  28. // TODO: waste a few more hours actually fixing this for real
  29. // Forgive me this abomination of a work-around.
  30. // On the inital run (as in CI), when the sqlite db has just been populated,
  31. // this test will fail due to an "out of memory" exception originating from
  32. // SqljsQueryRunner.ts:79:22, which is part of the findOne() operation on the
  33. // Session repository called from the AuthService.validateSession() method.
  34. // After several hours of fruitless hunting, I did what any desperate JavaScript
  35. // developer would do, and threw in a setTimeout. Which of course "works"...
  36. const timeout = process.env.CI ? 2000 : 1000;
  37. await new Promise(resolve => {
  38. setTimeout(resolve, timeout);
  39. });
  40. const csvFile = path.join(__dirname, 'fixtures', 'product-import.csv');
  41. const result = await adminClient.fileUploadMutation({
  42. mutation: gql`
  43. mutation ImportProducts($csvFile: Upload!) {
  44. importProducts(csvFile: $csvFile) {
  45. imported
  46. processed
  47. errors
  48. }
  49. }
  50. `,
  51. filePaths: [csvFile],
  52. mapVariables: () => ({ csvFile: null }),
  53. });
  54. expect(result.importProducts.errors).toEqual([
  55. 'Invalid Record Length: header length is 16, got 1 on line 8',
  56. ]);
  57. expect(result.importProducts.imported).toBe(4);
  58. expect(result.importProducts.processed).toBe(4);
  59. const productResult = await adminClient.query(
  60. gql`
  61. query GetProducts($options: ProductListOptions) {
  62. products(options: $options) {
  63. totalItems
  64. items {
  65. id
  66. name
  67. slug
  68. description
  69. featuredAsset {
  70. id
  71. name
  72. preview
  73. source
  74. }
  75. assets {
  76. id
  77. name
  78. preview
  79. source
  80. }
  81. optionGroups {
  82. id
  83. code
  84. name
  85. }
  86. facetValues {
  87. id
  88. name
  89. facet {
  90. id
  91. name
  92. }
  93. }
  94. customFields {
  95. pageType
  96. }
  97. variants {
  98. id
  99. name
  100. sku
  101. price
  102. taxCategory {
  103. id
  104. name
  105. }
  106. options {
  107. id
  108. code
  109. }
  110. assets {
  111. id
  112. name
  113. preview
  114. source
  115. }
  116. featuredAsset {
  117. id
  118. name
  119. preview
  120. source
  121. }
  122. facetValues {
  123. id
  124. code
  125. name
  126. facet {
  127. id
  128. name
  129. }
  130. }
  131. stockOnHand
  132. trackInventory
  133. stockMovements {
  134. items {
  135. ... on StockMovement {
  136. id
  137. type
  138. quantity
  139. }
  140. }
  141. }
  142. customFields {
  143. weight
  144. }
  145. }
  146. }
  147. }
  148. }
  149. `,
  150. {
  151. options: {},
  152. },
  153. );
  154. expect(productResult.products.totalItems).toBe(4);
  155. expect(productResult.products.items).toMatchSnapshot();
  156. }, 20000);
  157. });