Comments (5)
Hi @PatrickDorneles ,
The best way to achieve this is to use a test database and add some fake data before the test. Here's an example with a SQLite db:
describe('UserService', () => {
let service: UserService;
let connection: Connection;
// Create a connection to the database before running all the tests.
before(async () => {
connection = await createConnection({
// Choose a test database. You don't want to run your tests on your production data.
database: './test_db.sqlite3',
// Drop the schema when the connection is established.
dropSchema: true,
// Register the models that are used.
entities: [ User ],
// Auto create the database schema.
synchronize: true,
// Specify the type of database.
type: 'sqlite',
});
const user = new User();
user.name = 'Mary';
await connection.manager.save(user);
});
// Close the database connection after running all the tests whether they succeed or failed.
after(() => connection.close());
it('should have a getUsers method which returns the users of the database', async () => {
service = createService(UserService);
const users = await service.getUsers();
strictEqual(users.length, 1);
strictEqual(users[0].name, 'Mary');
});
});
Note in this example that if another service (or a controller) uses the UserController
, you can test it by mocking the UserService
:
// my-other.service.ts
class MyOtherService {
@dependency
userService: UserService
}
// my-other.service.spec.ts
class MockUserService {
async getUsers() {
return [];
}
}
...
myOtherService = createService(MyOtherService, { userService: MockUserService });
...
Does it help?
from foal.
My bad, your example was to register a user, not getting them. It's the same logic though
from foal.
The dropSchema
option lets you empty your database when establishing the connection (see https://github.com/typeorm/typeorm/blob/master/docs/connection-options.md).
from foal.
connection.getRepository(User).clear()
may also be useful if you have several tests.
From the comments of TypeORM:
/**
* Clears all the data from the given table/collection (truncates/drops it).
*
* Note: this method uses TRUNCATE and may not work as you expect in transactions on some platforms.
* @see https://stackoverflow.com/a/5972738/925151
*/
clear(): Promise<void>;
from foal.
Hi @PatrickDorneles,
Did it solve your problem? Can I close this?
Thanks!
from foal.
Related Issues (20)
- Compatibility issues between mongo store and node 19.1.0 (alpine) HOT 2
- AJV @ValidateBody schema regression in FoalTS 3.0 HOT 1
- @foal/password HOT 3
- Error with FOAL CONNECT for angular>13 (project) : foal connect angular frontend_dir HOT 2
- Multiple mysql "active" connections HOT 2
- Question: body size limits HOT 1
- Receiving xml in request body HOT 3
- How to get the response time and log it using winston logger in foalts HOT 1
- JWT support for SocketIO HOT 1
- Allow ajv strict option HOT 2
- TypeError: Class constructor SocialPost cannot be invoked without 'new' HOT 2
- Task Scheduling In App HOT 2
- SSR with built-in templates not working HOT 2
- Possibility to set option to static serving (via express.static) HOT 3
- Version 4 HOT 1
- a yarn on ubuntu 23 HOT 3
- Question : static access to Context outside a controller HOT 1
- foal: not found HOT 2
- It seems when setting FOAL_ENV=production, cookies setings in production.json of config folder not applied HOT 2
- [Proposal] Give full control of express response object for user
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from foal.