test-config.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { mergeConfig } from '@vendure/core';
  2. import {
  3. MysqlInitializer,
  4. PostgresInitializer,
  5. registerInitializer,
  6. SqljsInitializer,
  7. testConfig as defaultTestConfig,
  8. } from '@vendure/testing';
  9. import fs from 'fs-extra';
  10. import path from 'path';
  11. import { ConnectionOptions } from 'typeorm';
  12. import { getPackageDir } from './get-package-dir';
  13. declare global {
  14. namespace NodeJS {
  15. interface Global {
  16. e2eServerPortsUsed: number[];
  17. }
  18. }
  19. }
  20. /**
  21. * We use a relatively long timeout on the initial beforeAll() function of the
  22. * e2e tests because on the first run (and always in CI) the sqlite databases
  23. * need to be generated, which can take a while.
  24. */
  25. export const TEST_SETUP_TIMEOUT_MS = process.env.E2E_DEBUG ? 1800 * 1000 : 120000;
  26. const packageDir = getPackageDir();
  27. registerInitializer('sqljs', new SqljsInitializer(path.join(packageDir, '__data__')));
  28. registerInitializer('postgres', new PostgresInitializer());
  29. registerInitializer('mysql', new MysqlInitializer());
  30. registerInitializer('mariadb', new MysqlInitializer());
  31. /**
  32. * For local debugging of the e2e tests, we set a very long timeout value otherwise tests will
  33. * automatically fail for going over the 5 second default timeout.
  34. */
  35. if (process.env.E2E_DEBUG) {
  36. // tslint:disable-next-line:no-console
  37. console.log('E2E_DEBUG', process.env.E2E_DEBUG, ' - setting long timeout');
  38. jest.setTimeout(1800 * 1000);
  39. }
  40. /**
  41. * Increase default timeout in CI because occasionally valid tests fail due to
  42. * timeouts.
  43. */
  44. if (process.env.CI) {
  45. jest.setTimeout(30 * 1000);
  46. } else {
  47. jest.setTimeout(15 * 1000);
  48. }
  49. export const testConfig = () => {
  50. const portsFile = path.join(__dirname, 'ports.json');
  51. fs.ensureFileSync(portsFile);
  52. let usedPorts: number[];
  53. try {
  54. usedPorts = fs.readJSONSync(portsFile) ?? [3010];
  55. } catch (e) {
  56. usedPorts = [3010];
  57. }
  58. const nextPort = Math.max(...usedPorts) + 1;
  59. usedPorts.push(nextPort);
  60. if (100 < usedPorts.length) {
  61. // reset the used ports after it gets 100 entries long
  62. usedPorts = [3010];
  63. }
  64. fs.writeJSONSync(portsFile, usedPorts);
  65. return mergeConfig(defaultTestConfig, {
  66. apiOptions: {
  67. port: nextPort,
  68. },
  69. importExportOptions: {
  70. importAssetsDir: path.join(packageDir, 'fixtures/assets'),
  71. },
  72. dbConnectionOptions: getDbConfig(),
  73. });
  74. };
  75. function getDbConfig(): ConnectionOptions {
  76. const dbType = process.env.DB || 'sqljs';
  77. switch (dbType) {
  78. case 'postgres':
  79. return {
  80. synchronize: true,
  81. type: 'postgres',
  82. host: '127.0.0.1',
  83. port: process.env.CI ? +(process.env.E2E_POSTGRES_PORT || 5432) : 5432,
  84. username: 'admin',
  85. password: 'secret',
  86. };
  87. case 'mariadb':
  88. return {
  89. synchronize: true,
  90. type: 'mariadb',
  91. host: '127.0.0.1',
  92. port: process.env.CI ? +(process.env.E2E_MARIADB_PORT || 3306) : 3306,
  93. username: 'root',
  94. password: '',
  95. };
  96. case 'mysql':
  97. return {
  98. synchronize: true,
  99. type: 'mysql',
  100. host: '127.0.0.1',
  101. port: process.env.CI ? +(process.env.E2E_MYSQL_PORT || 3306) : 3306,
  102. username: 'root',
  103. password: '',
  104. };
  105. case 'sqljs':
  106. default:
  107. return defaultTestConfig.dbConnectionOptions;
  108. }
  109. }