Skip to content

Commit

Permalink
Convert lib/tests to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
kemmerle committed Jan 16, 2025
1 parent 48e9d58 commit 1603267
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 94 deletions.
36 changes: 19 additions & 17 deletions lib/__tests__/ProjectLogsManager.test.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -26,31 +24,33 @@ describe('lib/projects/ProjectLogsManager', () => {
const function1 = {
componentName: 'function1',
type: {
name: 'APP_FUNCTION',
name: SUBCOMPONENT_TYPES.APP_FUNCTION,
},
deployOutput: {
appId,
appFunctionName: 'function1',
},
};
const functions = [
function1,
{
componentName: 'function2',
type: {
name: 'APP_FUNCTION',
name: SUBCOMPONENT_TYPES.APP_FUNCTION,
},
deployOutput: {
appId,
appFunctionName: 'function2',
},
},
];

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: [
{
Expand Down Expand Up @@ -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(
Expand All @@ -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/);
Expand Down Expand Up @@ -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'] },
},
};
Expand All @@ -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();
});
});
});
100 changes: 65 additions & 35 deletions lib/__tests__/commonOpts.test.ts
Original file line number Diff line number Diff line change
@@ -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<CmsPublishModeArgs> {
return {
_: [],
$0: '',
...args,
};
}

jest.mock('@hubspot/local-dev-lib/config');
jest.mock('@hubspot/local-dev-lib/logger');
Expand Down Expand Up @@ -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);
});
});
});
Expand Down
59 changes: 35 additions & 24 deletions lib/__tests__/dependencyManagement.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
jest.mock('../projects');
jest.mock('@hubspot/local-dev-lib/logger');
jest.mock('@hubspot/local-dev-lib/fs');
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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 });
});
Expand All @@ -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
);
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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.'
);
Expand All @@ -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.`
Expand All @@ -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(
Expand Down
Loading

0 comments on commit 1603267

Please sign in to comment.