diff --git a/test/integration/fixtures/metadata.fixture.ts b/test/integration/fixtures/metadata.fixture.ts index a68b325..db04783 100644 --- a/test/integration/fixtures/metadata.fixture.ts +++ b/test/integration/fixtures/metadata.fixture.ts @@ -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, ], }; diff --git a/test/unit/__snapshots__/config.test.ts.snap b/test/unit/__snapshots__/config.test.ts.snap deleted file mode 100644 index 91d3713..0000000 --- a/test/unit/__snapshots__/config.test.ts.snap +++ /dev/null @@ -1,34 +0,0 @@ -// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html - -exports[`Config Object > get policy() 1`] = ` -[ - { - "feedback": { - "frozen-state": "This is No-No", - "unfreeze-state": "This is Yes-Yes", - }, - "tags": [ - "alpha", - "beta", - ], - }, -] -`; - -exports[`Config Object > get policy() 2`] = ` -[ - "alpha", - "beta", -] -`; - -exports[`Config Object > get policy() 3`] = ` -{ - "frozen-state": "This is No-No", - "unfreeze-state": "This is Yes-Yes", -} -`; - -exports[`Config Object > get policy() 4`] = `"This is No-No"`; - -exports[`Config Object > get policy() 5`] = `"This is Yes-Yes"`; diff --git a/test/unit/config.test.ts b/test/unit/config.test.ts index 2e917c7..ee21e76 100644 --- a/test/unit/config.test.ts +++ b/test/unit/config.test.ts @@ -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(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('can be instantiated', context => { - context.configs.map(item => expect(item).toBeDefined()); + beforeEach(() => { + config = new Config(configRaw); }); - test('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) => { @@ -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(` [ { @@ -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$", + ], + }, ] `); @@ -89,10 +131,8 @@ describe('Config Object', () => { ); }); - test('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); }); diff --git a/test/unit/delay.test.ts b/test/unit/delay.test.ts new file mode 100644 index 0000000..d48270e --- /dev/null +++ b/test/unit/delay.test.ts @@ -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); + }); +}); diff --git a/test/unit/error.test.ts b/test/unit/error.test.ts new file mode 100644 index 0000000..5e8d39e --- /dev/null +++ b/test/unit/error.test.ts @@ -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]` + ); + }); +}); diff --git a/test/unit/fixtures/config.fixture.ts b/test/unit/fixtures/config.fixture.ts deleted file mode 100644 index 398f46d..0000000 --- a/test/unit/fixtures/config.fixture.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { Config } from '../../../src/config'; - -export interface IConfigTestContext { - configs: Config[]; -} - -export const configContextFixture: IConfigTestContext = { - configs: [ - new Config({ - policy: [ - { - tags: ['alpha', 'beta'], - feedback: { - 'frozen-state': 'This is No-No', - 'unfreeze-state': 'This is Yes-Yes', - }, - }, - ], - }), - ], -}; diff --git a/test/unit/milestone.test.ts b/test/unit/milestone.test.ts new file mode 100644 index 0000000..fbea462 --- /dev/null +++ b/test/unit/milestone.test.ts @@ -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); + }); +});