The tests are based on the [Jest](https://jestjs.io/) framework. To facilitate writing tests, `mockDatabase()` and `mockServer()` are provided for database and server-side application testing.
## `mockDatabase()`
provides a fully isolated db testing environment by default
```ts
import { mockDatabase } from '@nocobase/test';
describe('my db suite', () => {
let db;
beforeEach(async () => {
db = mockDatabase();
db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
}
]
});
await db.sync();
});
afterEach(async () => {
await db.close();
});
test('my case', async () => {
const repository = db.getRepository('posts');
const post = await repository.create({
values: {
title: 'hello'
}
});
expect(post.get('title')).toEqual('hello');
});
});
```
## `mockServer()`
provides a mock server-side application instance, corresponding to app.db as a `mockDatabase()` instance, and also provides a convenient `app.agent()` for testing the HTTP API, and wraps `app.agent().resource()` for the Resource Action of NocoBase for testing the Action.
Once finished, allow the test command on the command line:
```bash
yarn test packages/samples/shop-actions
```
This test will verify that:
1. products can be created successfully.
2. orders can be created successfully.
3. orders can be shipped successfully.
Of course this is just a minimal example, not perfect business-wise, but as an example it can already illustrate the whole testing process.
## Summary
The sample code covered in this chapter is integrated in the corresponding package [packages/samples/shop-actions](https://github.com/nocobase/nocobase/tree/main/packages/samples/shop-actions), which can be run directly locally to see the results.