database-transactions.e2e-spec.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { mergeConfig } from '@vendure/core';
  2. import { createTestEnvironment } from '@vendure/testing';
  3. import gql from 'graphql-tag';
  4. import path from 'path';
  5. import { initialData } from '../../../e2e-common/e2e-initial-data';
  6. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  7. import { TransactionTestPlugin, TRIGGER_EMAIL } from './fixtures/test-plugins/transaction-test-plugin';
  8. describe('Transaction infrastructure', () => {
  9. const { server, adminClient } = createTestEnvironment(
  10. mergeConfig(testConfig, {
  11. plugins: [TransactionTestPlugin],
  12. }),
  13. );
  14. beforeAll(async () => {
  15. await server.init({
  16. initialData,
  17. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  18. customerCount: 0,
  19. });
  20. await adminClient.asSuperAdmin();
  21. }, TEST_SETUP_TIMEOUT_MS);
  22. afterAll(async () => {
  23. await server.destroy();
  24. });
  25. it('non-failing mutation', async () => {
  26. const { createTestAdministrator } = await adminClient.query(CREATE_ADMIN, {
  27. emailAddress: 'test1',
  28. fail: false,
  29. });
  30. expect(createTestAdministrator.emailAddress).toBe('test1');
  31. expect(createTestAdministrator.user.identifier).toBe('test1');
  32. const { verify } = await adminClient.query(VERIFY_TEST);
  33. expect(verify.admins.length).toBe(2);
  34. expect(verify.users.length).toBe(2);
  35. expect(!!verify.admins.find((a: any) => a.emailAddress === 'test1')).toBe(true);
  36. expect(!!verify.users.find((u: any) => u.identifier === 'test1')).toBe(true);
  37. });
  38. it('failing mutation', async () => {
  39. try {
  40. await adminClient.query(CREATE_ADMIN, {
  41. emailAddress: 'test2',
  42. fail: true,
  43. });
  44. fail('Should have thrown');
  45. } catch (e) {
  46. expect(e.message).toContain('Failed!');
  47. }
  48. const { verify } = await adminClient.query(VERIFY_TEST);
  49. expect(verify.admins.length).toBe(2);
  50. expect(verify.users.length).toBe(2);
  51. expect(!!verify.admins.find((a: any) => a.emailAddress === 'test2')).toBe(false);
  52. expect(!!verify.users.find((u: any) => u.identifier === 'test2')).toBe(false);
  53. });
  54. it('failing manual mutation', async () => {
  55. try {
  56. await adminClient.query(CREATE_ADMIN2, {
  57. emailAddress: 'test3',
  58. fail: true,
  59. });
  60. fail('Should have thrown');
  61. } catch (e) {
  62. expect(e.message).toContain('Failed!');
  63. }
  64. const { verify } = await adminClient.query(VERIFY_TEST);
  65. expect(verify.admins.length).toBe(2);
  66. expect(verify.users.length).toBe(2);
  67. expect(!!verify.admins.find((a: any) => a.emailAddress === 'test3')).toBe(false);
  68. expect(!!verify.users.find((u: any) => u.identifier === 'test3')).toBe(false);
  69. });
  70. it('failing manual mutation without transaction', async () => {
  71. try {
  72. await adminClient.query(CREATE_ADMIN3, {
  73. emailAddress: 'test4',
  74. fail: true,
  75. });
  76. fail('Should have thrown');
  77. } catch (e) {
  78. expect(e.message).toContain('Failed!');
  79. }
  80. const { verify } = await adminClient.query(VERIFY_TEST);
  81. expect(verify.admins.length).toBe(2);
  82. expect(verify.users.length).toBe(3);
  83. expect(!!verify.admins.find((a: any) => a.emailAddress === 'test4')).toBe(false);
  84. expect(!!verify.users.find((u: any) => u.identifier === 'test4')).toBe(true);
  85. });
  86. // Testing https://github.com/vendure-ecommerce/vendure/issues/520
  87. it('passing transaction via EventBus', async () => {
  88. TransactionTestPlugin.errorHandler.mockClear();
  89. const { createTestAdministrator } = await adminClient.query(CREATE_ADMIN, {
  90. emailAddress: TRIGGER_EMAIL,
  91. fail: false,
  92. });
  93. await TransactionTestPlugin.eventHandlerComplete$.toPromise();
  94. expect(createTestAdministrator.emailAddress).toBe(TRIGGER_EMAIL);
  95. expect(TransactionTestPlugin.errorHandler).not.toHaveBeenCalled();
  96. });
  97. });
  98. const ADMIN_FRAGMENT = gql`
  99. fragment CreatedAdmin on Administrator {
  100. id
  101. emailAddress
  102. user {
  103. id
  104. identifier
  105. }
  106. }
  107. `;
  108. const CREATE_ADMIN = gql`
  109. mutation CreateTestAdmin($emailAddress: String!, $fail: Boolean!) {
  110. createTestAdministrator(emailAddress: $emailAddress, fail: $fail) {
  111. ...CreatedAdmin
  112. }
  113. }
  114. ${ADMIN_FRAGMENT}
  115. `;
  116. const CREATE_ADMIN2 = gql`
  117. mutation CreateTestAdmin2($emailAddress: String!, $fail: Boolean!) {
  118. createTestAdministrator2(emailAddress: $emailAddress, fail: $fail) {
  119. ...CreatedAdmin
  120. }
  121. }
  122. ${ADMIN_FRAGMENT}
  123. `;
  124. const CREATE_ADMIN3 = gql`
  125. mutation CreateTestAdmin2($emailAddress: String!, $fail: Boolean!) {
  126. createTestAdministrator3(emailAddress: $emailAddress, fail: $fail) {
  127. ...CreatedAdmin
  128. }
  129. }
  130. ${ADMIN_FRAGMENT}
  131. `;
  132. const VERIFY_TEST = gql`
  133. query VerifyTest {
  134. verify {
  135. admins {
  136. id
  137. emailAddress
  138. }
  139. users {
  140. id
  141. identifier
  142. }
  143. }
  144. }
  145. `;