- Test environment includes `sakila` sample database and any change to it by a test is reverted before running other tests.
- While running unit tests, it tries to connect to mysql server running on `localhost:3306` with username `root` and password `password`(which can be configured) and if not found, it will use `sqlite` as a fallback, hence no requirement of any sql server to run tests.
### Walk through of writing a unit test
## Walk through of writing a unit test
We will create an `Table` test suite as an example.
#### Configure test
### Configure test
We will configure `beforeEach` which is called before each test is executed. We will use `init` function from `nocodb/packages/tests/unit/init/index.ts`, which is a helper function which configures the test environment(i.e resetting state, etc.).
@ -88,7 +88,7 @@ beforeEach(async function () {
});
```
##### Test case
### Test case
We will use `it` function to create a test case. We will use `supertest` to make a request to the server. We use `expect`(`chai`) to assert the response.
> NOTE: We can also run individual test by using `.only` in `describe` or `it` function and the running the test command.
```typescript
it.only('Get table list', async () => {
```
### Integrating the new test suite
We create a new file `table.test.ts` in `packages/nocodb/tests/unit/rest/tests` directory.
@ -148,17 +154,7 @@ export default function () {
We can then import the `Table` test suite to `Rest` test suite in `packages/nocodb/tests/unit/rest/index.test.ts` file(`Rest` test suite is imported in the root test suite file which is `packages/nocodb/tests/unit/index.test.ts`).
#### Running test
To run tests, run `npm run test:unit` in `packages/nocodb` directory.
> NOTE: We can also run individual test by using `.only` in `describe` or `it` function and the running the test command.
```typescript
it.only('Get table list', async () => {
```
#### Folder structure
## Folder structure
The root folder for unit tests is `packages/tests/unit`
@ -169,7 +165,7 @@ The root folder for unit tests is `packages/tests/unit`
- `index.test.ts` is the root test suite file which imports all the test suites.
- `TestDbMngr.ts` is a helper class to manage test databases (i.e. creating, dropping, etc.).
#### Patterns to follow
## Patterns to follow
- **Factories**
- Use factories for create/update/delete data. No data should be directly create/updated/deleted in the test.
@ -182,10 +178,35 @@ The root folder for unit tests is `packages/tests/unit`
- Use one file per factory.
#### Using sakila db
## Using sakila db
To use sakila db use `createSakilaProject` from `factory/project` to create a project. This project will be seeded with `sakila` tables.