Skip to content

Commit

Permalink
fix: Vitest issues with coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kevintyj committed Jun 26, 2024
1 parent b5acd01 commit 3b10115
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ PNPM and node is used to install necessary dependencies, then config-conventiona
When using the above configuration, `pnpm-lock.yaml` is required. Please use npm and node if `package-lock.json` is used.

## v2 release

> [!CAUTION]
> v2.0.0 release is an unstable breaking build. v2.0.0 is known to have issues with
> **remote action run**. You will still be able to run v2.0.0 as a local action.

----

**This new major version of prlint updates commitlint package to the new v19.
This update removes support for CJS and only exports the app as a ESM package
(this should not affect the way you use this plugin in any way as the Github
Expand Down
20 changes: 16 additions & 4 deletions __tests__/errHandle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,48 @@ describe('error handler', () => {
beforeEach(() => {
vi.clearAllMocks();
});

it('simple string error calls error and fail once', () => {
handleError('error');
expect(debugErr).toBeCalledWith('error');
expect(debugErr).toBeCalledTimes(1);
expect(debugFail).toBeCalledWith('error');
expect(debugFail).toBeCalledTimes(1);
});

it('simple string warning calls error once but does not block', () => {
handleError('error', false);
expect(debugErr).toBeCalledWith('error');
expect(debugErr).toBeCalledTimes(1);
expect(debugFail).toBeCalledTimes(0);
});

it('class error calls error and fail once', () => {
handleError(new Error('error'));
expect(debugErr).toBeCalledWith(new Error('error'));
const errorInstance = new Error('error');
// eslint-disable-next-line ts/no-unsafe-return
expect(() => handleError(errorInstance)).not.toThrow();
expect(debugErr).toBeCalledWith(errorInstance);
expect(debugErr).toBeCalledTimes(1);
expect(debugFail).toBeCalledWith(`Error Name: ${errorInstance.name} \nMessage: ${errorInstance.message} \nStack: ${errorInstance.stack}`);
expect(debugFail).toBeCalledTimes(1);
});

it('class error warning calls error once but does not block', () => {
handleError(new Error('error'), false);
expect(debugErr).toBeCalledWith(new Error('error'));
const errorInstance = new Error('error');
handleError(errorInstance, false);
expect(debugErr).toBeCalledWith(errorInstance);
expect(debugErr).toBeCalledTimes(1);
expect(debugFail).toBeCalledTimes(0);
});

it('unknown error calls error and fail once', () => {
handleError(false);
expect(debugErr).toBeCalledWith('Unknown error has occurred!');
expect(debugErr).toBeCalledTimes(1);
expect(debugFail).toBeCalledWith('Unknown error has occurred!');
expect(debugFail).toBeCalledTimes(1);
});

it('unknown error warning calls error once but does not block', () => {
handleError(false, false);
expect(debugErr).toBeCalledWith('Unknown error has occurred!');
Expand Down
6 changes: 4 additions & 2 deletions __tests__/lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ describe('handler', async () => {

it('test valid config', () => {
expect(loadCommitLintConfig()).resolves.not.toThrow;
expect(loadCommitLintConfig('node')).resolves.not.toThrow;
});

it('test failing config', () => {
vi.spyOn(process, 'cwd').mockReturnValue('/tmp');
expect(loadCommitLintConfig()).toThrow;
vi.spyOn(process, 'cwd').mockReturnValue('/text-tmp');
expect(loadCommitLintConfig('node')).toThrow;
expect(loadCommitLintConfig('ignore')).toThrow;
});
});
13 changes: 3 additions & 10 deletions src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,14 @@ async function loadCommitLintConfig(downloadConfig: downloadOptions) {
try {
return await load({});
}
/* v8 ignore next 3 */
/* v8 ignore next 8 */
catch (err) {
const missingPackage = extractPackageNameFromError(err instanceof Error ? err.message : '');
if (missingPackage != null && downloadConfig !== 'ignore') {
try {
await execPromise(`npm install ${missingPackage} --omit=dev --legacy-peer-deps`);
}
catch (err) {
handleError(err);
}
await execPromise(`npm install ${missingPackage} --omit=dev --legacy-peer-deps`).catch(handleError);
return loadCommitLintConfig(downloadConfig);
}
else {
handleError(err);
}
handleError(err);
}
}

Expand Down
3 changes: 3 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
include: ['**/__tests__/**/?(*.)+(spec|test).[jt]s?(x)'],
coverage: {
exclude: ['**/src/index.ts'],
},
},
});

0 comments on commit 3b10115

Please sign in to comment.