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 9 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
99 changes: 67 additions & 32 deletions lib/__tests__/commonOpts.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
// @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,
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';

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;
};

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 @@ -38,59 +58,74 @@ describe('lib/commonOpts', () => {
};

afterEach(() => {
getAndLoadConfigIfNeeded.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(
mockedGetAndLoadConfigIfNeeded.mockReturnValue(
configWithDefaultCmsPublishMode
);
getAccountConfig.mockReturnValue(devAccountConfig);
mockedGetAccountConfig.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(
mockedGetAndLoadConfigIfNeeded.mockReturnValue(
configWithDefaultCmsPublishMode
);
getAccountConfig.mockReturnValue(devAccountConfig);
loadConfigFromEnvironment.mockReturnValue(undefined);
expect(getCmsPublishMode({ account: accounts.DEV })).toBe(
CMS_PUBLISH_MODE.draft
);
mockedGetAccountConfig.mockReturnValue(devAccountConfig);
mockedLoadConfigFromEnvironment.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(
mockedGetAndLoadConfigIfNeeded.mockReturnValue(
configWithDefaultCmsPublishMode
);
getAccountConfig.mockReturnValue(prodAccountConfig);
loadConfigFromEnvironment.mockReturnValue(undefined);
expect(getCmsPublishMode({ account: accounts.PROD })).toBe(
CMS_PUBLISH_MODE.draft
);
mockedGetAccountConfig.mockReturnValue(prodAccountConfig);
mockedLoadConfigFromEnvironment.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
);
mockedLoadConfigFromEnvironment.mockReturnValue(undefined);
expect(
getCmsPublishMode(
buildArguments({
account: 'xxxxx',
})
)
).toBe(DEFAULT_CMS_PUBLISH_MODE);
});
});
});
Expand Down
58 changes: 33 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 @@ -8,21 +7,21 @@ jest.mock('fs', () => ({
existsSync: jest.fn().mockReturnValue(true),
}));

kemmerle marked this conversation as resolved.
Show resolved Hide resolved
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;
let execMock: jest.Mock;

const projectDir = path.join('path', 'to', 'project');
const srcDir = 'src';
Expand All @@ -32,10 +31,19 @@ describe('lib/dependencyManagement', () => {
const projectName = 'super cool test project';
const installLocations = [appFunctionsDir, extensionsDir];

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

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

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

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

walk.mockResolvedValue(installLocations);
mockedWalk.mockResolvedValue(installLocations);

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

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

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

walk.mockResolvedValue(installLocations);
mockedWalk.mockResolvedValue(installLocations);

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

describe('getProjectPackageJsonFiles', () => {
it('should throw an error when ran outside the boundary of a project', async () => {
getProjectConfig.mockResolvedValue({});
mockedGetProjectConfig.mockResolvedValue({});
await expect(() => getProjectPackageJsonLocations()).rejects.toThrowError(
'No project detected. Run this command from a project directory.'
);
Expand All @@ -230,14 +238,14 @@ describe('lib/dependencyManagement', () => {
execMock = jest.fn().mockImplementation(() => {
throw new Error('OH NO');
});
util.promisify = jest.fn().mockReturnValue(execMock);
util.promisify = mockedPromisify(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 +263,14 @@ describe('lib/dependencyManagement', () => {
path.join(nodeModulesDir, 'package.json'),
];

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

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