Files
medusa-store/integration-tests/helpers/use-template-db.js
Patrick 86f9455d00 test: allow integration-tests to leverage workspaces optimizations (#2727)
### What
Leverage yarn workspaces and Turborepo for integration-tests in order to accelerate development and reduce DevX complexity.

### Why
The current solution for running integration tests requires using `medusa-dev-cli` in order to publish packages to a local npm repository. The package where the command is executed will have its package.json altered for any known medusa dependency in order to install from the local npm. The process is taxing on the host machine resources and prevents rapid iterations when working with integration tests.

For more information, see documentation: f0cc1b324c/docs/content/usage/local-development.md (run-api-integration-tests)

### How
By declaring `integeration-tests/**/*` as a workspace, Turborepo can now be leveraged to build and run integration test as if there were packages. The build process will take care of interdependency between package in order to ensure local dependency are met.

In addition, within each integration-tests "packages", we can declare local dependencies as "*" which will translate to using the one that is part of the current build, regardless of the dependency's version number. No more fiddling with version numbers.

Github actions pertaining to integration-tests have been streamlined to use the new behavior.

The integration-tests packages have been marked as `private:true` in order to avoid publishing them to the public npm registry.

### Testing
```
cd root-of-medusajs-medusa-repo/
yarn install
yarn build
yarn test:integration:api
yarn test:integration:plugins
```

After a code change, `yarn build` must be run before re-running an integration test, which is the same procedure as for unit tests.

Resolves: CORE-845
2022-12-08 14:57:16 +00:00

113 lines
2.8 KiB
JavaScript

const path = require("path")
require("dotenv").config({ path: path.join(__dirname, "../.env.test") })
const { createDatabase, dropDatabase } = require("pg-god")
const { createConnection, getConnection } = require("typeorm")
const DB_HOST = process.env.DB_HOST
const DB_USERNAME = process.env.DB_USERNAME
const DB_PASSWORD = process.env.DB_PASSWORD
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}`
const pgGodCredentials = {
user: DB_USERNAME,
password: DB_PASSWORD,
host: DB_HOST,
}
class DatabaseFactory {
constructor() {
this.connection_ = null
this.masterConnectionName = "master"
this.templateDbName = "medusa-integration-template"
}
async createTemplateDb_({ cwd }) {
// const cwd = path.resolve(path.join(__dirname, ".."))
const connection = await this.getMasterConnection()
const migrationDir = path.resolve(
path.join(
__dirname,
`../../`,
`node_modules`,
`@medusajs`,
`medusa`,
`dist`,
`migrations`,
`*.js`
)
)
const {
getEnabledMigrations,
} = require("@medusajs/medusa/dist/commands/utils/get-migrations")
// filter migrations to only include those that don't have feature flags
const enabledMigrations = await getEnabledMigrations(
[migrationDir],
(flag) => false
)
await dropDatabase(
{
databaseName: this.templateDbName,
errorIfNonExist: false,
},
pgGodCredentials
)
await createDatabase(
{ databaseName: this.templateDbName },
pgGodCredentials
)
const templateDbConnection = await createConnection({
type: "postgres",
name: "templateConnection",
url: `${DB_URL}/${this.templateDbName}`,
migrations: enabledMigrations,
})
await templateDbConnection.runMigrations()
await templateDbConnection.close()
return connection
}
async getMasterConnection() {
try {
return await getConnection(this.masterConnectionName)
} catch (err) {
return await this.createMasterConnection()
}
}
async createMasterConnection() {
const connection = await createConnection({
type: "postgres",
name: this.masterConnectionName,
url: `${DB_URL}`,
})
return connection
}
async createFromTemplate(dbName) {
const connection = await this.getMasterConnection()
await connection.query(`DROP DATABASE IF EXISTS "${dbName}";`)
await connection.query(
`CREATE DATABASE "${dbName}" TEMPLATE "${this.templateDbName}";`
)
}
async destroy() {
const connection = await this.getMasterConnection()
await connection.query(`DROP DATABASE IF EXISTS "${this.templateDbName}";`)
await connection.close()
}
}
module.exports = new DatabaseFactory()