From 1a4c518329e83b9346b754926148937ff9407647 Mon Sep 17 00:00:00 2001 From: Sebastien Date: Mon, 9 Oct 2023 09:58:15 +0200 Subject: [PATCH] fix: typescript default exported value (#706) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Paweł Idczak --- .github/linters/.cspell.json | 1 + __tests__/functional/main.test.ts | 94 +++++++++++++++++++++++++++++++ package.json | 2 +- src/main.ts | 2 +- yarn.lock | 10 ++-- 5 files changed, 102 insertions(+), 7 deletions(-) create mode 100644 __tests__/functional/main.test.ts diff --git a/.github/linters/.cspell.json b/.github/linters/.cspell.json index 9387c4a5..79a1f022 100644 --- a/.github/linters/.cspell.json +++ b/.github/linters/.cspell.json @@ -117,6 +117,7 @@ "predicat", "quotepath", "recentsha", + "repogitdiff", "rulesets", "samlssoconfig", "samlssoconfigs", diff --git a/__tests__/functional/main.test.ts b/__tests__/functional/main.test.ts new file mode 100644 index 00000000..d3fe9cdf --- /dev/null +++ b/__tests__/functional/main.test.ts @@ -0,0 +1,94 @@ +;`use strict` +// eslint-disable-next-line @typescript-eslint/no-var-requires +const sgd = require('../../src/main') +import { expect, jest, describe, it } from '@jest/globals' + +const mockValidateConfig = jest.fn() +jest.mock('../../src/utils/cliHelper', () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const actualModule: any = jest.requireActual('../../src/utils/cliHelper') + return jest.fn().mockImplementation(function () { + return { + ...actualModule, + validateConfig: mockValidateConfig, + } + }) +}) + +const mockGetLines = jest.fn() +jest.mock('../../src/utils/repoGitDiff', () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const actualModule: any = jest.requireActual('../../src/utils/repoGitDiff') + return jest.fn().mockImplementation(function () { + return { + ...actualModule, + getLines: mockGetLines, + } + }) +}) + +const mockProcess = jest.fn() +jest.mock('../../src/service/diffLineInterpreter', () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const actualModule: any = jest.requireActual( + '../../src/service/diffLineInterpreter' + ) + return jest.fn().mockImplementation(function () { + return { + ...actualModule, + process: mockProcess, + } + }) +}) + +describe('external library inclusion', () => { + describe('when configuration is not valid', () => { + beforeEach(() => { + // Arrange + mockValidateConfig.mockImplementationOnce(() => + Promise.reject(new Error('test')) + ) + }) + + it('it should throw', async () => { + // Arrange + expect.assertions(1) + + // Act + try { + await sgd({}) + } catch (error) { + // Assert + expect((error as Error).message).toEqual('test') + } + }) + }) + + describe('when there are no changes', () => { + beforeEach(() => { + // Arrange + mockGetLines.mockImplementationOnce(() => Promise.resolve([])) + }) + it('it should not process lines', async () => { + // Act + await sgd({}) + + // Assert + expect(mockProcess).toBeCalledWith([]) + }) + }) + + describe('when there are changes', () => { + beforeEach(() => { + // Arrange + mockGetLines.mockImplementationOnce(() => Promise.resolve(['line'])) + }) + it('it should process those lines', async () => { + // Act + await sgd({}) + + // Assert + expect(mockProcess).toBeCalledWith(['line']) + }) + }) +}) diff --git a/package.json b/package.json index 543fd1d0..88466f1c 100644 --- a/package.json +++ b/package.json @@ -91,7 +91,7 @@ "nyc": "^15.1.0", "prettier": "^3.0.3", "shx": "^0.3.4", - "sinon": "^16.0.0", + "sinon": "^16.1.0", "ts-jest": "^29.1.1", "ts-node": "^10.9.1", "typescript": "^5.2.2", diff --git a/src/main.ts b/src/main.ts index e8b468af..e53b0b66 100644 --- a/src/main.ts +++ b/src/main.ts @@ -27,4 +27,4 @@ const sgd = async (config: Config): Promise => { return work } -export default sgd +export = sgd diff --git a/yarn.lock b/yarn.lock index 76d82a76..ecc70678 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9725,7 +9725,7 @@ __metadata: nyc: ^15.1.0 prettier: ^3.0.3 shx: ^0.3.4 - sinon: ^16.0.0 + sinon: ^16.1.0 ts-jest: ^29.1.1 ts-node: ^10.9.1 typescript: ^5.2.2 @@ -9817,9 +9817,9 @@ __metadata: languageName: node linkType: hard -"sinon@npm:^16.0.0": - version: 16.0.0 - resolution: "sinon@npm:16.0.0" +"sinon@npm:^16.1.0": + version: 16.1.0 + resolution: "sinon@npm:16.1.0" dependencies: "@sinonjs/commons": ^3.0.0 "@sinonjs/fake-timers": ^10.3.0 @@ -9827,7 +9827,7 @@ __metadata: diff: ^5.1.0 nise: ^5.1.4 supports-color: ^7.2.0 - checksum: fbcad39d1610c669bb37ce2d325f2ac666ff5187935d09d1e757e11c50d07b8556b7dfa322eb38ad6ace8a18912224cc502e990779bc3d0954244a1ae8391984 + checksum: b3f910e9b3d28f1241b28ac9d384f45c673b64ecfabf7ca2b94c964036118bd4166a2315f2ec2379a85745a7d4840384514aca416bbf189508a9d7fb55b9d5a8 languageName: node linkType: hard