Table of contents
Testing schema
Tests Location: /api/tests/ Mirror the api folder structure.
Tests Naming convention: file-name.test.ts where file-name is the name of the file being tested.
Mock Location: According to Jest documentation mocks must go in a folder called mocks in the same directory as the file you want to mock, not the test file.
Mocking the Database and Knex
You can mock the database and knex with the knex-mock-client package. knex-mock-client allows you to test your queries and provide a response. It should be used for validating queries. It also comes in handy when mocking getDatabase() and other modules as it can be combined with Jest.fn() for a deep mock.
Example of mocking getDatabase()
import getDatabase from '../../../src/database/index';
jest.mock('getDatabase', () => {
return knex({ client: MockClient });
});
someFunctionThatUsesGetDatabaseOnce()
expect(getDatabase()).toBeCalledTimes(1)
Example of deep mock using database/migrations/run
describe('run', () => {
let db: jest.Mocked<Knex>;
let tracker: Tracker;
beforeAll(() => {
db = knex({ client: MockClient }) as jest.Mocked<Knex>;
tracker = getTracker();
});
afterEach(() => {
tracker.reset();
});
describe('when passed the argument up', () => {
it('returns "Nothing To Updage" if no directus_migrations', async () => {
tracker.on.select('directus_migrations').response(['Empty']);
await run(db, 'up').catch((e: Error) => {
expect(e).toBeInstanceOf(Error);
expect(e.message).toBe('Nothing to upgrade');
});
});
});
});
See knex-mock-client - npm for full documentation and usage examples
- Home
- Debugging
- Filters
- Field Transforms
- API
- App
- App Components
- transition-bounce
- transition-dialog
- transition-expand
- v-avatar
- v-badge
- v-breadcrumb
- v-button
- v-card-actions
- v-card-subtitle
- v-card-text
- v-card-title
- v-card
- v-checkbox
- v-chip
- v-detail
- v-dialog
- v-divider
- v-drawer
- v-error
- v-fancy-select
- v-field-select
- v-field-template
- v-form
- v-hover
- v-icon
- v-info
- v-input
- v-item-group
- v-item
- v-list-group
- v-list-item-content
- v-list-item-hint
- v-list-item-icon
- v-list-item
- v-list
- v-menu
- v-notice
- v-overlay
- v-pagination
- v-progress-circular
- v-progress-linear
- v-radio
- v-select
- v-sheet
- v-skeleton-loader
- v-slider
- v-switch
- v-tab-item
- v-tab
- v-table
- v-tabs-items
- v-tabs
- v-text-overflow
- v-textarea
- v-upload
These are reference documents on how Directus works on the inside. Unless you're contributing to the platform, you shouldn't need this.
Please see https://docs.directus.io for the usage guide.