From afcaad572fe14a9ced33e75436b52a179054c106 Mon Sep 17 00:00:00 2001 From: Muhammed Mustafa Date: Wed, 21 Sep 2022 11:50:41 +0530 Subject: [PATCH] fix(nocodb): Added env configuration to unit test and added that to docs as well --- .../content/en/engineering/testing.md | 66 +++++++++++++------ packages/nocodb/.gitignore | 3 +- packages/nocodb/tests/unit/.env.copy | 4 ++ packages/nocodb/tests/unit/index.test.ts | 5 ++ 4 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 packages/nocodb/tests/unit/.env.copy diff --git a/packages/noco-docs/content/en/engineering/testing.md b/packages/noco-docs/content/en/engineering/testing.md index 0111ad9e70..ec6733da16 100644 --- a/packages/noco-docs/content/en/engineering/testing.md +++ b/packages/noco-docs/content/en/engineering/testing.md @@ -1,46 +1,70 @@ --- -title: "Writing Tests" -description: "Overview to testing" +title: "Writing Unit Tests" +description: "Overview to unit testing" position: 3250 category: "Engineering" -menuTitle: "Testing" +menuTitle: "Unit tests" --- -## API Tests +## Pre-requisites -### Pre-requisites - MySQL is preferrable - however we fallback to SQLite -### Setup +## Setup + +- All the tests are in `packages/nocodb` folder, which will be our working directory. Use the following command to get into that folder. + +```bash +cd packages/nocodb ``` -cp scripts/.env.copy scripts/.env -open scripts/.env - -# Edit the following env variables -# `DB_USER` : mysql username -# `DB_PASSWORD` : mysql password -# `DB_HOST` : mysql host -# `DB_PORT` : mysql port + +- Install the dependencies for `nocodb` package + +```bash +npm install ``` -### Running tests +### Environment variables + +- Add your `env` file with the following command + ``` -cd packages/nocodb +cp tests/unit/.env.copy tests/unit/.env +``` + +- Open the `.env` file + +``` +open tests/unit/.env +```` + +- Configure the following variables + +> DB_USER : mysql username
+> DB_PASSWORD : mysql password
+> DB_HOST : mysql host
+> DB_PORT : mysql port
+ + + +## How to run tests + +``` + npm run test:unit ``` -### Notes +## Key points -#### Key points - All individual unit tests are independent of each other. We don't use any shared state between tests. - 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 -We will create an `Table` test suite as an example. +### 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.). diff --git a/packages/nocodb/.gitignore b/packages/nocodb/.gitignore index 7d6c6a2d14..3fbe40644f 100644 --- a/packages/nocodb/.gitignore +++ b/packages/nocodb/.gitignore @@ -17,4 +17,5 @@ noco.db* /nc/ /docker/main.js test_meta.db -test_sakila.db \ No newline at end of file +test_sakila.db +.env \ No newline at end of file diff --git a/packages/nocodb/tests/unit/.env.copy b/packages/nocodb/tests/unit/.env.copy new file mode 100644 index 0000000000..f3d7072553 --- /dev/null +++ b/packages/nocodb/tests/unit/.env.copy @@ -0,0 +1,4 @@ +DB_USER=root +DB_PASSWORD=password +DB_PORT=3306 +DB_HOST=localhost \ No newline at end of file diff --git a/packages/nocodb/tests/unit/index.test.ts b/packages/nocodb/tests/unit/index.test.ts index dd0a36273b..2e7d0d2edc 100644 --- a/packages/nocodb/tests/unit/index.test.ts +++ b/packages/nocodb/tests/unit/index.test.ts @@ -3,12 +3,17 @@ import 'mocha'; import restTests from './rest/index.test'; import modelTests from './model/index.test'; import TestDbMngr from './TestDbMngr' +import dotenv from 'dotenv'; process.env.NODE_ENV = 'test'; process.env.TEST = 'test'; process.env.NC_DISABLE_CACHE = 'true'; process.env.NC_DISABLE_TELE = 'true'; +// Load environment variables from .env file +dotenv.config({ + path: __dirname + '/.env' +}); (async function() { await TestDbMngr.init();