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: https://github.com/medusajs/medusa/blob/f0cc1b324c964680c26e0d5c1d45769cbd750176/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
This commit is contained in:
Patrick
2022-12-08 14:57:16 +00:00
committed by GitHub
parent 34e405c492
commit 86f9455d00
17 changed files with 186 additions and 21264 deletions
-4
View File
@@ -1,4 +0,0 @@
npmRegistryServer: "http://localhost:4873"
unsafeHttpWhitelist:
- localhost
+5 -4
View File
@@ -1,23 +1,24 @@
{
"name": "api",
"name": "integration-tests-api",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"private": true,
"scripts": {
"test": "jest --silent=false --runInBand --bail --detectOpenHandles --forceExit",
"build": "babel src -d dist --extensions \".ts,.js\""
},
"dependencies": {
"@medusajs/medusa": "1.6.5-dev-1670340951307",
"@medusajs/medusa": "*",
"faker": "^5.5.3",
"medusa-interfaces": "1.3.3-dev-1670340951307",
"medusa-interfaces": "*",
"typeorm": "^0.2.31"
},
"devDependencies": {
"@babel/cli": "^7.12.10",
"@babel/core": "^7.12.10",
"@babel/node": "^7.12.10",
"babel-preset-medusa-package": "1.1.19-dev-1670340951307",
"babel-preset-medusa-package": "*",
"jest": "^26.6.3"
}
}
File diff suppressed because it is too large Load Diff
+1 -5
View File
@@ -1,16 +1,12 @@
const path = require("path")
const express = require("express")
const getPort = require("get-port")
const importFrom = require("import-from")
module.exports = {
bootstrapApp: async ({ cwd } = {}) => {
const app = express()
const loaders = importFrom(
cwd || process.cwd(),
"@medusajs/medusa/dist/loaders"
).default
const loaders = require("@medusajs/medusa/dist/loaders").default
const { container, dbConnection } = await loaders({
directory: path.resolve(cwd || process.cwd()),
+12 -31
View File
@@ -59,7 +59,8 @@ const DbTestUtil = {
continue
}
await manager.query(`DELETE FROM "${entity.tableName}";`)
await manager.query(`DELETE
FROM "${entity.tableName}";`)
}
if (connectionType === "sqlite") {
await manager.query(`PRAGMA foreign_keys = ON`)
@@ -81,27 +82,12 @@ module.exports = {
const configPath = path.resolve(path.join(cwd, `medusa-config.js`))
const { projectConfig, featureFlags } = require(configPath)
const featureFlagsLoader = require(path.join(
cwd,
`node_modules`,
`@medusajs`,
`medusa`,
`dist`,
`loaders`,
`feature-flags`
)).default
const featureFlagsLoader =
require("@medusajs/medusa/dist/loaders/feature-flags").default
const featureFlagsRouter = featureFlagsLoader({ featureFlags })
const modelsLoader = require(path.join(
cwd,
`node_modules`,
`@medusajs`,
`medusa`,
`dist`,
`loaders`,
`models`
)).default
const modelsLoader = require("@medusajs/medusa/dist/loaders/models").default
const entities = modelsLoader({}, { register: false })
@@ -119,10 +105,11 @@ module.exports = {
} else {
await dbFactory.createFromTemplate(DB_NAME)
// get migraitons with enabled featureflags
// get migrations with enabled featureflags
const migrationDir = path.resolve(
path.join(
cwd,
__dirname,
`../../`,
`node_modules`,
`@medusajs`,
`medusa`,
@@ -132,16 +119,9 @@ module.exports = {
)
)
const { getEnabledMigrations } = require(path.join(
cwd,
`node_modules`,
`@medusajs`,
`medusa`,
`dist`,
`commands`,
`utils`,
`get-migrations`
))
const {
getEnabledMigrations,
} = require("@medusajs/medusa/dist/commands/utils/get-migrations")
const enabledMigrations = await getEnabledMigrations(
[migrationDir],
@@ -157,6 +137,7 @@ module.exports = {
url: DB_URL,
entities: enabledEntities,
migrations: enabledMigrations,
name: "integration-tests",
})
await dbConnection.runMigrations()
+6 -12
View File
@@ -28,7 +28,8 @@ class DatabaseFactory {
const connection = await this.getMasterConnection()
const migrationDir = path.resolve(
path.join(
cwd,
__dirname,
`../../`,
`node_modules`,
`@medusajs`,
`medusa`,
@@ -38,18 +39,11 @@ class DatabaseFactory {
)
)
const { getEnabledMigrations } = require(path.join(
cwd,
`node_modules`,
`@medusajs`,
`medusa`,
`dist`,
`commands`,
`utils`,
`get-migrations`
))
const {
getEnabledMigrations,
} = require("@medusajs/medusa/dist/commands/utils/get-migrations")
// filter migrations to only include those that dont have feature flags
// filter migrations to only include those that don't have feature flags
const enabledMigrations = await getEnabledMigrations(
[migrationDir],
(flag) => false
-14
View File
@@ -1,14 +0,0 @@
{
"name": "integration-tests",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"dotenv": {
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz",
"integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==",
"dev": true
}
}
}
+1
View File
@@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "jest.config.js",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
-4
View File
@@ -1,4 +0,0 @@
npmRegistryServer: "http://localhost:4873"
unsafeHttpWhitelist:
- localhost
+8 -7
View File
@@ -1,25 +1,26 @@
{
"name": "plugins",
"name": "integration-tests-plugins",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"private": true,
"scripts": {
"test": "jest --runInBand --silent=false",
"test": "jest --silent=false --runInBand --bail --detectOpenHandles --forceExit",
"build": "babel src -d dist --extensions \".ts,.js\""
},
"dependencies": {
"@medusajs/medusa": "1.4.1-dev-1665082901122",
"@medusajs/medusa": "*",
"faker": "^5.5.3",
"medusa-fulfillment-webshipper": "1.3.3-dev-1665082901122",
"medusa-interfaces": "1.3.3-dev-1665082901122",
"medusa-plugin-sendgrid": "1.3.3-dev-1665082901122",
"medusa-fulfillment-webshipper": "*",
"medusa-interfaces": "*",
"medusa-plugin-sendgrid": "*",
"typeorm": "^0.2.31"
},
"devDependencies": {
"@babel/cli": "^7.12.10",
"@babel/core": "^7.12.10",
"@babel/node": "^7.12.10",
"babel-preset-medusa-package": "1.1.19-dev-1665082901122",
"babel-preset-medusa-package": "*",
"jest": "^26.6.3"
}
}
File diff suppressed because it is too large Load Diff
-29
View File
@@ -1,29 +0,0 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!
__metadata:
version: 6
cacheKey: 8c0
"@faker-js/faker@npm:^5.5.3":
version: 5.5.3
resolution: "@faker-js/faker@npm:5.5.3"
checksum: 3f7fbf0b0cfe23c7750ab79b123be8f845e5f376ec28bf43b7b017983b6fc3a9dc22543c4eea52e30cc119699c0f47f62a2c02e9eae9b6a20b75955e9c3eb887
languageName: node
linkType: hard
"dotenv@npm:^10.0.0":
version: 10.0.0
resolution: "dotenv@npm:10.0.0"
checksum: 2d8d4ba64bfaff7931402aa5e8cbb8eba0acbc99fe9ae442300199af021079eafa7171ce90e150821a5cb3d74f0057721fbe7ec201a6044b68c8a7615f8c123f
languageName: node
linkType: hard
"integration-tests@workspace:.":
version: 0.0.0-use.local
resolution: "integration-tests@workspace:."
dependencies:
"@faker-js/faker": ^5.5.3
dotenv: ^10.0.0
languageName: unknown
linkType: soft