From d0d36280208a8c43edef7ce4993eff948131bc07 Mon Sep 17 00:00:00 2001 From: Josh Dales Date: Mon, 20 Mar 2023 17:23:11 -0400 Subject: [PATCH] Add extra tests and use toEqual matcher. Turns out that toMatchObject would allow extra properties through --- __tests__/branch.test.ts | 12 ++++---- __tests__/changedFiles.test.ts | 55 +++++++++++++++++++++------------- __tests__/labeler.test.ts | 4 +-- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/__tests__/branch.test.ts b/__tests__/branch.test.ts index 066c8f01..1085c1b2 100644 --- a/__tests__/branch.test.ts +++ b/__tests__/branch.test.ts @@ -90,7 +90,7 @@ describe('toBranchMatchConfig', () => { it('returns an empty object', () => { const result = toBranchMatchConfig(config); - expect(result).toMatchObject({}); + expect(result).toEqual({}); }); }); @@ -99,7 +99,7 @@ describe('toBranchMatchConfig', () => { it('sets headBranch in the matchConfig', () => { const result = toBranchMatchConfig(config); - expect(result).toMatchObject({ + expect(result).toEqual({ headBranch: ['testing'] }); }); @@ -109,7 +109,7 @@ describe('toBranchMatchConfig', () => { it('sets headBranch in the matchConfig', () => { const result = toBranchMatchConfig(stringConfig); - expect(result).toMatchObject({ + expect(result).toEqual({ headBranch: ['testing'] }); }); @@ -120,7 +120,7 @@ describe('toBranchMatchConfig', () => { const config = {'base-branch': ['testing']}; it('sets baseBranch in the matchConfig', () => { const result = toBranchMatchConfig(config); - expect(result).toMatchObject({ + expect(result).toEqual({ baseBranch: ['testing'] }); }); @@ -130,7 +130,7 @@ describe('toBranchMatchConfig', () => { it('sets baseBranch in the matchConfig', () => { const result = toBranchMatchConfig(stringConfig); - expect(result).toMatchObject({ + expect(result).toEqual({ baseBranch: ['testing'] }); }); @@ -141,7 +141,7 @@ describe('toBranchMatchConfig', () => { const config = {'base-branch': ['testing'], 'head-branch': ['testing']}; it('sets headBranch and baseBranch in the matchConfig', () => { const result = toBranchMatchConfig(config); - expect(result).toMatchObject({ + expect(result).toEqual({ baseBranch: ['testing'], headBranch: ['testing'] }); diff --git a/__tests__/changedFiles.test.ts b/__tests__/changedFiles.test.ts index 771d6b4d..eab87d17 100644 --- a/__tests__/changedFiles.test.ts +++ b/__tests__/changedFiles.test.ts @@ -58,32 +58,32 @@ describe('toChangedFilesMatchConfig', () => { it('returns an empty object', () => { const result = toChangedFilesMatchConfig(config); - expect(result).toMatchObject({}); - }); - }); - - describe(`when both 'any' and 'all' keys are present`, () => { - const config = {'changed-files': [{all: 'testing'}, {any: 'testing'}]}; - - it('sets both values in the config object', () => { - const result = toChangedFilesMatchConfig(config); - expect(result).toMatchObject({ - changedFiles: { - any: ['testing'], - all: ['testing'] - } - }); + expect(result).toEqual({}); }); }); describe(`when there is a 'changed-files' key in the config`, () => { + describe(`and both 'any' and 'all' keys are present`, () => { + const config = {'changed-files': [{all: 'testing'}, {any: 'testing'}]}; + + it('sets both values in the config object', () => { + const result = toChangedFilesMatchConfig(config); + expect(result).toEqual({ + changedFiles: { + any: ['testing'], + all: ['testing'] + } + }); + }); + }); + describe(`and it contains a 'all' key`, () => { describe('with a value of a string', () => { const config = {'changed-files': [{all: 'testing'}]}; it('sets the value to be an array of strings in the config object', () => { const result = toChangedFilesMatchConfig(config); - expect(result).toMatchObject({ + expect(result).toEqual({ changedFiles: { all: ['testing'] } @@ -96,7 +96,7 @@ describe('toChangedFilesMatchConfig', () => { it('sets the value in the config object', () => { const result = toChangedFilesMatchConfig(config); - expect(result).toMatchObject({ + expect(result).toEqual({ changedFiles: { all: ['testing'] } @@ -111,7 +111,7 @@ describe('toChangedFilesMatchConfig', () => { it('sets the value to be an array of strings on the config object', () => { const result = toChangedFilesMatchConfig(config); - expect(result).toMatchObject({ + expect(result).toEqual({ changedFiles: { any: ['testing'] } @@ -124,7 +124,7 @@ describe('toChangedFilesMatchConfig', () => { it('sets the value in the config object', () => { const result = toChangedFilesMatchConfig(config); - expect(result).toMatchObject({ + expect(result).toEqual({ changedFiles: { any: ['testing'] } @@ -138,7 +138,7 @@ describe('toChangedFilesMatchConfig', () => { it(`sets the string as an array under an 'any' key`, () => { const result = toChangedFilesMatchConfig(config); - expect(result).toMatchObject({ + expect(result).toEqual({ changedFiles: { any: ['testing'] } @@ -151,7 +151,20 @@ describe('toChangedFilesMatchConfig', () => { it(`sets the array under an 'any' key`, () => { const result = toChangedFilesMatchConfig(config); - expect(result).toMatchObject({ + expect(result).toEqual({ + changedFiles: { + any: ['testing'] + } + }); + }); + }); + + describe('and there is an unknown value in the config', () => { + const config = {'changed-files': [{any: 'testing'}, {sneaky: 'test'}]}; + + it(`does not set the value in the match config`, () => { + const result = toChangedFilesMatchConfig(config); + expect(result).toEqual({ changedFiles: { any: ['testing'] } diff --git a/__tests__/labeler.test.ts b/__tests__/labeler.test.ts index b8bd423b..10ba96db 100644 --- a/__tests__/labeler.test.ts +++ b/__tests__/labeler.test.ts @@ -20,7 +20,7 @@ describe('toMatchConfig', () => { it('returns a MatchConfig object with all options', () => { const result = toMatchConfig(config); - expect(result).toMatchObject({ + expect(result).toEqual({ changedFiles: { all: ['testing-all'], any: ['testing-any'] @@ -35,7 +35,7 @@ describe('toMatchConfig', () => { it('does not include the unexpected items in the returned MatchConfig object', () => { const result = toMatchConfig(config); - expect(result).toMatchObject({ + expect(result).toEqual({ changedFiles: { all: ['testing-all'], any: ['testing-any']