Skip to content

Commit

Permalink
Merge pull request #8 from jlndk/feature/polish-before-first-release
Browse files Browse the repository at this point in the history
  • Loading branch information
jlndk authored Feb 1, 2021
2 parents 48c2583 + 0a071f4 commit 2814873
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ jobs:
test-command: yarn test:demo
# Dont post code coverage for the demo tests
coverage-comment: false
# Dont mark action as failed if jest returns non-zero exit code
fail-action-if-jest-fails: false
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ inputs:
describe: 'If true, the content of the code coverage comment will be printed as debug output instead of actually submitting it.'
required: false
default: 'false'
fail-action-if-jest-fails:
describe: 'If true, the action is marked as failed, if jest returns a non-zero exit code.'
required: false
default: 'true'
runs:
using: 'node12'
main: 'dist/index.js'
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
transform: {
'^.+\\.ts$': 'ts-jest',
},
collectCoverageFrom: ['src/**/*.ts'],
collectCoverageFrom: ['src/**/*.ts', '!src/demo_tests/*.ts'],
clearMocks: true,
testEnvironment: 'node',
testRunner: 'jest-circus/runner',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "github-action-jest",
"version": "0.0.1",
"version": "1.0.0",
"private": true,
"description": "A github action for running jest. With code coverage!",
"main": "lib/main.js",
Expand All @@ -17,6 +17,7 @@
},
"keywords": [
"actions",
"ci",
"node",
"jest",
"code-coverage"
Expand Down
5 changes: 2 additions & 3 deletions src/__tests__/run.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ jest.mock('@actions/github', () => ({

jest.mock('@actions/exec');

// TODO: Reintroduce test and mock/test file system access
describe.skip('runJest', () => {
describe('runJest', () => {
it('executes command', async () => {
await runJest({
cmd: 'yarn test',
cwd: './',
coverageFilePath: 'foo.json',
});

expect(exec).toBeCalledWith('yarn test', [], {
cwd: './',
ignoreReturnCode: true,
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/args.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getInput } from '@actions/core';

export function hasBooleanArg(key: string, required = false): boolean {
export function getBooleanArg(key: string, required = false): boolean {
return Boolean(JSON.parse(getInput(key, { required })));
}

Expand Down
24 changes: 18 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import path, { sep } from 'path';
import * as core from '@actions/core';
import { getGithubToken, hasBooleanArg } from './args';
import { getGithubToken, getBooleanArg } from './args';
import { generateCommentBody } from './coverage';
import updateOrCreateComment from './comment';
import runJest, { getJestCommand } from './run';
import runJest, { getJestCommand, readTestResults } from './run';
import { reportTestResults } from './testResults';

async function main(): Promise<void> {
// Get args
const baseCommand = core.getInput('test-command', { required: false }) ?? 'npm test';
const workingDirectory = core.getInput('working-directory', { required: false });
const shouldCommentCoverage = hasBooleanArg('coverage-comment');
const dryRun = hasBooleanArg('dry-run');
const runOnlyChangedFiles = hasBooleanArg('changes-only');
const shouldCommentCoverage = getBooleanArg('coverage-comment');
const dryRun = getBooleanArg('dry-run');
const runOnlyChangedFiles = getBooleanArg('changes-only');
const exitOnJestFail = getBooleanArg('fail-action-if-jest-fails');

// Compute paths
const cwd = workingDirectory ? path.resolve(workingDirectory) : process.cwd();
Expand All @@ -29,11 +30,22 @@ async function main(): Promise<void> {
core.info('Executing jest');

// execute jest
const results = await runJest({ cmd, cwd, coverageFilePath });
const statusCode = await runJest({ cmd, cwd });
const results = readTestResults(coverageFilePath);

core.info('Reporting test results');
reportTestResults(results);

if (exitOnJestFail && statusCode !== 0) {
throw new Error(
'Jest returned non-zero exit code. Check annotations or debug output for more information.'
);
} else if (!exitOnJestFail) {
core.info(
'Continuing even though jest failed, since "fail-action-if-jest-fails" is false.'
);
}

// Return early if we should not post code coverage comment
if (!shouldCommentCoverage) {
core.info('Code coverage commenting is disabled. Skipping...');
Expand Down
20 changes: 13 additions & 7 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { readFileSync } from 'fs';
import { exec } from '@actions/exec';
import { context } from '@actions/github';
import type { FormattedTestResults } from '@jest/test-result/build/types';
import { debug, endGroup, startGroup } from '@actions/core';

export type RunJestOptions = {
coverageFilePath: string;
cmd: string;
cwd: string;
};
Expand All @@ -19,13 +19,19 @@ export type GetJestCommandArgs = MakeJestArgs & {
baseCommand: string;
};

export default async function runJest({
cmd,
cwd,
coverageFilePath,
}: RunJestOptions): Promise<FormattedTestResults> {
await exec(cmd, [], { cwd, silent: true, ignoreReturnCode: true });
export default async function runJest({ cmd, cwd }: RunJestOptions): Promise<number> {
startGroup('Jest output');

const statusCode = await exec(cmd, [], { cwd, ignoreReturnCode: true });

debug(`Jest exited with status code: ${statusCode}`);

endGroup();

return statusCode;
}

export function readTestResults(coverageFilePath: string): FormattedTestResults {
const content = readFileSync(coverageFilePath, 'utf-8');

const results = JSON.parse(content) as FormattedTestResults;
Expand Down

0 comments on commit 2814873

Please # to comment.