Skip to content

Commit

Permalink
test(tools): increase unit test coverage (#810)
Browse files Browse the repository at this point in the history
- [x] add test cases to increase unit test coverage of executors;
  • Loading branch information
rfprod authored Jan 6, 2024
1 parent daa9566 commit 6c87d49
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
47 changes: 44 additions & 3 deletions tools/executors/prettier/check.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -64,12 +64,53 @@ describe('check', () => {
expect((<Error>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((<Error>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((<Error>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 {
Expand Down
47 changes: 44 additions & 3 deletions tools/executors/stylelint/check.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -64,12 +64,53 @@ describe('check', () => {
expect((<Error>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((<Error>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((<Error>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 {
Expand Down
6 changes: 3 additions & 3 deletions tools/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 6c87d49

Please sign in to comment.