-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from victorsoares96/fix/fix-tests
🩹 fix: fix tests
- Loading branch information
Showing
15 changed files
with
352 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,8 @@ | |
{ | ||
"devDependencies": [ | ||
"**/*.spec.ts", | ||
"**/scripts/*.ts" | ||
"**/scripts/*.ts", | ||
"**/*.mock.ts" | ||
] | ||
} | ||
], | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { createFakeUser } from '@/mocks/user.mock'; | ||
import Database from './database'; | ||
import { User } from '@/entities/user.entity'; | ||
import { Session } from '@/entities/session.entity'; | ||
|
||
export const DEFAULT_USERS = [ | ||
createFakeUser({ | ||
id: '1', | ||
name: 'John Doe', | ||
email: '[email protected]', | ||
}), | ||
]; | ||
|
||
class MockedDatabase extends Database { | ||
public async clear(): Promise<void> { | ||
await this.dataSource.manager.delete(User, {}); | ||
await this.dataSource.manager.delete(Session, {}); | ||
} | ||
|
||
public async seed(): Promise<void> { | ||
await this.dataSource.manager.insert(User, DEFAULT_USERS); | ||
} | ||
|
||
public async findUser(id: string) { | ||
return this.dataSource.manager.findBy(User, { id }); | ||
} | ||
|
||
public async createUsers(count?: number, elements?: User[]): Promise<void> { | ||
const documents: User[] = [ | ||
...Array(count || 0) | ||
.fill(0) | ||
.map(() => createFakeUser()), | ||
]; | ||
|
||
await this.dataSource.manager.insert(User, elements || documents); | ||
} | ||
} | ||
|
||
export default MockedDatabase; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { DataSource } from 'typeorm'; | ||
import productionDataSource from './sources/production.source'; | ||
import stagingDataSource from './sources/staging.source'; | ||
import developmentDataSource from './sources/development.source'; | ||
|
||
class Database { | ||
public dataSource: DataSource; | ||
|
||
constructor() { | ||
this.dataSource = this.getDataSource(); | ||
} | ||
|
||
private getDataSource(): DataSource { | ||
const environment = process.env.NODE_ENV; | ||
|
||
if (environment === 'production') { | ||
return productionDataSource; | ||
} | ||
|
||
if (environment === 'staging') { | ||
return stagingDataSource; | ||
} | ||
return developmentDataSource; | ||
} | ||
|
||
public async connect(): Promise<DataSource> { | ||
return this.dataSource.initialize(); | ||
} | ||
|
||
public async disconnect(): Promise<void> { | ||
await this.dataSource.destroy(); | ||
} | ||
} | ||
|
||
export default Database; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import 'source-map-support/register'; | ||
import serverlessExpress from '@vendia/serverless-express'; | ||
import app from './app'; | ||
import Server from './server'; | ||
|
||
export const handler = serverlessExpress({ app: app.express }); | ||
const server = new Server(); | ||
export const handler = serverlessExpress({ app: server.express }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { faker } from '@faker-js/faker'; | ||
import { User } from '@/entities/user.entity'; | ||
|
||
export function createFakeUser(data?: Partial<User>): User { | ||
const createdAt = faker.date.past(); | ||
return { | ||
id: faker.string.numeric(5), | ||
name: faker.internet.displayName(), | ||
avatar: faker.internet.avatar(), | ||
createdAt, | ||
updatedAt: createdAt, | ||
deletionDate: null as never, | ||
username: faker.internet.userName(), | ||
email: faker.internet.email(), | ||
password: faker.internet.password(), | ||
...data, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.