Skip to content

Commit

Permalink
test(cli): basic cli usage and exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidenke committed Oct 22, 2024
1 parent 059b83f commit 53eddfd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const jestConfig: JestConfigWithTsJest = {
extensionsToTreatAsEsm: ['.ts'],
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
moduleNameMapper: { '(.+)\\.js': '$1' },
modulePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/public/', '<rootDir>/tmp/'],
setupFiles: ['<rootDir>/jest.setup.ts'],
transform: {
'\\.[jt]s?$': ['ts-jest', { useESM: true }],
Expand Down
2 changes: 2 additions & 0 deletions public/examples/invalid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
colllections:
- name: noFields
32 changes: 32 additions & 0 deletions src/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { exec as _exec } from 'node:child_process';
import { promisify } from 'node:util';

const exec = promisify(_exec);
const cmd = 'node --loader ts-node/esm src/cli.ts';

describe('cli', () => {
it('throws an error if no config path is provided', async () => {
const runsOrFails = () => exec(`${cmd} -t path/to/target`);
await expect(runsOrFails).rejects.toThrow('Missing required argument: --config.');
});

it('throws an error if no target path is provided', async () => {
const runsOrFails = () => exec(`${cmd} -c path/to/config.yml`);
await expect(runsOrFails).rejects.toThrow('Missing required argument: --target.');
});

it('throws an error if the config path is invalid', async () => {
const runsOrFails = () => exec(`${cmd} -c path/to/config.yml -t tmp`);
await expect(runsOrFails).rejects.toThrow('Error: File not found: path/to/config.yml');
});

it('does not throw if the config can not be parsed', async () => {
const runsOrFails = () => exec(`${cmd} -c public/examples/invalid.yml -t tmp`);
await expect(runsOrFails()).resolves.toEqual({ stdout: '', stderr: '' });
});

it('loads and transforms a given config', async () => {
const { stdout } = await exec(`${cmd} -c public/examples/blog.yml -t tmp`);
expect(stdout).toEqual(expect.stringContaining('> blog schema written to'));
});
});
5 changes: 5 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@
"skipLibCheck": true,
"strict": true,
"target": "ESNext"
},
"ts-node": {
"experimentalSpecifierResolution": "node",
"transpileOnly": true,
"esm": true
}
}

0 comments on commit 53eddfd

Please sign in to comment.