docs: added to workflow legend + example improvements (#11895)

This commit is contained in:
Shahed Nasser
2025-03-19 08:31:28 +02:00
committed by GitHub
parent 9ead47c51e
commit 4827db98f7
19 changed files with 11339 additions and 11302 deletions
@@ -101,24 +101,25 @@ jest.setTimeout(60 * 1000)
## Test a POST API Route
Suppose you have a `hello` module whose main service extends the service factory, and that has the following model:
Suppose you have a `blog` module whose main service extends the service factory, and that has the following model:
```ts title="src/modules/hello/models/my-custom.ts"
```ts title="src/modules/blog/models/my-custom.ts"
import { model } from "@medusajs/framework/utils"
const MyCustom = model.define("my_custom", {
const Post = model.define("post", {
id: model.id().primaryKey(),
name: model.text(),
})
export default MyCustom
export default Post
```
And consider that the file `src/api/custom/route.ts` defines another route handler for `POST` requests:
```ts title="src/api/custom/route.ts"
// other imports...
import HelloModuleService from "../../../modules/hello/service"
import BlogModuleService from "../../../modules/blog/service"
import { BLOG_MODULE } from "../../../modules/blog"
// ...
@@ -126,21 +127,21 @@ export async function POST(
req: MedusaRequest,
res: MedusaResponse
) {
const helloModuleService: HelloModuleService = req.scope.resolve(
"helloModuleService"
const blogModuleService: BlogModuleService = req.scope.resolve(
BLOG_MODULE
)
const myCustom = await helloModuleService.createMyCustoms(
const post = await blogModuleService.createPosts(
req.body
)
res.json({
my_custom: myCustom,
post,
})
}
```
This API route creates a new record of `MyCustom`.
This API route creates a new record of `Post`.
To write tests for this API route, add the following at the end of the `testSuite` function in `integration-tests/http/custom-routes.spec.ts`:
@@ -150,7 +151,7 @@ export const postHighlights = [
```ts title="integration-tests/http/custom-routes.spec.ts" highlights={postHighlights}
// other imports...
import HelloModuleService from "../../src/modules/hello/service"
import BlogModuleService from "../../src/modules/blog/service"
medusaIntegrationTestRunner({
testSuite: ({ api, getContainer }) => {
@@ -171,8 +172,8 @@ medusaIntegrationTestRunner({
)
expect(response.status).toEqual(200)
expect(response.data).toHaveProperty("my_custom")
expect(response.data.my_custom).toEqual({
expect(response.data).toHaveProperty("post")
expect(response.data.post).toEqual({
id,
name: "Test",
created_at: expect.any(String),
@@ -190,7 +191,7 @@ This adds a test for the `POST /custom` API route. It uses `api.post` to send th
The test passes if the response has:
- Status code `200`.
- A `my_custom` property in its data.
- A `post` property in its data.
- Its `id` and `name` match the ones provided to the request.
### Tear Down Created Record
@@ -203,7 +204,8 @@ So, add an `afterAll` hook in the `describe` block for `POST /custom`:
```ts title="integration-tests/http/custom-routes.spec.ts"
// other imports...
import HelloModuleService from "../../src/modules/hello/service"
import BlogModuleService from "../../src/modules/blog/service"
import { BLOG_MODULE } from "../../src/modules/blog"
medusaIntegrationTestRunner({
testSuite: ({ api, getContainer }) => {
@@ -213,11 +215,11 @@ medusaIntegrationTestRunner({
describe("POST /custom", () => {
// ...
afterAll(() => async () => {
const helloModuleService: HelloModuleService = getContainer().resolve(
"helloModuleService"
const blogModuleService: BlogModuleService = getContainer().resolve(
BLOG_MODULE
)
await helloModuleService.deleteMyCustoms(id)
await blogModuleService.deletePosts(id)
})
})
})
@@ -225,7 +227,7 @@ medusaIntegrationTestRunner({
})
```
The `afterAll` hook resolves the `HelloModuleService` and use its `deleteMyCustoms` to delete the record created by the test.
The `afterAll` hook resolves the `BlogModuleService` and use its `deletePosts` to delete the record created by the test.
---
@@ -235,17 +237,18 @@ Consider a `/custom/:id` API route created at `src/api/custom/[id]/route.ts`:
```ts title="src/api/custom/[id]/route.ts"
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import HelloModuleService from "../../../modules/hello/service"
import BlogModuleService from "../../../modules/blog/service"
import { BLOG_MODULE } from "../../../modules/blog"
export async function DELETE(
req: MedusaRequest,
res: MedusaResponse
) {
const helloModuleService: HelloModuleService = req.scope.resolve(
"helloModuleService"
const blogModuleService: BlogModuleService = req.scope.resolve(
BLOG_MODULE
)
await helloModuleService.deleteMyCustoms(req.params.id)
await blogModuleService.deletePosts(req.params.id)
res.json({
success: true,
@@ -253,7 +256,7 @@ export async function DELETE(
}
```
This API route accepts an ID path parameter, and uses the `HelloModuleService` to delete a `MyCustom` record by that ID.
This API route accepts an ID path parameter, and uses the `BlogModuleService` to delete a `Post` record by that ID.
To add tests for this API route, add the following to `integration-tests/http/custom-routes.spec.ts`:
@@ -271,11 +274,11 @@ medusaIntegrationTestRunner({
const id = "1"
beforeAll(() => async () => {
const helloModuleService: HelloModuleService = getContainer().resolve(
"helloModuleService"
const blogModuleService: BlogModuleService = getContainer().resolve(
BLOG_MODULE
)
await helloModuleService.createMyCustoms({
await blogModuleService.createPosts({
id,
name: "Test",
})
@@ -296,7 +299,7 @@ medusaIntegrationTestRunner({
})
```
This adds a new test for the `DELETE /custom/:id` API route. You use the `beforeAll` hook to create a `MyCustom` record using the `HelloModuleService`.
This adds a new test for the `DELETE /custom/:id` API route. You use the `beforeAll` hook to create a `Post` record using the `BlogModuleService`.
In the test, you use the `api.delete` method to send a `DELETE` request to `/custom/:id`. The test passes if the response:
@@ -19,13 +19,13 @@ In this chapter, find an example of writing an integration test for a module usi
## Write Integration Test for Module
Consider a `hello` module with a `HelloModuleService` that has a `getMessage` method:
Consider a `blog` module with a `BlogModuleService` that has a `getMessage` method:
```ts title="src/modules/hello/service.ts"
```ts title="src/modules/blog/service.ts"
import { MedusaService } from "@medusajs/framework/utils"
import MyCustom from "./models/my-custom"
class HelloModuleService extends MedusaService({
class BlogModuleService extends MedusaService({
MyCustom,
}){
getMessage(): string {
@@ -33,23 +33,23 @@ class HelloModuleService extends MedusaService({
}
}
export default HelloModuleService
export default BlogModuleService
```
To create an integration test for the method, create the file `src/modules/hello/__tests__/service.spec.ts` with the following content:
To create an integration test for the method, create the file `src/modules/blog/__tests__/service.spec.ts` with the following content:
```ts title="src/modules/hello/__tests__/service.spec.ts"
```ts title="src/modules/blog/__tests__/service.spec.ts"
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
import { HELLO_MODULE } from ".."
import HelloModuleService from "../service"
import { BLOG_MODULE } from ".."
import BlogModuleService from "../service"
import MyCustom from "../models/my-custom"
moduleIntegrationTestRunner<HelloModuleService>({
moduleName: HELLO_MODULE,
moduleIntegrationTestRunner<BlogModuleService>({
moduleName: BLOG_MODULE,
moduleModels: [MyCustom],
resolve: "./src/modules/hello",
resolve: "./src/modules/blog",
testSuite: ({ service }) => {
describe("HelloModuleService", () => {
describe("BlogModuleService", () => {
it("says hello world", () => {
const message = service.getMessage()
@@ -62,7 +62,7 @@ moduleIntegrationTestRunner<HelloModuleService>({
jest.setTimeout(60 * 1000)
```
You use the `moduleIntegrationTestRunner` function to add tests for the `hello` module. You have one test that passes if the `getMessage` method returns the `"Hello, World!"` string.
You use the `moduleIntegrationTestRunner` function to add tests for the `blog` module. You have one test that passes if the `getMessage` method returns the `"Hello, World!"` string.
---
@@ -21,18 +21,18 @@ In this chapter, you'll learn about `moduleIntegrationTestRunner` from Medusa's
`moduleIntegrationTestRunner` creates integration tests for a module. The integration tests run on a test Medusa application with only the specified module enabled.
For example, assuming you have a `hello` module, create a test file at `src/modules/hello/__tests__/service.spec.ts`:
For example, assuming you have a `blog` module, create a test file at `src/modules/blog/__tests__/service.spec.ts`:
```ts title="src/modules/hello/__tests__/service.spec.ts"
```ts title="src/modules/blog/__tests__/service.spec.ts"
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
import { HELLO_MODULE } from ".."
import HelloModuleService from "../service"
import MyCustom from "../models/my-custom"
import { BLOG_MODULE } from ".."
import BlogModuleService from "../service"
import Post from "../models/post"
moduleIntegrationTestRunner<HelloModuleService>({
moduleName: HELLO_MODULE,
moduleModels: [MyCustom],
resolve: "./src/modules/hello",
moduleIntegrationTestRunner<BlogModuleService>({
moduleName: BLOG_MODULE,
moduleModels: [Post],
resolve: "./src/modules/blog",
testSuite: ({ service }) => {
// TODO write tests
},
@@ -86,9 +86,9 @@ For example:
```ts
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
import HelloModuleService from "../service"
import BlogModuleService from "../service"
moduleIntegrationTestRunner<HelloModuleService>({
moduleIntegrationTestRunner<BlogModuleService>({
moduleOptions: {
apiKey: "123",
},
@@ -106,14 +106,14 @@ For example:
```ts
import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
import HelloModuleService from "../service"
import BlogModuleService from "../service"
import { model } from "@medusajs/framework/utils"
const DummyModel = model.define("dummy_model", {
id: model.id().primaryKey(),
})
moduleIntegrationTestRunner<HelloModuleService>({
moduleIntegrationTestRunner<BlogModuleService>({
moduleModels: [DummyModel],
// ...
})