Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Convert lib/tests to TypeScript #1345

Merged
merged 10 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
kemmerle marked this conversation as resolved.
Show resolved Hide resolved
): 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(
kemmerle marked this conversation as resolved.
Show resolved Hide resolved
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
50 changes: 25 additions & 25 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 @@ -7,22 +6,23 @@ jest.mock('fs', () => ({
...jest.requireActual('fs'),
existsSync: jest.fn().mockReturnValue(true),
}));
jest.mock('util');
kemmerle marked this conversation as resolved.
Show resolved Hide resolved

const util = require('util');
const {
import { promisify } 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;
let execMock: jest.Mock;

const projectDir = path.join('path', 'to', 'project');
const srcDir = 'src';
Expand All @@ -34,8 +34,8 @@ describe('lib/dependencyManagement', () => {

beforeEach(() => {
execMock = jest.fn();
util.promisify = jest.fn().mockReturnValue(execMock);
getProjectConfig.mockResolvedValue({
(promisify as unknown as jest.Mock).mockReturnValue(execMock);
(getProjectConfig as jest.Mock).mockResolvedValue({
kemmerle marked this conversation as resolved.
Show resolved Hide resolved
projectDir,
projectConfig: {
srcDir,
Expand All @@ -52,7 +52,7 @@ describe('lib/dependencyManagement', () => {
.fn()
.mockResolvedValueOnce({ stdout: JSON.stringify({ latest, next }) });

util.promisify = jest.fn().mockReturnValueOnce(execMock);
(promisify as unknown as jest.Mock).mockReturnValue(execMock);
const actual = await getLatestCliVersion();
expect(actual).toEqual({ latest, next });
});
Expand All @@ -62,7 +62,7 @@ describe('lib/dependencyManagement', () => {
execMock = jest.fn().mockImplementationOnce(() => {
throw new Error(errorMessage);
});
util.promisify = jest.fn().mockReturnValueOnce(execMock);
(promisify as unknown as jest.Mock).mockReturnValue(execMock);
await expect(() => getLatestCliVersion()).rejects.toThrowError(
errorMessage
);
Expand All @@ -81,7 +81,7 @@ describe('lib/dependencyManagement', () => {
execMock = jest.fn().mockImplementationOnce(() => {
throw new Error('unsuccessful');
});
util.promisify = jest.fn().mockReturnValueOnce(execMock);
(promisify as unknown as jest.Mock).mockReturnValue(execMock);
const actual = await isGloballyInstalled('npm');
expect(actual).toBe(false);
expect(execMock).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -152,9 +152,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 +179,16 @@ describe('lib/dependencyManagement', () => {
}
});

util.promisify = jest.fn().mockReturnValue(execMock);
(promisify as unknown as jest.Mock).mockReturnValue(execMock);

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 +220,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 +230,14 @@ describe('lib/dependencyManagement', () => {
execMock = jest.fn().mockImplementation(() => {
throw new Error('OH NO');
});
util.promisify = jest.fn().mockReturnValue(execMock);
(promisify as unknown as jest.Mock).mockReturnValue(execMock);
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 +255,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
5 changes: 4 additions & 1 deletion lib/commonOpts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ export function getAccountId(
}

export function getCmsPublishMode(
options: Arguments<{ cmsPublishMode?: CmsPublishMode }>
options: Arguments<{
cmsPublishMode?: CmsPublishMode;
account?: number | string;
}>
kemmerle marked this conversation as resolved.
Show resolved Hide resolved
): CmsPublishMode {
// 1. --cmsPublishMode
const { cmsPublishMode } = options;
Expand Down
Loading
Loading