Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend test suite 🧪 #231

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions test/integration/fixtures/metadata.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export interface IMetadataTestContext {

export const metadataContextFixture: IMetadataTestContext = {
payloads: [
payloadPullRequestOpened as PullRequestOpenedEvent,
payloadPullRequestReopened as PullRequestReopenedEvent,
payloadPullRequestSynchronize as PullRequestSynchronizeEvent,
payloadPullRequestOpened as unknown as PullRequestOpenedEvent,
payloadPullRequestReopened as unknown as PullRequestReopenedEvent,
payloadPullRequestSynchronize as unknown as PullRequestSynchronizeEvent,
],
};
34 changes: 0 additions & 34 deletions test/unit/__snapshots__/config.test.ts.snap

This file was deleted.

116 changes: 78 additions & 38 deletions test/unit/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,78 @@
import { describe, it, expect, beforeEach, test } from 'vitest';

import { Config } from '../../src/config';

import {
configContextFixture,
IConfigTestContext,
} from './fixtures/config.fixture';
import { CustomOctokit } from '../../src/octokit';

describe('Config Object', () => {
beforeEach<IConfigTestContext>(context => {
context.configs = configContextFixture.configs;
});
let config: Config;
let configRaw = {
policy: [
{
tags: ['alpha', 'beta'],
feedback: {
'frozen-state': 'This is No-No',
'unfreeze-state': 'This is Yes-Yes',
},
},
{
tags: [`^(v\d*)-rc\d$`],
labels: {
allow: ['regression ⚠️'],
},
feedback: {
'frozen-state': `We are currently in a development freeze phase.\nPlease ...`,
'unfreeze-state': `We had successfully released a new major release.\nWe are no longer in a development freeze phase.`,
},
},
],
};

it<IConfigTestContext>('can be instantiated', context => {
context.configs.map(item => expect(item).toBeDefined());
beforeEach(() => {
config = new Config(configRaw);
});

test<IConfigTestContext>('get policy()', context => {
context.configs.map(configItem => {
expect(configItem.policy).toMatchSnapshot();
it('can be instantiated', () => {
expect(config).toBeDefined();
expect(config).toBeInstanceOf(Config);
});

configItem.policy.map(policyItem => {
expect(policyItem.tags).toMatchSnapshot();
expect(policyItem.feedback).toMatchSnapshot();
expect(policyItem.feedback['frozen-state']).toMatchSnapshot();
expect(policyItem.feedback['unfreeze-state']).toMatchSnapshot();
});
});
test('get policy()', () => {
expect(config.policy).toMatchInlineSnapshot(`
[
{
"feedback": {
"frozen-state": "This is No-No",
"unfreeze-state": "This is Yes-Yes",
},
"tags": [
"alpha",
"beta",
],
},
{
"feedback": {
"frozen-state": "We are currently in a development freeze phase.
Please ...",
"unfreeze-state": "We had successfully released a new major release.
We are no longer in a development freeze phase.",
},
"labels": {
"allow": [
"regression ⚠️",
],
},
"tags": [
"^(vd*)-rcd$",
],
},
]
`);
});

test('getConfig()', async () => {
process.env['INPUT_CONFIG-PATH'] = '.github/development-freeze.yml';
process.env['GITHUB_REPOSITORY'] = 'test/test';

const configObject = {
policy: [
{
tags: ['alpha', 'beta'],
feedback: {
'frozen-state': 'This is No-No',
'unfreeze-state': 'This is Yes-Yes',
},
random: 'random',
},
],
};

const noConfigObject = undefined;

const octokit = (data: unknown) => {
Expand All @@ -66,7 +92,7 @@ describe('Config Object', () => {
} as unknown as CustomOctokit;
};

let config = await Config.getConfig(octokit(configObject));
let config = await Config.getConfig(octokit(configRaw));
expect(config.policy).toMatchInlineSnapshot(`
[
{
Expand All @@ -79,6 +105,22 @@ describe('Config Object', () => {
"beta",
],
},
{
"feedback": {
"frozen-state": "We are currently in a development freeze phase.
Please ...",
"unfreeze-state": "We had successfully released a new major release.
We are no longer in a development freeze phase.",
},
"labels": {
"allow": [
"regression ⚠️",
],
},
"tags": [
"^(vd*)-rcd$",
],
},
]
`);

Expand All @@ -89,10 +131,8 @@ describe('Config Object', () => {
);
});

test<IConfigTestContext>('is config empty', context => {
context.configs.map(async item =>
expect(Config.isConfigEmpty(item)).toEqual(false)
);
test('is config empty', () => {
expect(Config.isConfigEmpty(configRaw)).toEqual(false);

expect(Config.isConfigEmpty(null)).toEqual(true);
});
Expand Down
13 changes: 13 additions & 0 deletions test/unit/delay.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { describe, expect, test } from 'vitest';

import { delay } from '../../src/delay';

describe('Test delay functionality', () => {
test('delay', async () => {
const start = Date.now();
await delay(4);
const end = Date.now();

expect(end - start).toBeGreaterThanOrEqual(4000);
});
});
31 changes: 31 additions & 0 deletions test/unit/error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, test } from 'vitest';

import { FreezerError, raise } from '../../src/error';

describe('Test FreezerError class', () => {
test('without error code', () => {
const error = new FreezerError('Some error');

expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(FreezerError);

expect(error.message).toMatchInlineSnapshot(`"Some error"`);
expect(error.code).toBeUndefined();
});

test('with error code', () => {
const error = new FreezerError('Some error', 42);

expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(FreezerError);

expect(error.message).toMatchInlineSnapshot(`"Some error"`);
expect(error.code).toMatchInlineSnapshot(`42`);
});

test('raise error', () => {
expect(() => raise('Some error')).toThrowErrorMatchingInlineSnapshot(
`[Error: Some error]`
);
});
});
21 changes: 0 additions & 21 deletions test/unit/fixtures/config.fixture.ts

This file was deleted.

32 changes: 32 additions & 0 deletions test/unit/milestone.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, beforeEach, test } from 'vitest';

import { Milestone } from '../../src/milestone';

describe('Metadata Class', () => {
let milestone: Milestone;

beforeEach(() => {
milestone = new Milestone({
html_url: '',
number: 1,
title: 'v255',
description: 'description',
state: 'open',
});
});

test('can be instantiated', () => {
expect(milestone).toBeDefined();
expect(milestone).toBeInstanceOf(Milestone);
expect(milestone.regex).toBeInstanceOf(RegExp);
expect(milestone.regex.toString()).toBe('/^v255\\S*$/');
});

test('isCompliant()', () => {
expect(milestone.isCompliant('v255-rc1')).toBe(true);
expect(milestone.isCompliant('v255')).toBe(true);

expect(milestone.isCompliant('v256-rc1')).toBe(false);
expect(milestone.isCompliant('bla-bla')).toBe(false);
});
});
Loading