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
2024-03-14 21:01:50 +00:00
committed by GitHub
parent 1eb90f739b
commit 68d869607f
102 changed files with 21692 additions and 25473 deletions
@@ -1,12 +1,9 @@
import { IUserModuleService } from "@medusajs/types/dist/user"
import { MikroOrmWrapper } from "../../../utils"
import { MockEventBusService } from "medusa-test-utils"
import { Modules } from "@medusajs/modules-sdk"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { UserEvents } from "@medusajs/utils"
import { createInvites } from "../../../__fixtures__/invite"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { initModules } from "medusa-test-utils"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
jest.setTimeout(30000)
@@ -28,243 +25,235 @@ const defaultInviteData = [
},
]
describe("UserModuleService - Invite", () => {
let service: IUserModuleService
let testManager: SqlEntityManager
let shutdownFunc: () => Promise<void>
beforeAll(async () => {
const initModulesConfig = getInitModuleConfig()
const { medusaApp, shutdown } = await initModules(initModulesConfig)
service = medusaApp.modules[Modules.USER]
shutdownFunc = shutdown
})
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
testManager = MikroOrmWrapper.forkManager()
jest.clearAllMocks()
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
})
afterAll(async () => {
await shutdownFunc()
})
describe("listInvites", () => {
it("should list invites", async () => {
await createInvites(testManager, defaultInviteData)
const invites = await service.listInvites()
expect(invites).toEqual([
expect.objectContaining({
id: "1",
}),
expect.objectContaining({
id: "2",
}),
])
})
it("should list invites by id", async () => {
await createInvites(testManager, defaultInviteData)
const invites = await service.listInvites({
id: ["1"],
moduleIntegrationTestRunner({
moduleName: Modules.USER,
moduleOptions: {
jwt_secret: "test",
},
injectedDependencies: {
eventBusModuleService: new MockEventBusService(),
},
testSuite: ({
MikroOrmWrapper,
service,
}: SuiteOptions<IUserModuleService>) => {
describe("UserModuleService - Invite", () => {
beforeEach(async () => {
jest.clearAllMocks()
})
expect(invites).toEqual([
expect.objectContaining({
id: "1",
}),
])
})
})
describe("listInvites", () => {
it("should list invites", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
describe("listAndCountInvites", () => {
it("should list and count invites", async () => {
await createInvites(testManager, defaultInviteData)
const [invites, count] = await service.listAndCountInvites()
const invites = await service.listInvites()
expect(count).toEqual(2)
expect(invites).toEqual([
expect.objectContaining({
id: "1",
}),
expect.objectContaining({
id: "2",
}),
])
})
it("should listAndCount invites by id", async () => {
await createInvites(testManager, defaultInviteData)
const [invites, count] = await service.listAndCountInvites({
id: "1",
})
expect(count).toEqual(1)
expect(invites).toEqual([
expect.objectContaining({
id: "1",
}),
])
})
})
describe("retrieveInvite", () => {
const id = "1"
it("should return an invite for the given id", async () => {
await createInvites(testManager, defaultInviteData)
const invite = await service.retrieveInvite(id)
expect(invite).toEqual(
expect.objectContaining({
id,
expect(invites).toEqual([
expect.objectContaining({
id: "1",
}),
expect.objectContaining({
id: "2",
}),
])
})
)
})
it("should throw an error when an invite with the given id does not exist", async () => {
const error = await service
.retrieveInvite("does-not-exist")
.catch((e) => e)
it("should list invites by id", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
const invites = await service.listInvites({
id: ["1"],
})
expect(error.message).toEqual(
"Invite with id: does-not-exist was not found"
)
})
it("should throw an error when inviteId is not provided", async () => {
const error = await service
.retrieveInvite(undefined as unknown as string)
.catch((e) => e)
expect(error.message).toEqual("invite - id must be defined")
})
it("should return invite based on config select param", async () => {
await createInvites(testManager, defaultInviteData)
const invite = await service.retrieveInvite(id, {
select: ["id"],
})
expect(invite).toEqual({
id,
})
})
})
describe("updateInvite", () => {
it("should throw an error when an id does not exist", async () => {
const error = await service
.updateInvites([
{
id: "does-not-exist",
},
])
.catch((e) => e)
expect(error.message).toEqual('Invite with id "does-not-exist" not found')
})
it("should emit invite updated events", async () => {
await createInvites(testManager, defaultInviteData)
jest.clearAllMocks()
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.updateInvites([
{
id: "1",
accepted: true,
},
])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.invite_updated,
}),
])
})
})
describe("resendInvite", () => {
it("should emit token generated event for invites", async () => {
await createInvites(testManager, defaultInviteData)
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.refreshInviteTokens(["1"])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.invite_token_generated,
}),
])
})
})
describe("createInvitie", () => {
it("should create an invite successfully", async () => {
await service.createInvites(defaultInviteData)
const [invites, count] = await service.listAndCountInvites({
id: ["1"],
})
expect(count).toEqual(1)
expect(invites[0]).toEqual(
expect.objectContaining({
id: "1",
expect(invites).toEqual([
expect.objectContaining({
id: "1",
}),
])
})
)
})
})
it("should emit invite created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.createInvites(defaultInviteData)
describe("listAndCountInvites", () => {
it("should list and count invites", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
const [invites, count] = await service.listAndCountInvites()
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.invite_created,
}),
expect.objectContaining({
body: expect.objectContaining({
data: { id: "2" },
}),
eventName: UserEvents.invite_created,
}),
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.invite_token_generated,
}),
expect.objectContaining({
body: expect.objectContaining({
data: { id: "2" },
}),
eventName: UserEvents.invite_token_generated,
}),
])
expect(count).toEqual(2)
expect(invites).toEqual([
expect.objectContaining({
id: "1",
}),
expect.objectContaining({
id: "2",
}),
])
})
it("should listAndCount invites by id", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
const [invites, count] = await service.listAndCountInvites({
id: "1",
})
expect(count).toEqual(1)
expect(invites).toEqual([
expect.objectContaining({
id: "1",
}),
])
})
})
describe("retrieveInvite", () => {
const id = "1"
it("should return an invite for the given id", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
const invite = await service.retrieveInvite(id)
expect(invite).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when an invite with the given id does not exist", async () => {
const error = await service
.retrieveInvite("does-not-exist")
.catch((e) => e)
expect(error.message).toEqual(
"Invite with id: does-not-exist was not found"
)
})
it("should throw an error when inviteId is not provided", async () => {
const error = await service
.retrieveInvite(undefined as unknown as string)
.catch((e) => e)
expect(error.message).toEqual("invite - id must be defined")
})
it("should return invite based on config select param", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
const invite = await service.retrieveInvite(id, {
select: ["id"],
})
expect(invite).toEqual({
id,
})
})
})
describe("updateInvite", () => {
it("should throw an error when an id does not exist", async () => {
const error = await service
.updateInvites([
{
id: "does-not-exist",
},
])
.catch((e) => e)
expect(error.message).toEqual(
'Invite with id "does-not-exist" not found'
)
})
it("should emit invite updated events", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
jest.clearAllMocks()
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.updateInvites([
{
id: "1",
accepted: true,
},
])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.invite_updated,
}),
])
})
})
describe("resendInvite", () => {
it("should emit token generated event for invites", async () => {
await createInvites(MikroOrmWrapper.forkManager(), defaultInviteData)
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.refreshInviteTokens(["1"])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.invite_token_generated,
}),
])
})
})
describe("createInvitie", () => {
it("should create an invite successfully", async () => {
await service.createInvites(defaultInviteData)
const [invites, count] = await service.listAndCountInvites({
id: ["1"],
})
expect(count).toEqual(1)
expect(invites[0]).toEqual(
expect.objectContaining({
id: "1",
})
)
})
it("should emit invite created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.createInvites(defaultInviteData)
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.invite_created,
}),
expect.objectContaining({
body: expect.objectContaining({
data: { id: "2" },
}),
eventName: UserEvents.invite_created,
}),
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.invite_token_generated,
}),
expect.objectContaining({
body: expect.objectContaining({
data: { id: "2" },
}),
eventName: UserEvents.invite_token_generated,
}),
])
})
})
})
})
},
})
@@ -1,12 +1,9 @@
import { IUserModuleService } from "@medusajs/types/dist/user"
import { MikroOrmWrapper } from "../../../utils"
import { MockEventBusService } from "medusa-test-utils"
import { Modules } from "@medusajs/modules-sdk"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { UserEvents } from "@medusajs/utils"
import { createUsers } from "../../../__fixtures__/user"
import { getInitModuleConfig } from "../../../utils/get-init-module-config"
import { initModules } from "medusa-test-utils"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
jest.setTimeout(30000)
@@ -21,231 +18,224 @@ const defaultUserData = [
},
]
describe("UserModuleService - User", () => {
let service: IUserModuleService
let testManager: SqlEntityManager
let shutdownFunc: () => Promise<void>
beforeAll(async () => {
const initModulesConfig = getInitModuleConfig()
const { medusaApp, shutdown } = await initModules(initModulesConfig)
service = medusaApp.modules[Modules.USER]
shutdownFunc = shutdown
})
beforeEach(async () => {
await MikroOrmWrapper.setupDatabase()
testManager = MikroOrmWrapper.forkManager()
})
afterEach(async () => {
await MikroOrmWrapper.clearDatabase()
jest.clearAllMocks()
})
afterAll(async () => {
await shutdownFunc()
})
describe("list", () => {
it("should list users", async () => {
await createUsers(testManager, defaultUserData)
const users = await service.list()
expect(users).toEqual([
expect.objectContaining({
id: "1",
}),
expect.objectContaining({
id: "2",
}),
])
})
it("should list users by id", async () => {
await createUsers(testManager, defaultUserData)
const users = await service.list({
id: ["1"],
moduleIntegrationTestRunner({
moduleName: Modules.USER,
moduleOptions: {
jwt_secret: "test",
},
injectedDependencies: {
eventBusModuleService: new MockEventBusService(),
},
testSuite: ({
MikroOrmWrapper,
service,
medusaApp,
}: SuiteOptions<IUserModuleService>) => {
describe("UserModuleService - User", () => {
afterEach(async () => {
jest.clearAllMocks()
})
expect(users).toEqual([
expect.objectContaining({
id: "1",
}),
])
})
})
describe("list", () => {
it("should list users", async () => {
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
describe("listAndCount", () => {
it("should list and count users", async () => {
await createUsers(testManager, defaultUserData)
const [users, count] = await service.listAndCount()
const users = await service.list()
expect(count).toEqual(2)
expect(users).toEqual([
expect.objectContaining({
id: "1",
}),
expect.objectContaining({
id: "2",
}),
])
})
it("should list and count users by id", async () => {
await createUsers(testManager, defaultUserData)
const [Users, count] = await service.listAndCount({
id: "1",
})
expect(count).toEqual(1)
expect(Users).toEqual([
expect.objectContaining({
id: "1",
}),
])
})
})
describe("retrieve", () => {
const id = "1"
it("should return an user for the given id", async () => {
await createUsers(testManager, defaultUserData)
const user = await service.retrieve(id)
expect(user).toEqual(
expect.objectContaining({
id,
expect(users).toEqual([
expect.objectContaining({
id: "1",
}),
expect.objectContaining({
id: "2",
}),
])
})
)
})
it("should throw an error when an user with the given id does not exist", async () => {
const error = await service.retrieve("does-not-exist").catch((e) => e)
it("should list users by id", async () => {
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
const users = await service.list({
id: ["1"],
})
expect(error.message).toEqual(
"User with id: does-not-exist was not found"
)
})
it("should throw an error when a userId is not provided", async () => {
const error = await service
.retrieve(undefined as unknown as string)
.catch((e) => e)
expect(error.message).toEqual("user - id must be defined")
})
it("should return user based on config select param", async () => {
await createUsers(testManager, defaultUserData)
const User = await service.retrieve(id, {
select: ["id"],
})
const serialized = JSON.parse(JSON.stringify(User))
expect(serialized).toEqual({
id,
})
})
})
describe("delete", () => {
const id = "1"
it("should delete the users given an id successfully", async () => {
await createUsers(testManager, defaultUserData)
await service.delete([id])
const users = await service.list({
id: [id],
})
expect(users).toHaveLength(0)
})
})
describe("update", () => {
it("should throw an error when a id does not exist", async () => {
const error = await service
.update([
{
id: "does-not-exist",
},
])
.catch((e) => e)
expect(error.message).toEqual('User with id "does-not-exist" not found')
})
it("should emit user created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.create(defaultUserData)
jest.clearAllMocks()
await service.update([
{
id: "1",
first_name: "John",
},
])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.updated,
}),
])
})
})
describe("create", () => {
it("should create a user successfully", async () => {
await service.create(defaultUserData)
const [User, count] = await service.listAndCount({
id: ["1"],
})
expect(count).toEqual(1)
expect(User[0]).toEqual(
expect.objectContaining({
id: "1",
expect(users).toEqual([
expect.objectContaining({
id: "1",
}),
])
})
)
})
})
it("should emit user created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.create(defaultUserData)
describe("listAndCount", () => {
it("should list and count users", async () => {
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
const [users, count] = await service.listAndCount()
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.created,
}),
expect.objectContaining({
body: expect.objectContaining({
data: { id: "2" },
}),
eventName: UserEvents.created,
}),
])
expect(count).toEqual(2)
expect(users).toEqual([
expect.objectContaining({
id: "1",
}),
expect.objectContaining({
id: "2",
}),
])
})
it("should list and count users by id", async () => {
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
const [Users, count] = await service.listAndCount({
id: "1",
})
expect(count).toEqual(1)
expect(Users).toEqual([
expect.objectContaining({
id: "1",
}),
])
})
})
describe("retrieve", () => {
const id = "1"
it("should return an user for the given id", async () => {
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
const user = await service.retrieve(id)
expect(user).toEqual(
expect.objectContaining({
id,
})
)
})
it("should throw an error when an user with the given id does not exist", async () => {
const error = await service.retrieve("does-not-exist").catch((e) => e)
expect(error.message).toEqual(
"User with id: does-not-exist was not found"
)
})
it("should throw an error when a userId is not provided", async () => {
const error = await service
.retrieve(undefined as unknown as string)
.catch((e) => e)
expect(error.message).toEqual("user - id must be defined")
})
it("should return user based on config select param", async () => {
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
const User = await service.retrieve(id, {
select: ["id"],
})
const serialized = JSON.parse(JSON.stringify(User))
expect(serialized).toEqual({
id,
})
})
})
describe("delete", () => {
const id = "1"
it("should delete the users given an id successfully", async () => {
await createUsers(MikroOrmWrapper.forkManager(), defaultUserData)
await service.delete([id])
const users = await service.list({
id: [id],
})
expect(users).toHaveLength(0)
})
})
describe("update", () => {
it("should throw an error when a id does not exist", async () => {
const error = await service
.update([
{
id: "does-not-exist",
},
])
.catch((e) => e)
expect(error.message).toEqual(
'User with id "does-not-exist" not found'
)
})
it("should emit user created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.create(defaultUserData)
jest.clearAllMocks()
await service.update([
{
id: "1",
first_name: "John",
},
])
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.updated,
}),
])
})
})
describe("create", () => {
it("should create a user successfully", async () => {
await service.create(defaultUserData)
const [User, count] = await service.listAndCount({
id: ["1"],
})
expect(count).toEqual(1)
expect(User[0]).toEqual(
expect.objectContaining({
id: "1",
})
)
})
it("should emit user created events", async () => {
const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
await service.create(defaultUserData)
expect(eventBusSpy).toHaveBeenCalledTimes(1)
expect(eventBusSpy).toHaveBeenCalledWith([
expect.objectContaining({
body: expect.objectContaining({
data: { id: "1" },
}),
eventName: UserEvents.created,
}),
expect.objectContaining({
body: expect.objectContaining({
data: { id: "2" },
}),
eventName: UserEvents.created,
}),
])
})
})
})
})
},
})