Skip to content

Commit

Permalink
feat: third step to replace sequelize with prisma, the tests are now …
Browse files Browse the repository at this point in the history
…working
  • Loading branch information
AnthonyLzq committed Jul 31, 2023
1 parent 5db6de1 commit 474f8e0
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 297 deletions.
1 change: 0 additions & 1 deletion example/fastify-prisma/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const config: Config.InitialOptions = {
preset: 'ts-jest',
testEnvironment: 'node',
testTimeout: 1 * 60 * 1000,
globalSetup: './test/jestGlobalSetup.ts',
modulePaths: ['<rootDir>/src', '<rootDir>/node_modules'],
roots: ['.'],
moduleFileExtensions: ['js', 'json', 'ts'],
Expand Down
2 changes: 1 addition & 1 deletion example/fastify-prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"start": "node dist/index.js",
"release": "standard-version",
"test:ci": "jest --ci -i",
"test:local": "NODE_ENV=local jest --ci -i"
"test:local": "NODE_ENV=local jest --ci -i --setupFiles dotenv/config"
},
"author": "AnthonyLzq <[email protected]>",
"license": "MIT",
Expand Down
150 changes: 20 additions & 130 deletions example/fastify-prisma/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,12 @@ describe('Simba.js tests', () => {
let userID = 0

describe('API: GET /', () => {
let data: BaseResponseDTO

test('Should return 200 as status code', async () => {
it('Should return 200 with a successful operation', async () => {
const result = await axios.get<BaseResponseDTO>(BASE_URL)

data = result.data
expect(result.status).toBe(200)
})

test('Should be a successfully operation', () => {
expect(data.error).toBe(false)
})

test('Should be return baseResponseDto', () => {
expect(validator(baseResponseDto, data)).toBe(true)
expect(result.data.error).toBe(false)
expect(validator(baseResponseDto, result.data)).toBe(true)
})
})

Expand All @@ -63,57 +54,19 @@ describe('Simba.js tests', () => {

type StoreUserDTO = Static<typeof storeUserResponse>

let data: StoreUserDTO
let status: number

test('Should return 201 as status code', async () => {
it('Should create a user successfully', async () => {
const result = await axios.post<StoreUserDTO>(`${BASE_URL}/api/users`, {
args: {
lastName: 'Lzq',
name: 'Anthony'
}
})

data = result.data
status = result.status
userID = result.data.message.id ?? userID
expect(status).toBe(201)
})

test('Should be a successfully operation', () => {
expect(data.error).toBe(false)
})

test('Should return storeUserResponse', () => {
expect(validator(storeUserResponse, data)).toBe(true)
})
})

describe('API: GET /api/users', () => {
const getAllUsersResponse = Type.Object({
error: Type.Boolean(),
message: Type.Array(userDto)
})

type GetAllUsersDTO = Static<typeof getAllUsersResponse>

let data: GetAllUsersDTO
let status: number

test('Should return 200 as status code', async () => {
const result = await axios.get<GetAllUsersDTO>(`${BASE_URL}/api/users`)

data = result.data
status = result.status
expect(status).toBe(200)
})

test('Should be a successfully operation', () => {
expect(data.error).toBe(false)
})

test('Should return getAllUsersResponse', () => {
expect(validator(getAllUsersResponse, data)).toBe(true)
expect(userID).toBeTruthy()
expect(result.status).toBe(201)
expect(result.data.error).toBe(false)
expect(validator(storeUserResponse, result.data)).toBe(true)
})
})

Expand All @@ -125,25 +78,14 @@ describe('Simba.js tests', () => {

type GetOneUserDTO = Static<typeof getOneUserResponse>

let data: GetOneUserDTO
let status: number

test('Should return 200 as status code', async () => {
it('Should return a user', async () => {
const result = await axios.get<GetOneUserDTO>(
`${BASE_URL}/api/user/${userID}`
)

data = result.data
status = result.status
expect(status).toBe(200)
})

test('Should be a successfully operation', () => {
expect(data.error).toBe(false)
})

test('Should return getOneUserResponse', () => {
expect(validator(getOneUserResponse, data)).toBe(true)
expect(result.status).toBe(200)
expect(result.data.error).toBe(false)
expect(validator(getOneUserResponse, result.data)).toBe(true)
})
})

Expand All @@ -155,10 +97,7 @@ describe('Simba.js tests', () => {

type UpdateUserDTO = Static<typeof updateUserResponse>

let data: UpdateUserDTO
let status: number

test('Should return 200 as status code', async () => {
it('Should update a user successfully', async () => {
const result = await axios.patch<UpdateUserDTO>(
`${BASE_URL}/api/user/${userID}`,
{
Expand All @@ -169,70 +108,21 @@ describe('Simba.js tests', () => {
}
)

data = result.data
status = result.status
expect(status).toBe(200)
})

test('Should be a successfully operation', () => {
expect(data.error).toBe(false)
})

test('Should return updateUserResponse', () => {
expect(validator(updateUserResponse, data)).toBe(true)
expect(result.status).toBe(200)
expect(result.data.error).toBe(false)
expect(validator(updateUserResponse, result.data)).toBe(true)
})
})

describe('API: DELETE /api/user/:id', () => {
let data: BaseResponseDTO
let status: number

test('Should return 200 as status code', async () => {
it('Should delete the created user', async () => {
const result = await axios.delete<BaseResponseDTO>(
`${BASE_URL}/api/user/${userID}`
)

data = result.data
status = result.status
expect(status).toBe(200)
})

test('Should be a successfully operation', () => {
expect(data.error).toBe(false)
})

test('Should return deleteUserResponse', () => {
expect(validator(baseResponseDto, data)).toBe(true)
})
})

describe('API: DELETE /api/users', () => {
let data: BaseResponseDTO
let status: number

test('Should return 200 as status code', async () => {
await axios.post(`${BASE_URL}/api/users`, {
args: {
lastName: 'Lzq',
name: 'Anthony'
}
})

const result = await axios.delete<BaseResponseDTO>(
`${BASE_URL}/api/users`
)

data = result.data
status = result.status
expect(status).toBe(200)
})

test('Should be a successfully operation', () => {
expect(data.error).toBe(false)
})

test('Should return deleteAllUsersResponse', () => {
expect(validator(baseResponseDto, data)).toBe(true)
expect(result.status).toBe(200)
expect(result.data.error).toBe(false)
expect(validator(baseResponseDto, result.data)).toBe(true)
})
})
})
Expand Down
3 changes: 0 additions & 3 deletions example/fastify-prisma/test/jestGlobalSetup.ts

This file was deleted.

6 changes: 4 additions & 2 deletions lib/src/functions/api/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,11 @@ export { store, removeById, getById, update }\n`,
writeFile(database[db].queries.user.file, database[db].queries.user.content)
])

const prismaMigrate = 'npx prisma migrate dev --name init'
if (process.env.NODE_ENV === 'local') {
const prismaMigrate = 'npx prisma migrate dev --name init'

await exec(prismaMigrate, { cwd: projectName })
await exec(prismaMigrate, { cwd: projectName })
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/src/functions/packageJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = async ({
"start": "node dist/index.js",
"release": "standard-version",
"test:ci": "jest --ci -i",
"test:local": "NODE_ENV=local jest --ci -i"
"test:local": "NODE_ENV=local jest --ci -i --setupFiles dotenv/config"
},
"author": "${author}",${
license !== 'unlicensed'
Expand Down
Loading

0 comments on commit 474f8e0

Please sign in to comment.