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
This commit is contained in:
Patrick
2022-12-08 09:57:16 -05:00
committed by GitHub
parent 34e405c492
commit 86f9455d00
17 changed files with 186 additions and 21264 deletions

View File

@@ -0,0 +1,6 @@
---
"integration-tests-api": patch
"integration-tests-plugins": patch
---
test: allow integration-tests to leverage workspaces optimizations

View File

@@ -123,28 +123,8 @@ jobs:
- name: Build Packages
run: yarn build
- name: Install dev cli
run: sudo npm i -g medusa-dev-cli@latest
- name: Set path to medusa repo
run: medusa-dev --set-path-to-repo `pwd`
- name: Set npm registry
run: |
yarn config set npmRegistryServer http://localhost:4873
echo -e 'unsafeHttpWhitelist:\n - "localhost"' >> .yarnrc.yml
- name: Force install
run: medusa-dev --force-install --external-registry
working-directory: integration-tests/api
- name: Build integration tests
run: yarn build
working-directory: integration-tests/api
- name: Run integration tests
run: yarn test
working-directory: integration-tests/api
run: yarn test:integration:api
env:
DB_PASSWORD: postgres
SPLIT: ${{ steps['split-tests'].outputs['split'] }}
@@ -195,26 +175,7 @@ jobs:
- name: Build Packages
run: yarn build
- name: Install dev cli
run: sudo npm i -g medusa-dev-cli
- name: Set path to medusa repo
run: medusa-dev --set-path-to-repo `pwd`
- name: Set npm registry
run: |
yarn config set npmRegistryServer http://localhost:4873
echo -e 'unsafeHttpWhitelist:\n - "localhost"' >> .yarnrc.yml
- name: Force install
run: medusa-dev --force-install --external-registry
working-directory: integration-tests/plugins
- name: Build plugin integration tests
run: yarn build
working-directory: integration-tests/plugins
- name: Run plugin integration tests
run: yarn test:integration:plugins --silent=false
run: yarn test:integration:plugins
env:
DB_PASSWORD: postgres

View File

@@ -1,4 +0,0 @@
npmRegistryServer: "http://localhost:4873"
unsafeHttpWhitelist:
- localhost

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

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()),

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()

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

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
}
}
}

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"
},

View File

@@ -1,4 +0,0 @@
npmRegistryServer: "http://localhost:4873"
unsafeHttpWhitelist:
- localhost

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

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

View File

@@ -3,7 +3,8 @@
"private": true,
"workspaces": {
"packages": [
"packages/*"
"packages/*",
"integration-tests/**/*"
]
},
"devDependencies": {
@@ -61,10 +62,10 @@
"lint": "eslint --ignore-path .eslintignore --ext .js,.ts,.tsx .",
"prettier": "prettier",
"jest": "jest",
"test": "turbo run test",
"test:integration": "NODE_ENV=test jest --runInBand --bail --config=integration-tests/jest.config.js",
"test:integration:api": "NODE_ENV=test jest --detectOpenHandles --forceExit --runInBand --bail --config=integration-tests/jest.config.js --projects=integration-tests/api -- integration-tests/api/__tests__/store/cart/cart.js",
"test:integration:plugins": "NODE_ENV=test jest --runInBand --bail --config=integration-tests/jest.config.js --projects=integration-tests/plugins",
"test": "turbo run test --filter=!integration-tests-api --filter=!integration-tests-plugins",
"test:integration": "turbo run test --filter=integration-tests-api --filter=integration-tests-plugins",
"test:integration:api": "turbo run test --filter=integration-tests-api",
"test:integration:plugins": "turbo run test --filter=integration-tests-plugins",
"openapi:generate": "node ./scripts/build-openapi.js",
"generate:services": "typedoc --options typedoc.services.js",
"generate:js-client": "typedoc --options typedoc.js-client.js",

View File

@@ -3,11 +3,26 @@
"baseBranch": "origin/master",
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**", "api/**", "services/**", "subscribers/**"]
"dependsOn": [
"^build"
],
"outputs": [
"dist/**",
"api/**",
"helpers/**",
"loaders/**",
"migrations/**",
"models/**",
"repositories/**",
"services/**",
"subscribers/**",
"utils/**"
]
},
"test": {
"dependsOn": ["build"],
"dependsOn": [
"build"
],
"outputs": []
}
}

127
yarn.lock
View File

@@ -243,6 +243,33 @@ __metadata:
languageName: node
linkType: hard
"@babel/cli@npm:^7.12.10":
version: 7.19.3
resolution: "@babel/cli@npm:7.19.3"
dependencies:
"@jridgewell/trace-mapping": ^0.3.8
"@nicolo-ribaudo/chokidar-2": 2.1.8-no-fsevents.3
chokidar: ^3.4.0
commander: ^4.0.1
convert-source-map: ^1.1.0
fs-readdir-recursive: ^1.1.0
glob: ^7.2.0
make-dir: ^2.1.0
slash: ^2.0.0
peerDependencies:
"@babel/core": ^7.0.0-0
dependenciesMeta:
"@nicolo-ribaudo/chokidar-2":
optional: true
chokidar:
optional: true
bin:
babel: ./bin/babel.js
babel-external-helpers: ./bin/babel-external-helpers.js
checksum: e996aa6a1cde07555ef83782d5809049e6ebecb16884e94acad6eea9a7f6323f6303ee74004a31b29b5ead257ad697f33650b6983e4e9fb14ef1f2908ea5c0a1
languageName: node
linkType: hard
"@babel/code-frame@npm:7.12.11":
version: 7.12.11
resolution: "@babel/code-frame@npm:7.12.11"
@@ -632,6 +659,24 @@ __metadata:
languageName: node
linkType: hard
"@babel/node@npm:^7.12.10":
version: 7.20.5
resolution: "@babel/node@npm:7.20.5"
dependencies:
"@babel/register": ^7.18.9
commander: ^4.0.1
core-js: ^3.26.0
node-environment-flags: ^1.0.5
regenerator-runtime: ^0.13.11
v8flags: ^3.1.1
peerDependencies:
"@babel/core": ^7.0.0-0
bin:
babel-node: ./bin/babel-node.js
checksum: 83a0203b3f62b7a584d9177678d6a7d8e2a343f1bf9891b28155314b740140bd5560aca4848f54bd2c35446190c3af35898bdec98323a38035465ddb65863e54
languageName: node
linkType: hard
"@babel/node@npm:^7.12.6, @babel/node@npm:^7.15.4, @babel/node@npm:^7.16.0, @babel/node@npm:^7.7.4":
version: 7.18.6
resolution: "@babel/node@npm:7.18.6"
@@ -1825,6 +1870,21 @@ __metadata:
languageName: node
linkType: hard
"@babel/register@npm:^7.18.9":
version: 7.18.9
resolution: "@babel/register@npm:7.18.9"
dependencies:
clone-deep: ^4.0.1
find-cache-dir: ^2.0.0
make-dir: ^2.1.0
pirates: ^4.0.5
source-map-support: ^0.5.16
peerDependencies:
"@babel/core": ^7.0.0-0
checksum: b19c1445adf202732a2e0d554749257da22e56f0fc159709200d962413fbd4e7bd1d684222e60c08a2b8ad8fe511d8699fbc978d92816953fc9cbb6cbcc40d63
languageName: node
linkType: hard
"@babel/runtime-corejs3@npm:^7.10.2":
version: 7.18.6
resolution: "@babel/runtime-corejs3@npm:7.18.6"
@@ -4333,7 +4393,7 @@ __metadata:
languageName: unknown
linkType: soft
"@medusajs/medusa@^1.3.3, @medusajs/medusa@^1.6.2, @medusajs/medusa@workspace:packages/medusa":
"@medusajs/medusa@*, @medusajs/medusa@^1.3.3, @medusajs/medusa@^1.6.2, @medusajs/medusa@workspace:packages/medusa":
version: 0.0.0-use.local
resolution: "@medusajs/medusa@workspace:packages/medusa"
dependencies:
@@ -10817,7 +10877,7 @@ __metadata:
languageName: node
linkType: hard
"babel-preset-medusa-package@^1.0.0, babel-preset-medusa-package@^1.1.19, babel-preset-medusa-package@workspace:packages/babel-preset-medusa-package":
"babel-preset-medusa-package@*, babel-preset-medusa-package@^1.0.0, babel-preset-medusa-package@^1.1.19, babel-preset-medusa-package@workspace:packages/babel-preset-medusa-package":
version: 0.0.0-use.local
resolution: "babel-preset-medusa-package@workspace:packages/babel-preset-medusa-package"
dependencies:
@@ -12925,6 +12985,13 @@ __metadata:
languageName: node
linkType: hard
"core-js@npm:^3.26.0":
version: 3.26.1
resolution: "core-js@npm:3.26.1"
checksum: 82d36c6f54fc0349998fa7fc67d200ba272f1cd1674c6786dc17f9d259d6555fc05662044528eae73ad6e90f71d503ab5c060ad4745492ef804308209f9aec13
languageName: node
linkType: hard
"core-util-is@npm:1.0.2":
version: 1.0.2
resolution: "core-util-is@npm:1.0.2"
@@ -16139,6 +16206,13 @@ __metadata:
languageName: node
linkType: hard
"faker@npm:^5.5.3":
version: 5.5.3
resolution: "faker@npm:5.5.3"
checksum: 55ee2fb6425df717253f237b4e952c94efe33da23a5826ca41c6ecb31ccfb49d06c5de64b82b6994ca9c76c05eab4dbf779cea047455fff6e62cc9d585c7d460
languageName: node
linkType: hard
"fast-copy@npm:^2.1.0":
version: 2.1.3
resolution: "fast-copy@npm:2.1.3"
@@ -17876,7 +17950,7 @@ __metadata:
languageName: node
linkType: hard
"glob@npm:^7.0.0, glob@npm:^7.1.1, glob@npm:^7.1.2, glob@npm:^7.1.3, glob@npm:^7.1.4, glob@npm:^7.1.6, glob@npm:^7.2.3":
"glob@npm:^7.0.0, glob@npm:^7.1.1, glob@npm:^7.1.2, glob@npm:^7.1.3, glob@npm:^7.1.4, glob@npm:^7.1.6, glob@npm:^7.2.0, glob@npm:^7.2.3":
version: 7.2.3
resolution: "glob@npm:7.2.3"
dependencies:
@@ -19263,6 +19337,40 @@ __metadata:
languageName: node
linkType: hard
"integration-tests-api@workspace:integration-tests/api":
version: 0.0.0-use.local
resolution: "integration-tests-api@workspace:integration-tests/api"
dependencies:
"@babel/cli": ^7.12.10
"@babel/core": ^7.12.10
"@babel/node": ^7.12.10
"@medusajs/medusa": "*"
babel-preset-medusa-package: "*"
faker: ^5.5.3
jest: ^26.6.3
medusa-interfaces: "*"
typeorm: ^0.2.31
languageName: unknown
linkType: soft
"integration-tests-plugins@workspace:integration-tests/plugins":
version: 0.0.0-use.local
resolution: "integration-tests-plugins@workspace:integration-tests/plugins"
dependencies:
"@babel/cli": ^7.12.10
"@babel/core": ^7.12.10
"@babel/node": ^7.12.10
"@medusajs/medusa": "*"
babel-preset-medusa-package: "*"
faker: ^5.5.3
jest: ^26.6.3
medusa-fulfillment-webshipper: "*"
medusa-interfaces: "*"
medusa-plugin-sendgrid: "*"
typeorm: ^0.2.31
languageName: unknown
linkType: soft
"internal-slot@npm:^1.0.3":
version: 1.0.3
resolution: "internal-slot@npm:1.0.3"
@@ -23710,7 +23818,7 @@ __metadata:
languageName: unknown
linkType: soft
"medusa-fulfillment-webshipper@workspace:packages/medusa-fulfillment-webshipper":
"medusa-fulfillment-webshipper@*, medusa-fulfillment-webshipper@workspace:packages/medusa-fulfillment-webshipper":
version: 0.0.0-use.local
resolution: "medusa-fulfillment-webshipper@workspace:packages/medusa-fulfillment-webshipper"
dependencies:
@@ -23735,7 +23843,7 @@ __metadata:
languageName: unknown
linkType: soft
"medusa-interfaces@^1.3.1, medusa-interfaces@^1.3.3, medusa-interfaces@workspace:packages/medusa-interfaces":
"medusa-interfaces@*, medusa-interfaces@^1.3.1, medusa-interfaces@^1.3.3, medusa-interfaces@workspace:packages/medusa-interfaces":
version: 0.0.0-use.local
resolution: "medusa-interfaces@workspace:packages/medusa-interfaces"
dependencies:
@@ -24162,7 +24270,7 @@ __metadata:
languageName: unknown
linkType: soft
"medusa-plugin-sendgrid@workspace:packages/medusa-plugin-sendgrid":
"medusa-plugin-sendgrid@*, medusa-plugin-sendgrid@workspace:packages/medusa-plugin-sendgrid":
version: 0.0.0-use.local
resolution: "medusa-plugin-sendgrid@workspace:packages/medusa-plugin-sendgrid"
dependencies:
@@ -29211,6 +29319,13 @@ __metadata:
languageName: node
linkType: hard
"regenerator-runtime@npm:^0.13.11":
version: 0.13.11
resolution: "regenerator-runtime@npm:0.13.11"
checksum: 12b069dc774001fbb0014f6a28f11c09ebfe3c0d984d88c9bced77fdb6fedbacbca434d24da9ae9371bfbf23f754869307fb51a4c98a8b8b18e5ef748677ca24
languageName: node
linkType: hard
"regenerator-runtime@npm:^0.13.3, regenerator-runtime@npm:^0.13.4, regenerator-runtime@npm:^0.13.5, regenerator-runtime@npm:^0.13.7, regenerator-runtime@npm:^0.13.9":
version: 0.13.9
resolution: "regenerator-runtime@npm:0.13.9"