Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(integration): setup #5511

Merged
merged 16 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 48 additions & 18 deletions integration-tests/environment-helpers/bootstrap-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,60 @@ 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 loaders = require("@medusajs/medusa/dist/loaders").default

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

const PORT = await getPort()
const PORT = await getPort()

return {
container,
db: dbConnection,
app,
port: PORT,
return {
container,
db: dbConnection,
app,
port: PORT,
}
}

module.exports = {
bootstrapApp,
startBootstrapApp: async ({
cwd,
env = {},
skipExpressListen = false,
riqwan marked this conversation as resolved.
Show resolved Hide resolved
} = {}) => {
const { app, port, container } = await bootstrapApp({ cwd, env })

setContainer(container)

if (skipExpressListen) {
return
}

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

setExpressServer(expressServer)
})
},
}
19 changes: 4 additions & 15 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 @@ -43,12 +32,12 @@ 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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: nice catch

},
}
await runMigrations(options)
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/environment-helpers/use-template-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,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
28 changes: 14 additions & 14 deletions integration-tests/plugins/__tests__/cart/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
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,
useExpressServer,
} 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

Expand All @@ -23,18 +27,14 @@ 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)
})
await startBootstrapApp({ cwd })
express = useExpressServer()
})

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

describe("POST /store/carts", () => {
Expand Down
20 changes: 11 additions & 9 deletions integration-tests/plugins/__tests__/inventory/cart/cart.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
const path = require("path")

const {
bootstrapApp,
startBootstrapApp,
} = require("../../../../environment-helpers/bootstrap-app")
const { initDb, useDb } = require("../../../../environment-helpers/use-db")
const { setPort, useApi } = require("../../../../environment-helpers/use-api")
const {
useApi,
useExpressServer,
} = require("../../../../environment-helpers/use-api")

const adminSeeder = require("../../../../helpers/admin-seeder")
const cartSeeder = require("../../../../helpers/cart-seeder")
const { simpleProductFactory } = require("../../../../factories")
const { simpleSalesChannelFactory } = require("../../../../factories")
const {
getContainer,
} = require("../../../../environment-helpers/use-container")

jest.setTimeout(30000)

Expand All @@ -32,13 +38,9 @@ describe("/store/carts", () => {
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
dbConnection = await initDb({ cwd })
const { container, app, port } = await bootstrapApp({ cwd })
appContainer = container

setPort(port)
express = app.listen(port, (err) => {
process.send(port)
})
await startBootstrapApp({ cwd })
appContainer = getContainer()
express = useExpressServer()
})

afterAll(async () => {
Expand Down
Loading