Skip to content

Commit

Permalink
test: Add tests for removeUndefined function
Browse files Browse the repository at this point in the history
  • Loading branch information
akash1810 committed Feb 27, 2024
1 parent 18e7c02 commit fdc0c63
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
27 changes: 26 additions & 1 deletion packages/github-actions-usage/src/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,36 @@ import fs from 'fs';
import assert from 'node:assert';
import { describe, test } from 'node:test';
import type { GithubWorkflow } from './transform';
import { getUsesInWorkflowTemplate, getWorkflowTemplate } from './transform';
import {
getUsesInWorkflowTemplate,
getWorkflowTemplate,
removeUndefined,
} from './transform';

const readWorkflowFile = (path: string): string =>
fs.readFileSync(`test/fixtures/${path}`, 'utf8');

void describe('removeUndefined', () => {
void test('removes undefined values from an array', () => {
const input = [1, undefined, 2, undefined, 3];
const expected = [1, 2, 3];
const actual = removeUndefined(input);
assert.deepEqual(actual, expected);
});
void test('returns an empty array if all values are undefined', () => {
const input = [undefined, undefined, undefined];
const expected: unknown[] = [];
const actual = removeUndefined(input);
assert.deepEqual(actual, expected);
});
void test('return the same array when no undefined values are present', () => {
const input = [1, 2, 3];
const expected = [1, 2, 3];
const actual = removeUndefined(input);
assert.deepEqual(actual, expected);
});
});

void describe('getWorkflowTemplate', () => {
void test('Workflow with single job, and multiple steps is recognised', async () => {
const path = 'multi-step-workflow.yml';
Expand Down
4 changes: 2 additions & 2 deletions packages/github-actions-usage/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import type {
import type { RawGithubWorkflow } from './db-read';
import type { UnsavedGithubActionUsage } from './db-write';

function removeUndefined<T>(array: Array<T | undefined>): T[] {
return array.filter((item) => item !== undefined) as T[];
export function removeUndefined<T>(array: Array<T | undefined>): T[] {
return array.filter((item): item is T => item !== undefined);
}

export interface GithubWorkflow {
Expand Down

0 comments on commit fdc0c63

Please sign in to comment.