From 28812d8745fc0f36893f29efd83a530875c9b3ee Mon Sep 17 00:00:00 2001 From: rfprod Date: Sat, 6 Jan 2024 21:58:23 +0200 Subject: [PATCH] test(tools): increase unit test coverage - [x] add test cases to increase unit test coverage of executors; --- tools/executors/prettier/check.spec.ts | 47 +++++++++++++++++++++++-- tools/executors/stylelint/check.spec.ts | 47 +++++++++++++++++++++++-- tools/jest.config.ts | 6 ++-- 3 files changed, 91 insertions(+), 9 deletions(-) diff --git a/tools/executors/prettier/check.spec.ts b/tools/executors/prettier/check.spec.ts index 47c6f9e9..d4bc906f 100644 --- a/tools/executors/prettier/check.spec.ts +++ b/tools/executors/prettier/check.spec.ts @@ -45,7 +45,7 @@ describe('check', () => { return { context, options }; }; - describe('errors, no projectName', () => { + describe('errors', () => { afterEach(() => jest.clearAllMocks()); it('should throw an error if context.projectName is undefined', async () => { @@ -64,12 +64,53 @@ describe('check', () => { expect((e).message).toEqual('Project name is not defined.'); } }); + + it('should throw an error if context.projectName does not exist', async () => { + const { context, options } = setup('does-not-exist'); + + try { + const result = await check(options, context); + expect(result).not.toMatchObject({ success: true }); + } catch (e) { + expect(childProcess.execFileSync).not.toHaveBeenCalledWith('npx', expect.any(Array), { + stdio: 'inherit', + cwd: process.cwd(), + env: process.env, + shell: true, + }); + expect((e).message).toEqual('Project does not exist.'); + } + }); + + it("should throw an error if a project's sourceRoot is undefined", async () => { + const { context, options } = setup('test'); + + const workspace = context.workspace; + Object.keys(workspace?.projects ?? {}).map(key => { + if (typeof workspace !== 'undefined') { + workspace.projects[key].sourceRoot = void 0; + } + }); + + try { + const result = await check(options, context); + expect(result).not.toMatchObject({ success: true }); + } catch (e) { + expect(childProcess.execFileSync).not.toHaveBeenCalledWith('npx', expect.any(Array), { + stdio: 'inherit', + cwd: process.cwd(), + env: process.env, + shell: true, + }); + expect((e).message).toEqual('Project root does not exist.'); + } + }); }); - describe('errors, no source directory', () => { + describe('no errors', () => { afterEach(() => jest.clearAllMocks()); - it('should throw an error if source directory does not exist', async () => { + it('should throw an error if a source directory does not exist', async () => { const { context, options } = setup('test'); try { diff --git a/tools/executors/stylelint/check.spec.ts b/tools/executors/stylelint/check.spec.ts index 47c6f9e9..1337a44b 100644 --- a/tools/executors/stylelint/check.spec.ts +++ b/tools/executors/stylelint/check.spec.ts @@ -45,7 +45,7 @@ describe('check', () => { return { context, options }; }; - describe('errors, no projectName', () => { + describe('errors: projectName', () => { afterEach(() => jest.clearAllMocks()); it('should throw an error if context.projectName is undefined', async () => { @@ -64,12 +64,53 @@ describe('check', () => { expect((e).message).toEqual('Project name is not defined.'); } }); + + it('should throw an error if context.projectName does not exist', async () => { + const { context, options } = setup('does-not-exist'); + + try { + const result = await check(options, context); + expect(result).not.toMatchObject({ success: true }); + } catch (e) { + expect(childProcess.execFileSync).not.toHaveBeenCalledWith('npx', expect.any(Array), { + stdio: 'inherit', + cwd: process.cwd(), + env: process.env, + shell: true, + }); + expect((e).message).toEqual('Project does not exist.'); + } + }); + + it("should throw an error if a project's sourceRoot is undefined", async () => { + const { context, options } = setup('test'); + + const workspace = context.workspace; + Object.keys(workspace?.projects ?? {}).map(key => { + if (typeof workspace !== 'undefined') { + workspace.projects[key].sourceRoot = void 0; + } + }); + + try { + const result = await check(options, context); + expect(result).not.toMatchObject({ success: true }); + } catch (e) { + expect(childProcess.execFileSync).not.toHaveBeenCalledWith('npx', expect.any(Array), { + stdio: 'inherit', + cwd: process.cwd(), + env: process.env, + shell: true, + }); + expect((e).message).toEqual('Project root does not exist.'); + } + }); }); - describe('errors, no source directory', () => { + describe('no errors', () => { afterEach(() => jest.clearAllMocks()); - it('should throw an error if source directory does not exist', async () => { + it('should throw an error if a source directory does not exist', async () => { const { context, options } = setup('test'); try { diff --git a/tools/jest.config.ts b/tools/jest.config.ts index 8596fbfc..344946ed 100644 --- a/tools/jest.config.ts +++ b/tools/jest.config.ts @@ -4,10 +4,10 @@ const config: Config.InitialOptions = { coverageDirectory: '../coverage/tools', coverageThreshold: { global: { - branches: 69, + branches: 71, functions: 79, - lines: 81, - statements: 80, + lines: 82, + statements: 81, }, }, displayName: 'tools',