Files
directus/api/src/cli/index.test.ts
Jay Cammarano a7b0cbe69b GitHub workflow to run tests on pushes to main and PRs (#8247)
* testing workflow

* name changed

* generateHash working

* changed pendingMock() to 0 because ticks > 5000

* removed empty tests

* removed empty tests

* Update packages/sdk/tests/utils.ts

Co-authored-by: José Varela <joselcvarela@gmail.com>

* updated workflow to add build

* added npm run build

* fixed get-filter-operators

* added env to workflow

* potential fix to index.test.ts

* added env variables for index.test.ts

* added caching and node-version matrix

* added all supported node versions

* only supported node versions

* updated to only support 16

* removed duplicate workflow caching

Co-authored-by: José Varela <joselcvarela@gmail.com>
2021-09-27 15:10:29 -04:00

68 lines
1.8 KiB
TypeScript

import { Command } from 'commander';
import { Extension } from '@directus/shared/types';
import { createCli } from '.';
jest.mock('../env', () => ({
...jest.requireActual('../env').default,
EXTENSIONS_PATH: '',
SERVE_APP: false,
DB_CLIENT: 'pg',
DB_HOST: 'localhost',
DB_PORT: 5432,
DB_DATABASE: 'directus',
DB_USER: 'postgres',
DB_PASSWORD: 'psql1234',
}));
jest.mock('@directus/shared/utils/node/get-extensions', () => ({
getPackageExtensions: jest.fn(() => Promise.resolve([])),
getLocalExtensions: jest.fn(() => Promise.resolve([customCliExtension])),
}));
jest.mock(`/hooks/custom-cli/index.js`, () => () => customCliHook, { virtual: true });
const customCliExtension: Extension = {
path: `/hooks/custom-cli`,
name: 'custom-cli',
type: 'hook',
entrypoint: 'index.js',
local: true,
root: true,
};
const beforeHook = jest.fn();
const afterAction = jest.fn();
const afterHook = jest.fn(({ program }: { program: Command }) => program.command('custom').action(afterAction));
const customCliHook = { 'cli.init.before': beforeHook, 'cli.init.after': afterHook };
const writeOut = jest.fn();
const writeErr = jest.fn();
const setup = async () => {
const program = await createCli();
program.exitOverride();
program.configureOutput({ writeOut, writeErr });
return program;
};
beforeEach(jest.clearAllMocks);
describe('cli hooks', () => {
test('should call hooks before and after creating the cli', async () => {
const program = await setup();
expect(beforeHook).toHaveBeenCalledTimes(1);
expect(beforeHook).toHaveBeenCalledWith({ program });
expect(afterHook).toHaveBeenCalledTimes(1);
expect(afterHook).toHaveBeenCalledWith({ program });
});
test('should be able to add a custom cli command', async () => {
const program = await setup();
program.parseAsync(['custom'], { from: 'user' });
expect(afterAction).toHaveBeenCalledTimes(1);
});
});