Skip to content

Commit

Permalink
feat: add withProject to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Feb 3, 2023
1 parent 4c8998c commit ec29da1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ describe('The CLI configuration in default mode', () => {
beforeEach(async () => {
projectSandbox = await CliProjectFactory.create(cfg);
});
afterEach(async () => {
projectSandbox = await CliProjectFactory.teardown(cfg);
});

it('should work', async () => {
const { exitCode, stdout, stderr } = await projectSandbox.exec();
Expand Down Expand Up @@ -83,6 +86,9 @@ describe('The CLI', () => {
beforeEach(async () => {
projectSandbox = await CliProjectFactory.create(cfg);
});
afterEach(async () => {
projectSandbox = await CliProjectFactory.teardown(cfg);
});

it('should work', async () => {
const { exitCode, stdout, stderr } = await projectSandbox.exec();
Expand All @@ -94,6 +100,42 @@ describe('The CLI', () => {
});
```

### Use setup helper

As is it kind of repetitive to set up `beforeEach` and `afterEach` this library provides a helper.
This comes in handy when there are many different setups in one describe block.

The helper consumes a configuration for the project and handles setup and teardown internally.

```ts
import { ProjectConfig, withProject } from '@push-based/node-cli-testing/cli-project';

const cfg: ProjectConfig = {
root: './',
bin: 'cli.js'
};
const cfg2: ProjectConfig = {
root: './other/',
bin: 'cli.js'
};

describe('The CLI', () => {

it('should work in version 1', withProject(cfg, async (prj) => {
const { exitCode } = await prj.exec();
expect(exitCode).toBe(0);
})
);

it('should work in version 2', withProject(cfg2, async (prj) => {
const { exitCode } = await prj.exec();
expect(exitCode).toBe(0);
})
);

});
```

### Use process arguments

Node processes can retrieve arguments over `process.argv` to be configurable.
Expand Down
9 changes: 9 additions & 0 deletions packages/node-cli-testing/cli-project/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from 'fs';
import { ProcessParams } from './types';
import * as path from 'path';
import {CliProjectFactory} from "./factory";

export function getFolderContent(folders: string[]): string[] {
return folders.flatMap((d) => {
Expand Down Expand Up @@ -45,3 +46,11 @@ export function processParamsToParamsArray(params: ProcessParams): string[] {
}) as string[];
}

export function withProject<T extends { }>(cfg: any, fn: (prj: unknown) => Promise<void>): () => Promise<void> {
return async () => {
let prj = await CliProjectFactory.create<T>(cfg);
await prj.setup();
await fn(prj).finally(prj.teardown);
}
}

0 comments on commit ec29da1

Please sign in to comment.