Skip to content

Commit

Permalink
Address feedback p4
Browse files Browse the repository at this point in the history
  • Loading branch information
kemmerle committed Jan 17, 2025
1 parent 71f3050 commit 37a009a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
27 changes: 16 additions & 11 deletions lib/__tests__/commonOpts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import { getCmsPublishMode } from '../commonOpts';
import { CmsPublishMode } from '@hubspot/local-dev-lib/types/Files';
import { Arguments } from 'yargs';

const mockedGetAccountId = getAccountId as jest.Mock;
const mockedGetAccountConfig = getAccountConfig as jest.Mock;
const mockedGetAndLoadConfigIfNeeded = getAndLoadConfigIfNeeded as jest.Mock;
const mockedLoadConfigFromEnvironment = loadConfigFromEnvironment as jest.Mock;

type CmsPublishModeArgs = {
cmsPublishMode?: CmsPublishMode;
account?: number | string;
Expand Down Expand Up @@ -61,10 +66,10 @@ describe('lib/commonOpts', () => {
describe('cms publish mode option precedence', () => {
describe('1. --cmsPublishMode', () => {
it('should return the cms publish mode specified by the command option if present.', () => {
(getAndLoadConfigIfNeeded as jest.Mock).mockReturnValue(
mockedGetAndLoadConfigIfNeeded.mockReturnValue(
configWithDefaultCmsPublishMode
);
(getAccountConfig as jest.Mock).mockReturnValue(devAccountConfig);
mockedGetAccountConfig.mockReturnValue(devAccountConfig);
expect(
getCmsPublishMode(
buildArguments({
Expand All @@ -83,12 +88,12 @@ describe('lib/commonOpts', () => {
});
describe('2. hubspot.config.yml -> config.accounts[x].defaultCmsPublishMode', () => {
it('should return the defaultCmsPublishMode specified by the account specific config if present.', () => {
(getAndLoadConfigIfNeeded as jest.Mock).mockReturnValue(
mockedGetAndLoadConfigIfNeeded.mockReturnValue(
configWithDefaultCmsPublishMode
);
(getAccountId as jest.Mock).mockReturnValue(accounts.DEV);
(getAccountConfig as jest.Mock).mockReturnValue(devAccountConfig);
(loadConfigFromEnvironment as jest.Mock).mockReturnValue(undefined);
mockedGetAccountId.mockReturnValue(accounts.DEV);
mockedGetAccountConfig.mockReturnValue(devAccountConfig);
mockedLoadConfigFromEnvironment.mockReturnValue(undefined);
expect(
getCmsPublishMode(
buildArguments({
Expand All @@ -100,12 +105,12 @@ describe('lib/commonOpts', () => {
});
describe('3. hubspot.config.yml -> config.defaultCmsPublishMode', () => {
it('should return the defaultCmsPublishMode specified by the config if present.', () => {
(getAndLoadConfigIfNeeded as jest.Mock).mockReturnValue(
mockedGetAndLoadConfigIfNeeded.mockReturnValue(
configWithDefaultCmsPublishMode
);
(getAccountId as jest.Mock).mockReturnValue(accounts.PROD);
(getAccountConfig as jest.Mock).mockReturnValue(prodAccountConfig);
(loadConfigFromEnvironment as jest.Mock).mockReturnValue(undefined);
mockedGetAccountId.mockReturnValue(accounts.PROD);
mockedGetAccountConfig.mockReturnValue(prodAccountConfig);
mockedLoadConfigFromEnvironment.mockReturnValue(undefined);
expect(
getCmsPublishMode(
buildArguments({
Expand All @@ -117,7 +122,7 @@ describe('lib/commonOpts', () => {
});
describe('4. DEFAULT_CMS_PUBLISH_MODE', () => {
it('should return the defaultCmsPubishMode specified by the config if present.', () => {
(loadConfigFromEnvironment as jest.Mock).mockReturnValue(undefined);
mockedLoadConfigFromEnvironment.mockReturnValue(undefined);
expect(
getCmsPublishMode(
buildArguments({
Expand Down
49 changes: 23 additions & 26 deletions lib/__tests__/dependencyManagement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ describe('lib/dependencyManagement', () => {
const projectName = 'super cool test project';
const installLocations = [appFunctionsDir, extensionsDir];

beforeEach(() => {
execMock = jest.fn();
util.promisify = jest
function mockedPromisify(execMock: jest.Mock): typeof util.promisify {
return jest
.fn()
.mockReturnValue(execMock) as unknown as typeof util.promisify;
(getProjectConfig as jest.Mock).mockResolvedValue({
}

const mockedWalk = walk as jest.Mock;
const mockedGetProjectConfig = getProjectConfig as jest.Mock;

beforeEach(() => {
execMock = jest.fn();
util.promisify = mockedPromisify(execMock);
mockedGetProjectConfig.mockResolvedValue({
projectDir,
projectConfig: {
srcDir,
Expand All @@ -53,9 +60,7 @@ describe('lib/dependencyManagement', () => {
.fn()
.mockResolvedValueOnce({ stdout: JSON.stringify({ latest, next }) });

util.promisify = jest
.fn()
.mockReturnValueOnce(execMock) as unknown as typeof util.promisify;
util.promisify = mockedPromisify(execMock);
const actual = await getLatestCliVersion();
expect(actual).toEqual({ latest, next });
});
Expand All @@ -65,9 +70,7 @@ describe('lib/dependencyManagement', () => {
execMock = jest.fn().mockImplementationOnce(() => {
throw new Error(errorMessage);
});
util.promisify = jest
.fn()
.mockReturnValueOnce(execMock) as unknown as typeof util.promisify;
util.promisify = mockedPromisify(execMock);
await expect(() => getLatestCliVersion()).rejects.toThrowError(
errorMessage
);
Expand All @@ -86,9 +89,7 @@ describe('lib/dependencyManagement', () => {
execMock = jest.fn().mockImplementationOnce(() => {
throw new Error('unsuccessful');
});
util.promisify = jest
.fn()
.mockReturnValueOnce(execMock) as unknown as typeof util.promisify;
util.promisify = mockedPromisify(execMock);
const actual = await isGloballyInstalled('npm');
expect(actual).toBe(false);
expect(execMock).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -159,9 +160,9 @@ describe('lib/dependencyManagement', () => {
path.join(extensionsDir, 'package.json'),
];

(walk as jest.Mock).mockResolvedValue(installLocations);
mockedWalk.mockResolvedValue(installLocations);

(getProjectConfig as jest.Mock).mockResolvedValue({
mockedGetProjectConfig.mockResolvedValue({
projectDir,
projectConfig: {
srcDir,
Expand All @@ -186,18 +187,16 @@ describe('lib/dependencyManagement', () => {
}
});

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

const installLocations = [
path.join(appFunctionsDir, 'package.json'),
path.join(extensionsDir, 'package.json'),
];

(walk as jest.Mock).mockResolvedValue(installLocations);
mockedWalk.mockResolvedValue(installLocations);

(getProjectConfig as jest.Mock).mockResolvedValue({
mockedGetProjectConfig.mockResolvedValue({
projectDir,
projectConfig: {
srcDir,
Expand Down Expand Up @@ -229,7 +228,7 @@ describe('lib/dependencyManagement', () => {

describe('getProjectPackageJsonFiles', () => {
it('should throw an error when ran outside the boundary of a project', async () => {
(getProjectConfig as jest.Mock).mockResolvedValue({});
mockedGetProjectConfig.mockResolvedValue({});
await expect(() => getProjectPackageJsonLocations()).rejects.toThrowError(
'No project detected. Run this command from a project directory.'
);
Expand All @@ -239,9 +238,7 @@ describe('lib/dependencyManagement', () => {
execMock = jest.fn().mockImplementation(() => {
throw new Error('OH NO');
});
util.promisify = jest
.fn()
.mockReturnValue(execMock) as unknown as typeof util.promisify;
util.promisify = mockedPromisify(execMock);
await expect(() => getProjectPackageJsonLocations()).rejects.toThrowError(
/This command depends on npm, install/
);
Expand All @@ -266,14 +263,14 @@ describe('lib/dependencyManagement', () => {
path.join(nodeModulesDir, 'package.json'),
];

(walk as jest.Mock).mockResolvedValue(installLocations);
mockedWalk.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 as jest.Mock).mockResolvedValue([]);
mockedWalk.mockResolvedValue([]);

await expect(() => getProjectPackageJsonLocations()).rejects.toThrowError(
new RegExp(
Expand Down

0 comments on commit 37a009a

Please sign in to comment.