From 6b05751bcb61e45066b43ad0dba10984a549a5ad Mon Sep 17 00:00:00 2001 From: Aadit M Shah Date: Thu, 29 Sep 2022 11:41:14 +0530 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=90=9B=20Migrate=20from=20require=20t?= =?UTF-8?q?o=20readFileSync.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configurationVault/getConfiguration.js | 11 ++++--- .../getConfiguration.spec.js | 29 +++++++++---------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/utils/configurationVault/getConfiguration.js b/src/utils/configurationVault/getConfiguration.js index 15374ef36c..933299260c 100644 --- a/src/utils/configurationVault/getConfiguration.js +++ b/src/utils/configurationVault/getConfiguration.js @@ -1,5 +1,6 @@ import Conf from 'conf' import { cwd } from 'process' +import { readFileSync } from 'fs' import { pathExistsSync } from 'path-exists' import { CONFIG, EMOJI_COMMIT_FORMATS } from '@constants/configuration' @@ -30,12 +31,14 @@ const getConfiguration = (): { get: Function, set: Function } => { const packageJson = `${cwd()}/package.json` const configurationFile = `${cwd()}/.gitmojirc.json` - if (pathExistsSync(packageJson) && require(packageJson)?.gitmoji) { - return require(packageJson).gitmoji + if (pathExistsSync(packageJson)) { + const config = JSON.parse(readFileSync(packageJson))?.gitmoji + if (config) return config } - if (pathExistsSync(configurationFile) && require(configurationFile)) { - return require(configurationFile) + if (pathExistsSync(configurationFile)) { + const config = JSON.parse(readFileSync(configurationFile)) + if (config) return config } return LOCAL_CONFIGURATION.store diff --git a/test/utils/configurationVault/getConfiguration.spec.js b/test/utils/configurationVault/getConfiguration.spec.js index 0907e13fb9..d2bb4e001b 100644 --- a/test/utils/configurationVault/getConfiguration.spec.js +++ b/test/utils/configurationVault/getConfiguration.spec.js @@ -1,13 +1,16 @@ import Conf from 'conf' +import { readFileSync } from 'fs' import { pathExistsSync } from 'path-exists' import { CONFIG, EMOJI_COMMIT_FORMATS } from '@constants/configuration' import getConfiguration from '@utils/configurationVault/getConfiguration' -jest.mock('conf', () => jest.fn().mockReturnValue({ - store: { from: 'local-cli' }, - set: jest.fn() -})) +jest.mock('conf', () => + jest.fn().mockReturnValue({ + store: { from: 'local-cli' }, + set: jest.fn() + }) +) describe('getConfiguration', () => { describe('setup', () => { @@ -40,9 +43,9 @@ describe('getConfiguration', () => { describe('when `gitmoji` key is defined', () => { beforeAll(() => { - jest.doMock('../../../package.json', () => ({ - gitmoji: { from: 'package.json' } - })) + readFileSync.mockImplementation(() => + JSON.stringify({ gitmoji: { from: 'package.json' } }) + ) }) it('should return package.json configuration', () => { @@ -54,9 +57,7 @@ describe('getConfiguration', () => { describe('when `gitmoji` key is not defined', () => { beforeAll(() => { - jest.doMock('../../../package.json', () => ({ - gitmoji: undefined - })) + readFileSync.mockImplementation(() => JSON.stringify({})) }) it('should return local configuration', () => { @@ -80,9 +81,7 @@ describe('getConfiguration', () => { describe('when file contains a valid json', () => { beforeAll(() => { - jest.doMock('../../../.gitmojirc.json', () => ({ - from: 'rc' - }), { virtual: true }) + readFileSync.mockImplementation(() => JSON.stringify({ from: 'rc' })) }) it('should return .gitmojirc.json configuration', () => { @@ -94,7 +93,7 @@ describe('getConfiguration', () => { describe('when file is empty', () => { beforeAll(() => { - jest.doMock('../../../.gitmojirc.json', () => undefined, { virtual: true }) + readFileSync.mockImplementation(() => JSON.stringify(null)) }) it('should return .gitmojirc.json configuration', () => { @@ -145,4 +144,4 @@ describe('getConfiguration', () => { expect(Conf().set).toHaveBeenCalledWith('key', 'value') }) }) -}) \ No newline at end of file +}) From 3237e0ac19c45df7fcaf1737c3168ca49cf972ab Mon Sep 17 00:00:00 2001 From: Aadit M Shah Date: Thu, 29 Sep 2022 11:49:03 +0530 Subject: [PATCH 2/3] :white_check_mark: Import mockProcessExit from jest-mock-process. --- test/commands/commit.spec.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/test/commands/commit.spec.js b/test/commands/commit.spec.js index 564e0029c1..1a960c99c5 100644 --- a/test/commands/commit.spec.js +++ b/test/commands/commit.spec.js @@ -2,7 +2,7 @@ import inquirer from 'inquirer' import { execa } from 'execa' import fs from 'fs' import chalk from 'chalk' -const mockProcess = require('jest-mock-process') +import { mockProcessExit } from 'jest-mock-process' import configurationVault from '@utils/configurationVault' import getDefaultCommitContent from '@utils/getDefaultCommitContent' @@ -162,7 +162,7 @@ describe('commit command', () => { Promise.resolve(stubs.clientCommitAnswers) ) getEmojis.mockResolvedValue(stubs.gitmojis) - mockProcess.mockProcessExit() + mockProcessExit() process.argv[3] = stubs.argv process.argv[COMMIT_MESSAGE_SOURCE] = undefined getDefaultCommitContent.mockReturnValueOnce( @@ -192,7 +192,7 @@ describe('commit command', () => { Promise.resolve(stubs.clientCommitAnswersWithScope) ) getEmojis.mockResolvedValue(stubs.gitmojis) - mockProcess.mockProcessExit() + mockProcessExit() process.argv[3] = stubs.argv process.argv[COMMIT_MESSAGE_SOURCE] = stubs.commitSource getDefaultCommitContent.mockReturnValueOnce( @@ -223,7 +223,7 @@ describe('commit command', () => { Simulation needed because if we just mock process exit, then the code execution resume in the test. */ process.argv[COMMIT_MESSAGE_SOURCE] = stubs.commitSource - mockProcess.mockProcessExit(new Error('ProcessExit0')) + mockProcessExit(new Error('ProcessExit0')) execa.mockReturnValueOnce(Promise.resolve(stubs.gitAbsoluteDir)) // mock that we found one of the rebase trigger (file existence in .git) fs.existsSync.mockReturnValueOnce(true) @@ -245,7 +245,7 @@ describe('commit command', () => { when the hook mode detect that the user is rebasing. (Simulated to not kill the tests) Simulation needed because if we just mock process exit, then the code execution resume in the test. */ - mockProcess.mockProcessExit(new Error('ProcessExit0')) + mockProcessExit(new Error('ProcessExit0')) execa.mockReturnValueOnce(Promise.resolve(stubs.gitAbsoluteDir)) // mock that we are amending process.argv[COMMIT_MESSAGE_SOURCE] = 'commit sha123' @@ -282,7 +282,7 @@ describe('commit command', () => { getEmojis.mockResolvedValue(stubs.gitmojis) // Use an exception to suspend code execution to simulate process.exit - mockProcess.mockProcessExit(new Error('SIGINT')) + mockProcessExit(new Error('SIGINT')) process.argv[3] = stubs.argv process.argv[COMMIT_MESSAGE_SOURCE] = stubs.commitSource @@ -303,7 +303,7 @@ describe('commit command', () => { describe('with git auto merge trigger by git pull', () => { it('should cancel the hook', async () => { - mockProcess.mockProcessExit(new Error('ProcessExit0')) + mockProcessExit(new Error('ProcessExit0')) execa.mockReturnValueOnce(Promise.resolve(stubs.gitAbsoluteDir)) // mock that we are merging process.argv[3] = '.git/MERGE_MSG' @@ -318,7 +318,6 @@ describe('commit command', () => { expect(process.exit).toHaveBeenCalledWith(0) }) }) - }) describe('guard', () => { From 2c13df33c9ac2ff073f2823cd1e136a13710ab21 Mon Sep 17 00:00:00 2001 From: Aadit M Shah Date: Thu, 29 Sep 2022 12:00:43 +0530 Subject: [PATCH 3/3] :white_check_mark: Use dynamic imports instead of require. --- test/utils/buildFetchOptions.spec.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/test/utils/buildFetchOptions.spec.js b/test/utils/buildFetchOptions.spec.js index 33373097b8..b55264cf2d 100644 --- a/test/utils/buildFetchOptions.spec.js +++ b/test/utils/buildFetchOptions.spec.js @@ -103,11 +103,12 @@ describe('buildFetchOptions', () => { describe('when using an anonymous proxy', () => { describe('when is http proxy', () => { - it('should return an object containing a proxy agent', () => { + it('should return an object containing a proxy agent', async () => { process.env.http_proxy = 'http://localhost-env:8080' - const buildFetchOptionsFromEnv = require('../../src/utils/buildFetchOptions') - .default + const { default: buildFetchOptionsFromEnv } = await import( + '../../src/utils/buildFetchOptions' + ) expect(buildFetchOptionsFromEnv()).toMatchObject({ agent: { @@ -126,11 +127,12 @@ describe('buildFetchOptions', () => { }) describe('when is https proxy', () => { - it('should return an object containing a proxy agent', () => { + it('should return an object containing a proxy agent', async () => { process.env.https_proxy = 'https://localhost-env:8080' - const buildFetchOptionsFromEnv = require('../../src/utils/buildFetchOptions') - .default + const { default: buildFetchOptionsFromEnv } = await import( + '../../src/utils/buildFetchOptions' + ) expect(buildFetchOptionsFromEnv()).toMatchObject({ agent: { @@ -151,11 +153,12 @@ describe('buildFetchOptions', () => { describe('when using an authenticated proxy', () => { describe('when is http proxy', () => { - it('should return an object containing a proxy agent', () => { + it('should return an object containing a proxy agent', async () => { process.env.http_proxy = 'http://user:pass@localhost-env:8080' - const buildFetchOptionsFromEnv = require('../../src/utils/buildFetchOptions') - .default + const { default: buildFetchOptionsFromEnv } = await import( + '../../src/utils/buildFetchOptions' + ) expect(buildFetchOptionsFromEnv()).toMatchObject({ agent: { @@ -174,11 +177,12 @@ describe('buildFetchOptions', () => { }) describe('when is https proxy', () => { - it('should return an object containing a proxy agent', () => { + it('should return an object containing a proxy agent', async () => { process.env.https_proxy = 'https://user:pass@localhost-env:8080' - const buildFetchOptionsFromEnv = require('../../src/utils/buildFetchOptions') - .default + const { default: buildFetchOptionsFromEnv } = await import( + '../../src/utils/buildFetchOptions' + ) expect(buildFetchOptionsFromEnv()).toMatchObject({ agent: {