Skip to content

Commit

Permalink
add posts test
Browse files Browse the repository at this point in the history
  • Loading branch information
susumutomita committed May 20, 2024
1 parent 02e18cd commit 35ce86c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
25 changes: 15 additions & 10 deletions __test__/posts.tests.ts → __test__/posts.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PrismaClient } from '@prisma/client';
import { POST as POST_USER } from '../src/app/api/users/route';
import { POST, GET } from '../src/app/api/posts/route';
import { GET as GET_ID, PUT, DELETE } from '../src/app/api/posts/[id]/route';

Expand All @@ -8,13 +9,17 @@ describe('Posts API', () => {
let userId: number;

beforeEach(async () => {
// テストごとにユーザーを作成し、そのIDを取得
const user = await prisma.user.create({
data: {
email: `testuser-${Date.now()}@example.com`,
name: 'Test User',
},
const requestBody = JSON.stringify({
email: `testuser-${Date.now()}@example.com`,
name: 'Test User',
});
const request = new Request('http://localhost:3000/api/users', {
method: 'POST',
body: requestBody,
});

const response = await POST_USER(request);
const user = await response.json();
userId = user.id;
});

Expand All @@ -32,7 +37,7 @@ describe('Posts API', () => {
title: 'Test Post',
body: 'This is a test post.',
tags: ['test', 'example'],
author_id: userId, // 作成したユーザーIDを使用
author_id: userId,
});
const request = new Request('http://localhost:3000/api/posts', {
method: 'POST',
Expand Down Expand Up @@ -60,7 +65,7 @@ describe('Posts API', () => {
title: 'Specific Post',
body: 'This is a specific post.',
tags: ['specific'],
author_id: userId, // 作成したユーザーIDを使用
author_id: userId,
});
const createRequest = new Request('http://localhost:3000/api/posts', {
method: 'POST',
Expand All @@ -86,7 +91,7 @@ describe('Posts API', () => {
title: 'Update Post',
body: 'This post will be updated.',
tags: ['update'],
author_id: userId, // 作成したユーザーIDを使用
author_id: userId,
});
const createRequest = new Request('http://localhost:3000/api/posts', {
method: 'POST',
Expand Down Expand Up @@ -124,7 +129,7 @@ describe('Posts API', () => {
title: 'Delete Post',
body: 'This post will be deleted.',
tags: ['delete'],
author_id: userId, // 作成したユーザーIDを使用
author_id: userId,
});
const createRequest = new Request('http://localhost:3000/api/posts', {
method: 'POST',
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
maxWorkers: 1,
preset: 'ts-jest',
coverageReporters: ['lcov'],
testEnvironment: 'node',
Expand Down

0 comments on commit 35ce86c

Please sign in to comment.