1
0

test-config.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { mergeConfig } from '@vendure/core';
  2. import {
  3. testConfig as defaultTestConfig,
  4. MysqlInitializer,
  5. PostgresInitializer,
  6. registerInitializer,
  7. SqljsInitializer,
  8. } from '@vendure/testing';
  9. import fs from 'fs-extra';
  10. import path from 'path';
  11. import { DataSourceOptions } 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. export const testConfig = () => {
  32. const index = getIndexOfTestFileInParentDir();
  33. return mergeConfig(defaultTestConfig, {
  34. apiOptions: {
  35. port: 3010 + index,
  36. },
  37. importExportOptions: {
  38. importAssetsDir: path.join(packageDir, 'fixtures/assets'),
  39. },
  40. dbConnectionOptions: getDbConfig(),
  41. });
  42. };
  43. /**
  44. * Returns the index of the test file in the parent directory.
  45. * This is used to ensure each test file has a unique
  46. * port number.
  47. */
  48. function getIndexOfTestFileInParentDir() {
  49. const testFilePath = getCallerFilename(2);
  50. const parentDir = path.dirname(testFilePath);
  51. const files = fs.readdirSync(parentDir);
  52. const index = files.indexOf(path.basename(testFilePath));
  53. return index;
  54. }
  55. /**
  56. * Returns the full path to the file which called the function.
  57. * @param depth
  58. */
  59. function getCallerFilename(depth: number): string {
  60. let stack: any;
  61. let file: any;
  62. let frame: any;
  63. const pst = Error.prepareStackTrace;
  64. Error.prepareStackTrace = (_, _stack) => {
  65. Error.prepareStackTrace = pst;
  66. return _stack;
  67. };
  68. stack = new Error().stack;
  69. stack = stack.slice(depth + 1);
  70. do {
  71. frame = stack.shift();
  72. file = frame && frame.getFileName();
  73. } while (stack.length && file === 'module.js');
  74. return file;
  75. }
  76. function getDbConfig(): DataSourceOptions {
  77. const dbType = process.env.DB || 'sqljs';
  78. switch (dbType) {
  79. case 'postgres':
  80. return {
  81. synchronize: true,
  82. type: 'postgres',
  83. host: '127.0.0.1',
  84. port: process.env.CI ? +(process.env.E2E_POSTGRES_PORT || 5432) : 5432,
  85. username: 'vendure',
  86. password: 'password',
  87. };
  88. case 'mariadb':
  89. return {
  90. synchronize: true,
  91. type: 'mariadb',
  92. host: '127.0.0.1',
  93. port: process.env.CI ? +(process.env.E2E_MARIADB_PORT || 3306) : 3306,
  94. username: 'root',
  95. password: 'password',
  96. };
  97. case 'mysql':
  98. return {
  99. synchronize: true,
  100. type: 'mysql',
  101. host: '127.0.0.1',
  102. port: process.env.CI ? +(process.env.E2E_MYSQL_PORT || 3306) : 3306,
  103. username: 'root',
  104. password: 'password',
  105. };
  106. case 'sqljs':
  107. default:
  108. return defaultTestConfig.dbConnectionOptions;
  109. }
  110. }