Skip to content

Commit

Permalink
fix(integration): setup (#5511)
Browse files Browse the repository at this point in the history
* fix(integration): setup
  • Loading branch information
adrien2p authored Nov 1, 2023
1 parent 4692f54 commit 80fe362
Show file tree
Hide file tree
Showing 45 changed files with 459 additions and 403 deletions.
6 changes: 6 additions & 0 deletions .changeset/tough-cups-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/cache-redis": major
"@medusajs/cache-inmemory": patch
---

Integration tests fixes and ignore ttl 0 on cache modules
2 changes: 1 addition & 1 deletion integration-tests/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"private": true,
"scripts": {
"test:integration": "jest --silent=false --maxWorkers=50% --bail --detectOpenHandles --forceExit",
"test:integration": "jest --silent=false --maxWorkers=50% --bail --detectOpenHandles --forceExit --logHeapUsage",
"build": "babel src -d dist --extensions \".ts,.js\""
},
"dependencies": {
Expand Down
82 changes: 65 additions & 17 deletions integration-tests/environment-helpers/bootstrap-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,78 @@ const path = require("path")
const express = require("express")
const getPort = require("get-port")
const { isObject } = require("@medusajs/utils")
const { setContainer } = require("./use-container")
const { setPort, setExpressServer } = require("./use-api")

module.exports = {
bootstrapApp: async ({ cwd, env = {} } = {}) => {
const app = express()
async function bootstrapApp({ cwd, env = {} } = {}) {
const app = express()

if (isObject(env)) {
Object.entries(env).forEach(([k, v]) => (process.env[k] = v))
}
if (isObject(env)) {
Object.entries(env).forEach(([k, v]) => (process.env[k] = v))
}

const loaders = require("@medusajs/medusa/dist/loaders").default

const { container, dbConnection, pgConnection } = await loaders({
directory: path.resolve(cwd || process.cwd()),
expressApp: app,
isTest: false,
})

const loaders = require("@medusajs/medusa/dist/loaders").default
const PORT = await getPort()

const { container, dbConnection } = await loaders({
directory: path.resolve(cwd || process.cwd()),
expressApp: app,
isTest: false,
return {
container,
db: dbConnection,
pgConnection,
app,
port: PORT,
}
}

module.exports = {
bootstrapApp,
startBootstrapApp: async ({
cwd,
env = {},
skipExpressListen = false,
} = {}) => {
const { app, port, container, db, pgConnection } = await bootstrapApp({
cwd,
env,
})
let expressServer

const PORT = await getPort()
setContainer(container)

return {
container,
db: dbConnection,
app,
port: PORT,
if (skipExpressListen) {
return
}

const shutdown = async () => {
await Promise.all([
expressServer.close(),
db?.destroy(),
pgConnection?.context?.destroy(),
])

if (typeof global !== "undefined" && global?.gc) {
global.gc()
}
}

return await new Promise((resolve, reject) => {
expressServer = app.listen(port, async (err) => {
if (err) {
await shutdown()
return reject(err)
}
setPort(port)
process.send(port)
resolve(shutdown)
})

setExpressServer(expressServer)
})
},
}
17 changes: 4 additions & 13 deletions integration-tests/environment-helpers/setup-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,9 @@ const { spawn } = require("child_process")
const { setPort, useExpressServer } = require("./use-api")
const { setContainer } = require("./use-container")

module.exports = ({
cwd,
redisUrl,
uploadDir,
verbose,
env,
bootstrapApp = false,
}) => {
module.exports = async ({ cwd, redisUrl, uploadDir, verbose, env }) => {
const serverPath = path.join(__dirname, "test-server.js")

if (bootstrapApp) {
require(serverPath)
}

// in order to prevent conflicts in redis, use a different db for each worker
// same fix as for databases (works with up to 15)
// redis dbs are 0-indexed and jest worker ids are indexed from 1
Expand All @@ -25,7 +14,7 @@ module.exports = ({

verbose = verbose ?? false

return new Promise((resolve, reject) => {
return await new Promise((resolve, reject) => {
const medusaProcess = spawn("node", [path.resolve(serverPath)], {
cwd,
env: {
Expand All @@ -44,11 +33,13 @@ module.exports = ({

medusaProcess.on("error", (err) => {
console.log(err)
reject(err)
process.exit()
})

medusaProcess.on("uncaughtException", (err) => {
console.log(err)
reject(err)
medusaProcess.kill()
})

Expand Down
19 changes: 2 additions & 17 deletions integration-tests/environment-helpers/test-server.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
const { bootstrapApp } = require("./bootstrap-app")
const { setContainer } = require("./use-container")
const { setPort, setExpressServer } = require("./use-api")
const { startBootstrapApp } = require("./bootstrap-app")

const setup = async () => {
const { app, port, container } = await bootstrapApp()

setContainer(container)

const expressServer = app.listen(port, (err) => {
setPort(port)
process.send(port)
})

setExpressServer(expressServer)
}

setup()
startBootstrapApp()
5 changes: 0 additions & 5 deletions integration-tests/environment-helpers/use-container.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
const path = require("path")
const express = require("express")
const getPort = require("get-port")
const { isObject } = require("@medusajs/utils")

const AppUtils = {
container_: null,

Expand Down
13 changes: 10 additions & 3 deletions integration-tests/environment-helpers/use-db.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const path = require("path")

const { getConfigFile } = require("medusa-core-utils")
const { asValue } = require("awilix")
const { isObject, createMedusaContainer } = require("@medusajs/utils")
const { dropDatabase } = require("pg-god")
const { DataSource } = require("typeorm")
const dbFactory = require("./use-template-db")
const { getContainer } = require("./use-container")
const { ContainerRegistrationKeys } = require("@medusajs/utils")

const DB_HOST = process.env.DB_HOST
Expand Down Expand Up @@ -69,10 +69,10 @@ const DbTestUtil = {
},

shutdown: async function () {
await this.db_.destroy()
await this.db_?.destroy()
await this.pgConnection_?.context?.destroy()

return await dropDatabase({ DB_NAME }, pgGodCredentials)
return await dropDatabase({ databaseName: DB_NAME }, pgGodCredentials)
},
}

Expand Down Expand Up @@ -157,6 +157,12 @@ module.exports = {

const container = createMedusaContainer()

container.register({
[ContainerRegistrationKeys.CONFIG_MODULE]: asValue(configModule),
[ContainerRegistrationKeys.LOGGER]: asValue(console),
[ContainerRegistrationKeys.MANAGER]: asValue(dbDataSource.manager),
})

const pgConnection = await pgConnectionLoader({ configModule, container })
instance.setPgConnection(pgConnection)

Expand All @@ -168,6 +174,7 @@ module.exports = {
const options = {
database: {
clientUrl: DB_URL,
connection: pgConnection,
},
}
await runMigrations(options)
Expand Down
7 changes: 2 additions & 5 deletions integration-tests/environment-helpers/use-template-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ const pgGodCredentials = {

class DatabaseFactory {
constructor() {
this.dataSource_ = null
this.masterDataSourceName = "master"
this.templateDbName = "medusa-integration-template"
}

async createTemplateDb_({ cwd }) {
const { configModule } = getConfigFile(cwd, `medusa-config`)
const dataSource = await this.getMasterDataSource()

const migrationDir = path.resolve(
path.join(
__dirname,
Expand Down Expand Up @@ -80,8 +79,6 @@ class DatabaseFactory {
await templateDbDataSource.runMigrations()

await templateDbDataSource.destroy()

return dataSource
}

async getMasterDataSource() {
Expand All @@ -103,7 +100,7 @@ class DatabaseFactory {
async createFromTemplate(dbName) {
const dataSource = await this.getMasterDataSource()

await dataSource.query(`DROP DATABASE IF EXISTS "${dbName}";`)
await dropDatabase({ databaseName: dbName }, pgGodCredentials)
await dataSource.query(
`CREATE DATABASE "${dbName}" TEMPLATE "${this.templateDbName}";`
)
Expand Down
25 changes: 22 additions & 3 deletions integration-tests/globalTeardown.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
const dbFactory = require("./environment-helpers/use-template-db")
const { dropDatabase } = require("pg-god")

module.exports = async () => {
await dbFactory.destroy()
const DB_HOST = process.env.DB_HOST
const DB_USERNAME = process.env.DB_USERNAME
const DB_PASSWORD = process.env.DB_PASSWORD
const DB_NAME = process.env.DB_TEMP_NAME

const pgGodCredentials = {
user: DB_USERNAME,
password: DB_PASSWORD,
host: DB_HOST,
}

const teardown = async () => {
try {
await dropDatabase({ databaseName: DB_NAME }, pgGodCredentials)
} catch (e) {
console.error(
`This might fail if it is run during the unit tests since there is no database to drop. Otherwise, please check what is the issue. ${e.message}`
)
}
}

module.exports = teardown
26 changes: 11 additions & 15 deletions integration-tests/plugins/__tests__/cart/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { MoneyAmount, PriceList, Region } from "@medusajs/medusa"
import {
MoneyAmount,
PriceList,
ProductVariantMoneyAmount,
Region,
} from "@medusajs/medusa"
import path from "path"

import { ProductVariantMoneyAmount } from "@medusajs/medusa"
import { bootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import setupServer from "../../../../environment-helpers/setup-server"
import { setPort, useApi } from "../../../../environment-helpers/use-api"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import { simpleProductFactory } from "../../../../factories"

jest.setTimeout(30000)

describe("/store/carts", () => {
let medusaProcess
let dbConnection
let express
let shutdownServer

const doAfterEach = async () => {
const db = useDb()
Expand All @@ -23,18 +24,13 @@ describe("/store/carts", () => {
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({ cwd })
const { app, port } = await bootstrapApp({ cwd })
setPort(port)
express = app.listen(port, () => {
process.send?.(port)
})
shutdownServer = await startBootstrapApp({ cwd })
})

afterAll(async () => {
const db = useDb()
await db.shutdown()
medusaProcess.kill()
await shutdownServer()
})

describe("POST /store/carts", () => {
Expand Down
Loading

0 comments on commit 80fe362

Please sign in to comment.