diff --git a/lib/__tests__/ProjectLogsManager.test.ts b/lib/__tests__/ProjectLogsManager.test.ts index 2a06371ea..975177dd2 100644 --- a/lib/__tests__/ProjectLogsManager.test.ts +++ b/lib/__tests__/ProjectLogsManager.test.ts @@ -1,9 +1,7 @@ -// @ts-nocheck -const { ProjectLogsManager } = require('../projects/ProjectLogsManager'); -const { getProjectConfig, ensureProjectExists } = require('../projects'); -const { - fetchProjectComponentsMetadata, -} = require('@hubspot/local-dev-lib/api/projects'); +import { ProjectLogsManager } from '../projects/ProjectLogsManager'; +import { getProjectConfig, ensureProjectExists } from '../projects'; +import { fetchProjectComponentsMetadata } from '@hubspot/local-dev-lib/api/projects'; +import { SUBCOMPONENT_TYPES } from '../../../hubspot-local-dev-lib/dist/enums/build'; jest.mock('../projects'); jest.mock('@hubspot/local-dev-lib/api/projects'); @@ -26,10 +24,11 @@ describe('lib/projects/ProjectLogsManager', () => { const function1 = { componentName: 'function1', type: { - name: 'APP_FUNCTION', + name: SUBCOMPONENT_TYPES.APP_FUNCTION, }, deployOutput: { appId, + appFunctionName: 'function1', }, }; const functions = [ @@ -37,10 +36,11 @@ describe('lib/projects/ProjectLogsManager', () => { { componentName: 'function2', type: { - name: 'APP_FUNCTION', + name: SUBCOMPONENT_TYPES.APP_FUNCTION, }, deployOutput: { appId, + appFunctionName: 'function2', }, }, ]; @@ -48,9 +48,9 @@ describe('lib/projects/ProjectLogsManager', () => { beforeEach(() => { ProjectLogsManager.reset(); - getProjectConfig.mockResolvedValue(projectConfig); - ensureProjectExists.mockResolvedValue(projectDetails); - fetchProjectComponentsMetadata.mockResolvedValue({ + (getProjectConfig as jest.Mock).mockResolvedValue(projectConfig); + (ensureProjectExists as jest.Mock).mockResolvedValue(projectDetails); + (fetchProjectComponentsMetadata as jest.Mock).mockResolvedValue({ data: { topLevelComponentMetadata: [ { @@ -81,7 +81,7 @@ describe('lib/projects/ProjectLogsManager', () => { }); it('should throw an error if there is a problem with the config', async () => { - getProjectConfig.mockResolvedValue({}); + (getProjectConfig as jest.Mock).mockResolvedValue({}); await expect(async () => ProjectLogsManager.init(accountId) ).rejects.toThrow( @@ -99,7 +99,7 @@ describe('lib/projects/ProjectLogsManager', () => { }); it('should throw an error if there is data missing from the project details', async () => { - ensureProjectExists.mockResolvedValue({}); + (ensureProjectExists as jest.Mock).mockResolvedValue({}); await expect(async () => ProjectLogsManager.init(accountId) ).rejects.toThrow(/There was an error fetching project details/); @@ -177,9 +177,11 @@ describe('lib/projects/ProjectLogsManager', () => { const functionToChoose = { componentName: 'function1', type: { - name: 'APP_FUNCTION', + name: SUBCOMPONENT_TYPES.APP_FUNCTION, }, deployOutput: { + appId: 123, + appFunctionName: 'function1', endpoint: { path: 'yooooooo', methods: ['GET'] }, }, }; @@ -202,11 +204,11 @@ describe('lib/projects/ProjectLogsManager', () => { describe('reset', () => { it('should reset all the values', async () => { - ProjectLogsManager.someRandomField = 'value'; - expect(ProjectLogsManager.someRandomField).toBeDefined(); + ProjectLogsManager.projectName = 'value'; + expect(ProjectLogsManager.projectName).toBeDefined(); ProjectLogsManager.reset(); - expect(ProjectLogsManager.isPublicFunction).toBeUndefined(); + expect(ProjectLogsManager.projectName).toBeUndefined(); }); }); }); diff --git a/lib/__tests__/commonOpts.test.ts b/lib/__tests__/commonOpts.test.ts index 1354c0ec5..b971d8e36 100644 --- a/lib/__tests__/commonOpts.test.ts +++ b/lib/__tests__/commonOpts.test.ts @@ -1,15 +1,31 @@ -// @ts-nocheck -const { +import { CMS_PUBLISH_MODE, DEFAULT_CMS_PUBLISH_MODE, -} = require('@hubspot/local-dev-lib/constants/files'); -const { +} from '@hubspot/local-dev-lib/constants/files'; +import { getAndLoadConfigIfNeeded, getAccountId, getAccountConfig, loadConfigFromEnvironment, -} = require('@hubspot/local-dev-lib/config'); -const { getCmsPublishMode } = require('../commonOpts'); +} from '@hubspot/local-dev-lib/config'; +import { getCmsPublishMode } from '../commonOpts'; +import { CmsPublishMode } from '@hubspot/local-dev-lib/types/Files'; +import { Arguments } from 'yargs'; + +type CmsPublishModeArgs = { + cmsPublishMode?: CmsPublishMode; + account?: number | string; +}; + +function buildArguments( + args: CmsPublishModeArgs +): Arguments { + return { + _: [], + $0: '', + ...args, + }; +} jest.mock('@hubspot/local-dev-lib/config'); jest.mock('@hubspot/local-dev-lib/logger'); @@ -39,62 +55,76 @@ describe('lib/commonOpts', () => { }; afterEach(() => { - getAndLoadConfigIfNeeded.mockReset(); - getAccountId.mockReset(); - getAccountConfig.mockReset(); - loadConfigFromEnvironment.mockReset(); + jest.resetAllMocks(); }); describe('cms publish mode option precedence', () => { describe('1. --cmsPublishMode', () => { it('should return the cms publish mode specified by the command option if present.', () => { - getAndLoadConfigIfNeeded.mockReturnValue( + (getAndLoadConfigIfNeeded as jest.Mock).mockReturnValue( configWithDefaultCmsPublishMode ); - getAccountConfig.mockReturnValue(devAccountConfig); + (getAccountConfig as jest.Mock).mockReturnValue(devAccountConfig); expect( - getCmsPublishMode({ cmsPublishMode: CMS_PUBLISH_MODE.draft }) + getCmsPublishMode( + buildArguments({ + cmsPublishMode: CMS_PUBLISH_MODE.draft, + }) + ) ).toBe(CMS_PUBLISH_MODE.draft); expect( - getCmsPublishMode({ cmsPublishMode: CMS_PUBLISH_MODE.publish }) + getCmsPublishMode( + buildArguments({ + cmsPublishMode: CMS_PUBLISH_MODE.publish, + }) + ) ).toBe(CMS_PUBLISH_MODE.publish); - expect(getCmsPublishMode({ cmsPublishMode: 'undefined-mode' })).toBe( - 'undefined-mode' - ); }); }); describe('2. hubspot.config.yml -> config.accounts[x].defaultCmsPublishMode', () => { it('should return the defaultCmsPublishMode specified by the account specific config if present.', () => { - getAndLoadConfigIfNeeded.mockReturnValue( + (getAndLoadConfigIfNeeded as jest.Mock).mockReturnValue( configWithDefaultCmsPublishMode ); - getAccountId.mockReturnValue(accounts.DEV); - getAccountConfig.mockReturnValue(devAccountConfig); - loadConfigFromEnvironment.mockReturnValue(undefined); - expect(getCmsPublishMode({ account: accounts.DEV })).toBe( - CMS_PUBLISH_MODE.draft - ); + (getAccountId as jest.Mock).mockReturnValue(accounts.DEV); + (getAccountConfig as jest.Mock).mockReturnValue(devAccountConfig); + (loadConfigFromEnvironment as jest.Mock).mockReturnValue(undefined); + expect( + getCmsPublishMode( + buildArguments({ + account: accounts.DEV, + }) + ) + ).toBe(CMS_PUBLISH_MODE.draft); }); }); describe('3. hubspot.config.yml -> config.defaultCmsPublishMode', () => { it('should return the defaultCmsPublishMode specified by the config if present.', () => { - getAndLoadConfigIfNeeded.mockReturnValue( + (getAndLoadConfigIfNeeded as jest.Mock).mockReturnValue( configWithDefaultCmsPublishMode ); - getAccountId.mockReturnValue(accounts.PROD); - getAccountConfig.mockReturnValue(prodAccountConfig); - loadConfigFromEnvironment.mockReturnValue(undefined); - expect(getCmsPublishMode({ account: accounts.PROD })).toBe( - CMS_PUBLISH_MODE.draft - ); + (getAccountId as jest.Mock).mockReturnValue(accounts.PROD); + (getAccountConfig as jest.Mock).mockReturnValue(prodAccountConfig); + (loadConfigFromEnvironment as jest.Mock).mockReturnValue(undefined); + expect( + getCmsPublishMode( + buildArguments({ + account: accounts.PROD, + }) + ) + ).toBe(CMS_PUBLISH_MODE.draft); }); }); describe('4. DEFAULT_CMS_PUBLISH_MODE', () => { it('should return the defaultCmsPubishMode specified by the config if present.', () => { - loadConfigFromEnvironment.mockReturnValue(undefined); - expect(getCmsPublishMode({ account: 'xxxxx' })).toBe( - DEFAULT_CMS_PUBLISH_MODE - ); + (loadConfigFromEnvironment as jest.Mock).mockReturnValue(undefined); + expect( + getCmsPublishMode( + buildArguments({ + account: 'xxxxx', + }) + ) + ).toBe(DEFAULT_CMS_PUBLISH_MODE); }); }); }); diff --git a/lib/__tests__/dependencyManagement.test.ts b/lib/__tests__/dependencyManagement.test.ts index 52b60922b..e18c916c8 100644 --- a/lib/__tests__/dependencyManagement.test.ts +++ b/lib/__tests__/dependencyManagement.test.ts @@ -1,4 +1,3 @@ -// @ts-nocheck jest.mock('../projects'); jest.mock('@hubspot/local-dev-lib/logger'); jest.mock('@hubspot/local-dev-lib/fs'); @@ -8,18 +7,18 @@ jest.mock('fs', () => ({ existsSync: jest.fn().mockReturnValue(true), })); -const util = require('util'); -const { +import util from 'util'; +import { isGloballyInstalled, installPackages, getProjectPackageJsonLocations, getLatestCliVersion, -} = require('../dependencyManagement'); -const { walk } = require('@hubspot/local-dev-lib/fs'); -const path = require('path'); -const { getProjectConfig } = require('../projects'); -const SpinniesManager = require('../ui/SpinniesManager'); -const { existsSync } = require('fs'); +} from '../dependencyManagement'; +import { walk } from '@hubspot/local-dev-lib/fs'; +import path from 'path'; +import { getProjectConfig } from '../projects'; +import SpinniesManager from '../ui/SpinniesManager'; +import { existsSync } from 'fs'; describe('lib/dependencyManagement', () => { let execMock; @@ -34,8 +33,10 @@ describe('lib/dependencyManagement', () => { beforeEach(() => { execMock = jest.fn(); - util.promisify = jest.fn().mockReturnValue(execMock); - getProjectConfig.mockResolvedValue({ + util.promisify = jest + .fn() + .mockReturnValue(execMock) as unknown as typeof util.promisify; + (getProjectConfig as jest.Mock).mockResolvedValue({ projectDir, projectConfig: { srcDir, @@ -52,7 +53,9 @@ describe('lib/dependencyManagement', () => { .fn() .mockResolvedValueOnce({ stdout: JSON.stringify({ latest, next }) }); - util.promisify = jest.fn().mockReturnValueOnce(execMock); + util.promisify = jest + .fn() + .mockReturnValueOnce(execMock) as unknown as typeof util.promisify; const actual = await getLatestCliVersion(); expect(actual).toEqual({ latest, next }); }); @@ -62,7 +65,9 @@ describe('lib/dependencyManagement', () => { execMock = jest.fn().mockImplementationOnce(() => { throw new Error(errorMessage); }); - util.promisify = jest.fn().mockReturnValueOnce(execMock); + util.promisify = jest + .fn() + .mockReturnValueOnce(execMock) as unknown as typeof util.promisify; await expect(() => getLatestCliVersion()).rejects.toThrowError( errorMessage ); @@ -81,7 +86,9 @@ describe('lib/dependencyManagement', () => { execMock = jest.fn().mockImplementationOnce(() => { throw new Error('unsuccessful'); }); - util.promisify = jest.fn().mockReturnValueOnce(execMock); + util.promisify = jest + .fn() + .mockReturnValueOnce(execMock) as unknown as typeof util.promisify; const actual = await isGloballyInstalled('npm'); expect(actual).toBe(false); expect(execMock).toHaveBeenCalledTimes(1); @@ -152,9 +159,9 @@ describe('lib/dependencyManagement', () => { path.join(extensionsDir, 'package.json'), ]; - walk.mockResolvedValue(installLocations); + (walk as jest.Mock).mockResolvedValue(installLocations); - getProjectConfig.mockResolvedValue({ + (getProjectConfig as jest.Mock).mockResolvedValue({ projectDir, projectConfig: { srcDir, @@ -179,16 +186,18 @@ describe('lib/dependencyManagement', () => { } }); - util.promisify = jest.fn().mockReturnValue(execMock); + util.promisify = jest + .fn() + .mockReturnValue(execMock) as unknown as typeof util.promisify; const installLocations = [ path.join(appFunctionsDir, 'package.json'), path.join(extensionsDir, 'package.json'), ]; - walk.mockResolvedValue(installLocations); + (walk as jest.Mock).mockResolvedValue(installLocations); - getProjectConfig.mockResolvedValue({ + (getProjectConfig as jest.Mock).mockResolvedValue({ projectDir, projectConfig: { srcDir, @@ -220,7 +229,7 @@ describe('lib/dependencyManagement', () => { describe('getProjectPackageJsonFiles', () => { it('should throw an error when ran outside the boundary of a project', async () => { - getProjectConfig.mockResolvedValue({}); + (getProjectConfig as jest.Mock).mockResolvedValue({}); await expect(() => getProjectPackageJsonLocations()).rejects.toThrowError( 'No project detected. Run this command from a project directory.' ); @@ -230,14 +239,16 @@ describe('lib/dependencyManagement', () => { execMock = jest.fn().mockImplementation(() => { throw new Error('OH NO'); }); - util.promisify = jest.fn().mockReturnValue(execMock); + util.promisify = jest + .fn() + .mockReturnValue(execMock) as unknown as typeof util.promisify; await expect(() => getProjectPackageJsonLocations()).rejects.toThrowError( /This command depends on npm, install/ ); }); it('should throw an error if the project directory does not exist', async () => { - existsSync.mockReturnValueOnce(false); + (existsSync as jest.Mock).mockReturnValueOnce(false); await expect(() => getProjectPackageJsonLocations()).rejects.toThrowError( new RegExp( `No dependencies to install. The project ${projectName} folder might be missing component or subcomponent files.` @@ -255,14 +266,14 @@ describe('lib/dependencyManagement', () => { path.join(nodeModulesDir, 'package.json'), ]; - walk.mockResolvedValue(installLocations); + (walk as jest.Mock).mockResolvedValue(installLocations); const actual = await getProjectPackageJsonLocations(); expect(actual).toEqual([appFunctionsDir, extensionsDir]); }); it('should throw an error if no package.json files are found', async () => { - walk.mockResolvedValue([]); + (walk as jest.Mock).mockResolvedValue([]); await expect(() => getProjectPackageJsonLocations()).rejects.toThrowError( new RegExp( diff --git a/lib/__tests__/projects.test.ts b/lib/__tests__/projects.test.ts index f7f762f4f..528a4659f 100644 --- a/lib/__tests__/projects.test.ts +++ b/lib/__tests__/projects.test.ts @@ -1,10 +1,9 @@ -// @ts-nocheck -const fs = require('fs'); -const os = require('os'); -const path = require('path'); -const { EXIT_CODES } = require('../enums/exitCodes'); -const projects = require('../projects'); -const { logger } = require('@hubspot/local-dev-lib/logger'); +import fs from 'fs'; +import os from 'os'; +import path from 'path'; +import { EXIT_CODES } from '../enums/exitCodes'; +import * as projects from '../projects'; +import { logger } from '@hubspot/local-dev-lib/logger'; jest.mock('@hubspot/local-dev-lib/logger'); @@ -31,7 +30,7 @@ describe('lib/projects', () => { }); it('rejects undefined configuration', () => { - projects.validateProjectConfig(null, projectDir); + (projects.validateProjectConfig as jest.Mock)(null, projectDir); expect(exitMock).toHaveBeenCalledWith(EXIT_CODES.ERROR); expect(logger.error).toHaveBeenCalledWith( @@ -42,7 +41,10 @@ describe('lib/projects', () => { }); it('rejects configuration with missing name', () => { - projects.validateProjectConfig({ srcDir: '.' }, projectDir); + (projects.validateProjectConfig as jest.Mock)( + { srcDir: '.' }, + projectDir + ); expect(exitMock).toHaveBeenCalledWith(EXIT_CODES.ERROR); expect(logger.error).toHaveBeenCalledWith( @@ -51,7 +53,10 @@ describe('lib/projects', () => { }); it('rejects configuration with missing srcDir', () => { - projects.validateProjectConfig({ name: 'hello' }, projectDir); + (projects.validateProjectConfig as jest.Mock)( + { name: 'hello' }, + projectDir + ); expect(exitMock).toHaveBeenCalledWith(EXIT_CODES.ERROR); expect(logger.error).toHaveBeenCalledWith( @@ -61,7 +66,7 @@ describe('lib/projects', () => { describe('rejects configuration with srcDir outside project directory', () => { it('for parent directory', () => { - projects.validateProjectConfig( + (projects.validateProjectConfig as jest.Mock)( { name: 'hello', srcDir: '..' }, projectDir ); @@ -73,7 +78,7 @@ describe('lib/projects', () => { }); it('for root directory', () => { - projects.validateProjectConfig( + (projects.validateProjectConfig as jest.Mock)( { name: 'hello', srcDir: '/' }, projectDir ); @@ -87,7 +92,10 @@ describe('lib/projects', () => { it('for complicated directory', () => { const srcDir = './src/././../src/../../src'; - projects.validateProjectConfig({ name: 'hello', srcDir }, projectDir); + (projects.validateProjectConfig as jest.Mock)( + { name: 'hello', srcDir }, + projectDir + ); expect(exitMock).toHaveBeenCalledWith(EXIT_CODES.ERROR); expect(logger.error).toHaveBeenCalledWith( @@ -97,7 +105,7 @@ describe('lib/projects', () => { }); it('rejects configuration with srcDir that does not exist', () => { - projects.validateProjectConfig( + (projects.validateProjectConfig as jest.Mock)( { name: 'hello', srcDir: 'foo' }, projectDir ); @@ -110,7 +118,7 @@ describe('lib/projects', () => { describe('accepts configuration with valid srcDir', () => { it('for current directory', () => { - projects.validateProjectConfig( + (projects.validateProjectConfig as jest.Mock)( { name: 'hello', srcDir: '.' }, projectDir ); @@ -120,7 +128,7 @@ describe('lib/projects', () => { }); it('for relative directory', () => { - projects.validateProjectConfig( + (projects.validateProjectConfig as jest.Mock)( { name: 'hello', srcDir: './src' }, projectDir ); @@ -130,7 +138,7 @@ describe('lib/projects', () => { }); it('for implied relative directory', () => { - projects.validateProjectConfig( + (projects.validateProjectConfig as jest.Mock)( { name: 'hello', srcDir: 'src' }, projectDir ); diff --git a/lib/commonOpts.ts b/lib/commonOpts.ts index c2d37d152..8a3115a79 100644 --- a/lib/commonOpts.ts +++ b/lib/commonOpts.ts @@ -118,7 +118,10 @@ export function getAccountId( } export function getCmsPublishMode( - options: Arguments<{ cmsPublishMode?: CmsPublishMode }> + options: Arguments<{ + cmsPublishMode?: CmsPublishMode; + account?: number | string; + }> ): CmsPublishMode { // 1. --cmsPublishMode const { cmsPublishMode } = options;