chore: Use module test runner in all modules (#6706)

I replaced the custom test setup with the test runner. This should make migrating to mikroorm 6 a lot easier once we do it.

There are few more modules to be done, but I thought the PR is super big already so we can tackle them separately.

Note that there are no or very little test changes, it is mostly the setup around the tests.
This commit is contained in:
Stevche Radevski authored and GitHub committed 2024-03-14 21:01:50 +00:00
1 parent 1eb90f739b
commit 68d869607f
102 files changed
+21692 -25473

No files matched your search

@@ -1,362 +1,353 @@
import { Modules } from "@medusajs/modules-sdk"
import { IRegionModuleService } from "@medusajs/types"
import { initModules } from "medusa-test-utils"
import { MikroOrmWrapper } from "../utils"
import { getInitModuleConfig } from "../utils/get-init-module-config"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
jest.setTimeout(30000)
describe("Region Module Service", () => {
let service: IRegionModuleService
let shutdownFunc: () => Promise<void>
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
const initModulesConfig = getInitModuleConfig()
const { medusaApp, shutdown } = await initModules(initModulesConfig)
service = medusaApp.modules[Modules.REGION]
shutdownFunc = shutdown
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
await shutdownFunc()
})
it("should create countries on application start", async () => {
const countries = await service.listCountries({}, { take: null })
expect(countries.length).toEqual(250)
})
it("should create and list a region", async () => {
const createdRegion = await service.create({
name: "Europe",
currency_code: "EUR",
automatic_taxes: false,
})
expect(createdRegion).toEqual(
expect.objectContaining({
id: createdRegion.id,
name: "Europe",
currency_code: "eur",
countries: [],
automatic_taxes: false,
moduleIntegrationTestRunner({
moduleName: Modules.REGION,
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IRegionModuleService>) => {
describe("Region Module Service", () => {
it("should create countries on application start", async () => {
const countries = await service.listCountries({}, { take: null })
expect(countries.length).toEqual(250)
})
)
const region = await service.retrieve(createdRegion.id, {
relations: ["countries"],
})
it("should create and list a region", async () => {
const createdRegion = await service.create({
name: "Europe",
currency_code: "EUR",
automatic_taxes: false,
})
expect(region).toEqual(
expect.objectContaining({
id: region.id,
name: "Europe",
currency_code: "eur",
countries: [],
})
)
})
it("should create a region with countries", async () => {
const createdRegion = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
const region = await service.retrieve(createdRegion.id, {
relations: ["countries"],
})
expect(region).toEqual(
expect.objectContaining({
id: region.id,
name: "North America",
currency_code: "usd",
automatic_taxes: true,
countries: [
expect(createdRegion).toEqual(
expect.objectContaining({
display_name: "Canada",
iso_2: "ca",
}),
id: createdRegion.id,
name: "Europe",
currency_code: "eur",
countries: [],
automatic_taxes: false,
})
)
const region = await service.retrieve(createdRegion.id, {
relations: ["countries"],
})
expect(region).toEqual(
expect.objectContaining({
display_name: "United States",
iso_2: "us",
}),
],
id: region.id,
name: "Europe",
currency_code: "eur",
countries: [],
})
)
})
)
})
it("should throw when country doesn't exist", async () => {
await expect(
service.create({
name: "North America",
currency_code: "USD",
countries: ["neverland"],
})
).rejects.toThrowError('Countries with codes: "neverland" do not exist')
})
it("should throw when country is already assigned to a region", async () => {
await service.create({
name: "North America",
currency_code: "USD",
countries: ["us"],
})
await expect(
service.create({
name: "United States",
currency_code: "USD",
countries: ["us"],
})
).rejects.toThrowError(
'Countries with codes: "us" are already assigned to a region'
)
})
it("should throw when country is being assigned to multiple regions", async () => {
await expect(
service.create([
{
name: "United States",
it("should create a region with countries", async () => {
const createdRegion = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us"],
},
{
countries: ["us", "ca"],
})
const region = await service.retrieve(createdRegion.id, {
relations: ["countries"],
})
expect(region).toEqual(
expect.objectContaining({
id: region.id,
name: "North America",
currency_code: "usd",
automatic_taxes: true,
countries: [
expect.objectContaining({
display_name: "Canada",
iso_2: "ca",
}),
expect.objectContaining({
display_name: "United States",
iso_2: "us",
}),
],
})
)
})
it("should throw when country doesn't exist", async () => {
await expect(
service.create({
name: "North America",
currency_code: "USD",
countries: ["neverland"],
})
).rejects.toThrowError('Countries with codes: "neverland" do not exist')
})
it("should throw when country is already assigned to a region", async () => {
await service.create({
name: "North America",
currency_code: "USD",
countries: ["us"],
},
])
).rejects.toThrowError(
'Countries with codes: "us" are already assigned to a region'
)
})
})
it("should upsert the region successfully", async () => {
const createdRegion = await service.upsert({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
await expect(
service.create({
name: "United States",
currency_code: "USD",
countries: ["us"],
})
).rejects.toThrowError(
'Countries with codes: "us" are already assigned to a region'
)
})
await service.upsert({
id: createdRegion.id,
name: "Americas",
currency_code: "MXN",
countries: ["us", "mx"],
automatic_taxes: false,
})
it("should throw when country is being assigned to multiple regions", async () => {
await expect(
service.create([
{
name: "United States",
currency_code: "USD",
countries: ["us"],
},
{
name: "North America",
currency_code: "USD",
countries: ["us"],
},
])
).rejects.toThrowError(
'Countries with codes: "us" are already assigned to a region'
)
})
const latestRegion = await service.retrieve(createdRegion.id, {
relations: ["countries"],
})
it("should upsert the region successfully", async () => {
const createdRegion = await service.upsert({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
expect(latestRegion).toMatchObject({
id: createdRegion.id,
name: "Americas",
currency_code: "mxn",
automatic_taxes: false,
})
expect(latestRegion.countries.map((c) => c.iso_2)).toEqual(["mx", "us"])
})
it("should allow mixing create and update operations in upsert", async () => {
const createdRegion = await service.upsert({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
const upserted = await service.upsert([
{
id: createdRegion.id,
name: "Americas",
currency_code: "USD",
countries: ["us", "ca"],
},
{
name: "Central America",
currency_code: "MXN",
countries: ["mx"],
},
])
expect(upserted).toEqual(
expect.arrayContaining([
expect.objectContaining({
await service.upsert({
id: createdRegion.id,
name: "Americas",
currency_code: "usd",
}),
expect.objectContaining({
name: "Central America",
currency_code: "mxn",
}),
])
)
})
it("should update the region successfully", async () => {
const createdRegion = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
await service.update(createdRegion.id, {
name: "Americas",
currency_code: "MXN",
countries: ["us", "mx"],
})
const latestRegion = await service.retrieve(createdRegion.id, {
relations: ["countries"],
})
expect(latestRegion).toMatchObject({
id: createdRegion.id,
name: "Americas",
currency_code: "mxn",
})
expect(latestRegion.countries.map((c) => c.iso_2)).toEqual(["mx", "us"])
})
it("should update the region without affecting countries if countries are undefined", async () => {
const createdRegion = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
await service.update(createdRegion.id, {
name: "Americas",
currency_code: "MXN",
})
const updatedRegion = await service.retrieve(createdRegion.id, {
relations: ["countries"],
})
expect(updatedRegion).toMatchObject({
id: createdRegion.id,
name: "Americas",
currency_code: "mxn",
})
expect(updatedRegion.countries.map((c) => c.iso_2)).toEqual(["ca", "us"])
})
it("should remove the countries in a region successfully", async () => {
const createdRegion = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
await service.update(createdRegion.id, {
name: "Americas",
currency_code: "MXN",
countries: [],
})
const updatedRegion = await service.retrieve(createdRegion.id, {
relations: ["countries"],
})
expect(updatedRegion).toMatchObject({
id: createdRegion.id,
name: "Americas",
currency_code: "mxn",
})
expect(updatedRegion.countries).toHaveLength(0)
})
it("should fail updating the region countries to non-existent ones", async () => {
const createdRegion = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
await expect(
service.update(
{ id: createdRegion.id },
{
countries: ["us", "neverland"],
}
)
).rejects.toThrowError('Countries with codes: "neverland" do not exist')
})
it("should fail updating the region if there are duplicate countries", async () => {
const createdRegion = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
await expect(
service.update(
{ id: createdRegion.id },
{
countries: ["us", "us"],
}
)
).rejects.toThrowError(
'Countries with codes: "us" are already assigned to a region'
)
})
it("should fail updating the region if country is already used", async () => {
const [createdRegion] = await service.create([
{
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
},
{
name: "Americas",
currency_code: "USD",
countries: ["mx"],
},
])
await expect(
service.update(
{ id: createdRegion.id },
{
currency_code: "MXN",
countries: ["us", "mx"],
}
)
).rejects.toThrowError(
'Countries with codes: "mx" are already assigned to a region'
)
})
automatic_taxes: false,
})
it("should unset the region ID on the country when deleting a region", async () => {
const createdRegion = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
const latestRegion = await service.retrieve(createdRegion.id, {
relations: ["countries"],
})
expect(latestRegion).toMatchObject({
id: createdRegion.id,
name: "Americas",
currency_code: "mxn",
automatic_taxes: false,
})
expect(latestRegion.countries.map((c) => c.iso_2)).toEqual(["mx", "us"])
})
it("should allow mixing create and update operations in upsert", async () => {
const createdRegion = await service.upsert({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
const upserted = await service.upsert([
{
id: createdRegion.id,
name: "Americas",
currency_code: "USD",
countries: ["us", "ca"],
},
{
name: "Central America",
currency_code: "MXN",
countries: ["mx"],
},
])
expect(upserted).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: createdRegion.id,
name: "Americas",
currency_code: "usd",
}),
expect.objectContaining({
name: "Central America",
currency_code: "mxn",
}),
])
)
})
it("should update the region successfully", async () => {
const createdRegion = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
await service.update(createdRegion.id, {
name: "Americas",
currency_code: "MXN",
countries: ["us", "mx"],
})
const latestRegion = await service.retrieve(createdRegion.id, {
relations: ["countries"],
})
expect(latestRegion).toMatchObject({
id: createdRegion.id,
name: "Americas",
currency_code: "mxn",
})
expect(latestRegion.countries.map((c) => c.iso_2)).toEqual(["mx", "us"])
})
it("should update the region without affecting countries if countries are undefined", async () => {
const createdRegion = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
await service.update(createdRegion.id, {
name: "Americas",
currency_code: "MXN",
})
const updatedRegion = await service.retrieve(createdRegion.id, {
relations: ["countries"],
})
expect(updatedRegion).toMatchObject({
id: createdRegion.id,
name: "Americas",
currency_code: "mxn",
})
expect(updatedRegion.countries.map((c) => c.iso_2)).toEqual([
"ca",
"us",
])
})
it("should remove the countries in a region successfully", async () => {
const createdRegion = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
await service.update(createdRegion.id, {
name: "Americas",
currency_code: "MXN",
countries: [],
})
const updatedRegion = await service.retrieve(createdRegion.id, {
relations: ["countries"],
})
expect(updatedRegion).toMatchObject({
id: createdRegion.id,
name: "Americas",
currency_code: "mxn",
})
expect(updatedRegion.countries).toHaveLength(0)
})
it("should fail updating the region countries to non-existent ones", async () => {
const createdRegion = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
await expect(
service.update(
{ id: createdRegion.id },
{
countries: ["us", "neverland"],
}
)
).rejects.toThrowError('Countries with codes: "neverland" do not exist')
})
it("should fail updating the region if there are duplicate countries", async () => {
const createdRegion = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
await expect(
service.update(
{ id: createdRegion.id },
{
countries: ["us", "us"],
}
)
).rejects.toThrowError(
'Countries with codes: "us" are already assigned to a region'
)
})
it("should fail updating the region if country is already used", async () => {
const [createdRegion] = await service.create([
{
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
},
{
name: "Americas",
currency_code: "USD",
countries: ["mx"],
},
])
await expect(
service.update(
{ id: createdRegion.id },
{
countries: ["us", "mx"],
}
)
).rejects.toThrowError(
'Countries with codes: "mx" are already assigned to a region'
)
})
it("should unset the region ID on the country when deleting a region", async () => {
const createdRegion = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
await service.delete(createdRegion.id)
const resp = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
expect(resp.countries).toHaveLength(2)
})
})
await service.delete(createdRegion.id)
const resp = await service.create({
name: "North America",
currency_code: "USD",
countries: ["us", "ca"],
})
expect(resp.countries).toHaveLength(2)
})
},
})
@@ -1,8 +0,0 @@
if (typeof process.env.DB_TEMP_NAME === "undefined") {
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
process.env.DB_TEMP_NAME = `medusa-region-integration-${tempName}`
}
process.env.MEDUSA_REGION_DB_SCHEMA = "public"
// TODO: Remove this when all modules are migrated to v2
process.env.MEDUSA_FF_MEDUSA_V2 = true
@@ -1,3 +0,0 @@
import { JestUtils } from "medusa-test-utils"
JestUtils.afterAllHookDropDatabase()
@@ -1,6 +0,0 @@
import { ModuleServiceInitializeOptions } from "@medusajs/types"
export const databaseOptions: ModuleServiceInitializeOptions["database"] = {
schema: "public",
clientUrl: "medusa-region-test",
}
@@ -1,18 +0,0 @@
import {TestDatabaseUtils} from "medusa-test-utils"
import * as RegionModels from "@models"
const pathToMigrations = "../../src/migrations"
const mikroOrmEntities = RegionModels as unknown as any[]
export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper({
mikroOrmEntities,
pathToMigrations,
})
export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig({
mikroOrmEntities,
pathToMigrations,
})
export const DB_URL = TestDatabaseUtils.getDatabaseURL()
@@ -1,33 +0,0 @@
import { Modules, ModulesDefinition } from "@medusajs/modules-sdk"
import { DB_URL } from "./database"
export function getInitModuleConfig() {
const moduleOptions = {
defaultAdapterOptions: {
database: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_REGION_DB_SCHEMA,
},
},
}
const injectedDependencies = {}
const modulesConfig_ = {
[Modules.REGION]: {
definition: ModulesDefinition[Modules.REGION],
options: moduleOptions,
},
}
return {
injectedDependencies,
modulesConfig: modulesConfig_,
databaseConfig: {
clientUrl: DB_URL,
schema: process.env.MEDUSA_REGION_DB_SCHEMA,
},
joinerConfig: [],
}
}
@@ -1,3 +0,0 @@
export * from "./config"
export * from "./database"
-2
View File
@@ -17,6 +17,4 @@ module.exports = {
testEnvironment: `node`,
moduleFileExtensions: [`js`, `ts`],
modulePathIgnorePatterns: ["dist/"],
setupFiles: ["<rootDir>/integration-tests/setup-env.js"],
setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"],
}