diff --git a/jest.config.ts b/jest.config.ts index 5e1d596..17d7081 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -6,6 +6,7 @@ const jestConfig: JestConfigWithTsJest = { extensionsToTreatAsEsm: ['.ts'], moduleFileExtensions: ['ts', 'js', 'json', 'node'], moduleNameMapper: { '(.+)\\.js': '$1' }, + modulePathIgnorePatterns: ['/dist/', '/public/', '/tmp/'], setupFiles: ['/jest.setup.ts'], transform: { '\\.[jt]s?$': ['ts-jest', { useESM: true }], diff --git a/public/examples/invalid.yml b/public/examples/invalid.yml new file mode 100644 index 0000000..20ededc --- /dev/null +++ b/public/examples/invalid.yml @@ -0,0 +1,2 @@ +colllections: + - name: noFields diff --git a/src/cli.spec.ts b/src/cli.spec.ts new file mode 100644 index 0000000..65bb582 --- /dev/null +++ b/src/cli.spec.ts @@ -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')); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index d29d230..6845fbb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,5 +9,10 @@ "skipLibCheck": true, "strict": true, "target": "ESNext" + }, + "ts-node": { + "experimentalSpecifierResolution": "node", + "transpileOnly": true, + "esm": true } }