Skip to content

Commit

Permalink
test: fixes (#20402)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice authored Feb 14, 2023
1 parent b27ebee commit 42eab15
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 86 deletions.
1 change: 0 additions & 1 deletion lib/config/migrate-validate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { migrateAndValidate } from './migrate-validate';
let config: RenovateConfig;

beforeEach(() => {
jest.resetAllMocks();
config = getConfig();
});

Expand Down
4 changes: 0 additions & 4 deletions lib/config/presets/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ const projectPath = '/api/v4/projects/some%2Frepo';
const basePath = `${projectPath}/repository`;

describe('config/presets/gitlab/index', () => {
beforeEach(() => {
jest.resetAllMocks();
});

describe('getPreset()', () => {
it('throws EXTERNAL_HOST_ERROR', async () => {
httpMock.scope(gitlabApiHost).get(projectPath).reply(500);
Expand Down
1 change: 0 additions & 1 deletion lib/config/presets/local/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const gitlab = mocked(_gitlab);

describe('config/presets/local/index', () => {
beforeEach(() => {
jest.resetAllMocks();
const preset = { resolved: 'preset' };
platform.getRawFile.mockResolvedValue('{ resolved: "preset" }');
gitea.getPresetFromEndpoint.mockResolvedValueOnce(preset);
Expand Down
1 change: 0 additions & 1 deletion lib/config/presets/npm/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ jest.mock('delay');

describe('config/presets/npm/index', () => {
beforeEach(() => {
jest.resetAllMocks();
GlobalConfig.reset();
});

Expand Down
2 changes: 0 additions & 2 deletions lib/logger/err-serializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ describe('logger/err-serializer', () => {
const baseUrl = 'https://github.com';

beforeEach(() => {
// reset module
jest.resetAllMocks();
// clean up hostRules
hostRules.clear();
hostRules.add({
Expand Down
4 changes: 0 additions & 4 deletions lib/modules/datasource/artifactory/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ function getPath(folder: string): string {
}

describe('modules/datasource/artifactory/index', () => {
beforeEach(() => {
jest.resetAllMocks();
});

describe('getReleases', () => {
it('parses real data (folders): with slash at the end', async () => {
httpMock
Expand Down
1 change: 0 additions & 1 deletion lib/modules/datasource/clojure/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ describe('modules/datasource/clojure/index', () => {
matchHost: 'custom.registry.renovatebot.com',
token: '123test',
});
jest.resetAllMocks();
});

afterEach(() => {
Expand Down
4 changes: 0 additions & 4 deletions lib/modules/datasource/docker/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ describe('modules/datasource/docker/index', () => {
hostRules.hosts.mockReturnValue([]);
});

afterEach(() => {
jest.resetAllMocks();
});

describe('getRegistryRepository', () => {
it('handles local registries', () => {
const res = getRegistryRepository(
Expand Down
2 changes: 0 additions & 2 deletions lib/modules/datasource/go/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ const datasource = new GoDatasource();
describe('modules/datasource/go/index', () => {
describe('getReleases', () => {
beforeEach(() => {
jest.resetAllMocks();
hostRules.find.mockReturnValue({});
hostRules.hosts.mockReturnValue([]);
});

afterEach(() => {
jest.resetAllMocks();
delete process.env.GOPROXY;
});

Expand Down
5 changes: 0 additions & 5 deletions lib/modules/datasource/go/releases-direct.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@ describe('modules/datasource/go/releases-direct', () => {
);

beforeEach(() => {
jest.resetAllMocks();
hostRules.find.mockReturnValue({});
hostRules.hosts.mockReturnValue([]);
});

afterEach(() => {
jest.resetAllMocks();
});

describe('getReleases', () => {
it('returns null for null getDatasource result', async () => {
getDatasourceSpy.mockResolvedValueOnce(null);
Expand Down
4 changes: 0 additions & 4 deletions lib/modules/datasource/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ jest.mock('./metadata-manual', () => ({
}));

describe('modules/datasource/index', () => {
beforeEach(() => {
jest.resetAllMocks();
});

afterEach(() => {
datasources.delete(datasource);
});
Expand Down
4 changes: 0 additions & 4 deletions lib/util/exec/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,6 @@ describe('util/exec/common', () => {
const stdout = 'out message';
const stderr = 'err message';

beforeEach(() => {
jest.resetAllMocks();
});

describe('rawExec', () => {
it('command exits with code 0', async () => {
const cmd = 'ls -l';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ describe('util/github/graphql/cache-strategies/package-cache-strategy', () => {
const cacheGet = jest.spyOn(packageCache, 'get');
const cacheSet = jest.spyOn(packageCache, 'set');

beforeEach(() => {
jest.resetAllMocks();
});

it('reconciles old cache record with new items', async () => {
const oldItems = {
'1': { version: '1', releaseTimestamp: isoTs('2020-01-01 10:00') },
Expand Down
60 changes: 29 additions & 31 deletions lib/workers/global/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { CONFIG_PRESETS_INVALID } from '../../constants/error-messages';
import { DockerDatasource } from '../../modules/datasource/docker';
import * as _platform from '../../modules/platform';
import * as secrets from '../../util/sanitize';
import * as _repositoryWorker from '../repository';
import * as _configParser from './config/parse';
import * as _limits from './limits';
import * as repositoryWorker from '../repository';
import * as configParser from './config/parse';
import * as limits from './limits';
import * as globalWorker from '.';

jest.mock('../repository');
Expand All @@ -18,28 +18,24 @@ jest.mock('../../config/presets');

jest.mock('fs-extra');
const fs = mocked(_fs);
const platform = mocked(_platform);

// imports are readonly
const repositoryWorker = _repositoryWorker;
const configParser: jest.Mocked<typeof _configParser> = _configParser as never;
const platform: jest.Mocked<typeof _platform> = _platform as never;
const presets = mocked(_presets);
const limits = _limits;

const addSecretForSanitizing = jest.spyOn(secrets, 'addSecretForSanitizing');
const parseConfigs = jest.spyOn(configParser, 'parseConfigs');

describe('workers/global/index', () => {
beforeEach(() => {
jest.resetAllMocks();
logger.getProblems.mockImplementationOnce(() => []);
configParser.parseConfigs = jest.fn();
platform.initPlatform.mockImplementation((input) => Promise.resolve(input));
delete process.env.AWS_SECRET_ACCESS_KEY;
delete process.env.AWS_SESSION_TOKEN;
});

it('handles config warnings and errors', async () => {
configParser.parseConfigs.mockResolvedValueOnce({
parseConfigs.mockResolvedValueOnce({
repositories: [],
maintainYarnLock: true,
foo: 1,
Expand All @@ -51,7 +47,7 @@ describe('workers/global/index', () => {
});

it('resolves global presets immediately', async () => {
configParser.parseConfigs.mockResolvedValueOnce({
parseConfigs.mockResolvedValueOnce({
repositories: [],
globalExtends: [':pinVersions'],
hostRules: [{ matchHost: 'github.com', token: 'abc123' }],
Expand All @@ -61,33 +57,33 @@ describe('workers/global/index', () => {
expect(presets.resolveConfigPresets).toHaveBeenCalledWith({
extends: [':pinVersions'],
});
expect(parseConfigs).toHaveBeenCalledTimes(1);
});

it('throws if global presets could not be resolved', async () => {
configParser.parseConfigs.mockResolvedValueOnce({
repositories: [],
globalExtends: [':pinVersions'],
});
presets.resolveConfigPresets.mockImplementation(() => {
presets.resolveConfigPresets.mockImplementationOnce(() => {
throw new Error('some-error');
});
await expect(
globalWorker.resolveGlobalExtends(['some-preset'])
).rejects.toThrow(CONFIG_PRESETS_INVALID);
expect(presets.resolveConfigPresets).toHaveBeenCalled();
expect(parseConfigs).not.toHaveBeenCalled();
});

it('handles zero repos', async () => {
configParser.parseConfigs.mockResolvedValueOnce({
parseConfigs.mockResolvedValueOnce({
baseDir: '/tmp/base',
cacheDir: '/tmp/cache',
repositories: [],
});
await expect(globalWorker.start()).resolves.toBe(0);
expect(parseConfigs).toHaveBeenCalledTimes(1);
expect(repositoryWorker.renovateRepository).not.toHaveBeenCalled();
});

it('processes repositories', async () => {
configParser.parseConfigs.mockResolvedValueOnce({
parseConfigs.mockResolvedValueOnce({
gitAuthor: 'a@b.com',
enabled: true,
repositories: ['a', 'b'],
Expand All @@ -99,14 +95,15 @@ describe('workers/global/index', () => {
},
],
});
await globalWorker.start();
expect(configParser.parseConfigs).toHaveBeenCalledTimes(1);
await expect(globalWorker.start()).resolves.toBe(0);
expect(parseConfigs).toHaveBeenCalledTimes(1);
expect(repositoryWorker.renovateRepository).toHaveBeenCalledTimes(2);
});

it('processes repositories break', async () => {
limits.isLimitReached = jest.fn(() => true);
configParser.parseConfigs.mockResolvedValueOnce({
const isLimitReached = jest.spyOn(limits, 'isLimitReached');
isLimitReached.mockReturnValue(true);
parseConfigs.mockResolvedValueOnce({
gitAuthor: 'a@b.com',
enabled: true,
repositories: ['a', 'b'],
Expand All @@ -119,12 +116,13 @@ describe('workers/global/index', () => {
],
});
await globalWorker.start();
expect(configParser.parseConfigs).toHaveBeenCalledTimes(1);
expect(parseConfigs).toHaveBeenCalledTimes(1);
expect(repositoryWorker.renovateRepository).toHaveBeenCalledTimes(0);
isLimitReached.mockReset();
});

it('exits with non-zero when errors are logged', async () => {
configParser.parseConfigs.mockResolvedValueOnce({
parseConfigs.mockResolvedValueOnce({
baseDir: '/tmp/base',
cacheDir: '/tmp/cache',
repositories: [],
Expand All @@ -140,7 +138,7 @@ describe('workers/global/index', () => {
});

it('exits with zero when warnings are logged', async () => {
configParser.parseConfigs.mockResolvedValueOnce({
parseConfigs.mockResolvedValueOnce({
baseDir: '/tmp/base',
cacheDir: '/tmp/cache',
repositories: [],
Expand All @@ -157,31 +155,31 @@ describe('workers/global/index', () => {

describe('processes platforms', () => {
it('github', async () => {
configParser.parseConfigs.mockResolvedValueOnce({
parseConfigs.mockResolvedValueOnce({
repositories: ['a'],
platform: 'github',
endpoint: 'https://github.com/',
});
await globalWorker.start();
expect(configParser.parseConfigs).toHaveBeenCalledTimes(1);
expect(parseConfigs).toHaveBeenCalledTimes(1);
expect(repositoryWorker.renovateRepository).toHaveBeenCalledTimes(1);
});

it('gitlab', async () => {
configParser.parseConfigs.mockResolvedValueOnce({
parseConfigs.mockResolvedValueOnce({
repositories: [{ repository: 'a' }],
platform: 'gitlab',
endpoint: 'https://my.gitlab.com/',
});
await globalWorker.start();
expect(configParser.parseConfigs).toHaveBeenCalledTimes(1);
expect(parseConfigs).toHaveBeenCalledTimes(1);
expect(repositoryWorker.renovateRepository).toHaveBeenCalledTimes(1);
});
});

describe('write repositories to file', () => {
it('successfully write file', async () => {
configParser.parseConfigs.mockResolvedValueOnce({
parseConfigs.mockResolvedValueOnce({
repositories: ['myOrg/myRepo'],
platform: 'github',
endpoint: 'https://github.com/',
Expand All @@ -194,7 +192,7 @@ describe('workers/global/index', () => {
'/tmp/renovate-output.json',
'["myOrg/myRepo"]'
);
expect(configParser.parseConfigs).toHaveBeenCalledTimes(1);
expect(parseConfigs).toHaveBeenCalledTimes(1);
expect(repositoryWorker.renovateRepository).toHaveBeenCalledTimes(0);
});
});
Expand Down
11 changes: 5 additions & 6 deletions lib/workers/repository/config-migration/branch/rebase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('workers/repository/config-migration/branch/rebase', () => {
'applyPrettierFormatting'
);

beforeAll(() => {
beforeEach(() => {
GlobalConfig.set({
localDir: '',
});
Expand All @@ -47,8 +47,6 @@ describe('workers/repository/config-migration/branch/rebase', () => {
};

beforeEach(() => {
jest.resetAllMocks();
GlobalConfig.reset();
config = {
...getConfig(),
repository: 'some/repo',
Expand All @@ -72,16 +70,15 @@ describe('workers/repository/config-migration/branch/rebase', () => {
])(
'does nothing if branch is up to date (%s)',
async (filename, rawConfig) => {
git.getFile
.mockResolvedValueOnce(rawConfig)
.mockResolvedValueOnce(rawConfig);
git.getFile.mockResolvedValueOnce(rawConfig);
migratedConfigData.filename = filename;
migratedConfigData.content = rawConfig;

await rebaseMigrationBranch(config, migratedConfigData);

expect(checkoutBranch).toHaveBeenCalledTimes(0);
expect(git.commitFiles).toHaveBeenCalledTimes(0);
expect(git.getFile).toHaveBeenCalledTimes(1);
}
);

Expand All @@ -90,6 +87,7 @@ describe('workers/repository/config-migration/branch/rebase', () => {
['renovate.json5', renovateConfigJson5],
])('rebases migration branch (%s)', async (filename, rawConfig) => {
git.isBranchBehindBase.mockResolvedValueOnce(true);
prettierSpy.mockResolvedValueOnce('');
migratedConfigData.filename = filename;
migratedConfigData.content = rawConfig;

Expand Down Expand Up @@ -157,6 +155,7 @@ describe('workers/repository/config-migration/branch/rebase', () => {
])('rebases via platform (%s)', async (filename, rawConfig) => {
config.platformCommit = true;
git.isBranchBehindBase.mockResolvedValueOnce(true);
prettierSpy.mockResolvedValueOnce('');
migratedConfigData.filename = filename;
migratedConfigData.content = rawConfig;

Expand Down
5 changes: 3 additions & 2 deletions lib/workers/repository/process/lookup/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ describe('workers/repository/process/lookup/index', () => {
config.manager = 'npm';
config.versioning = npmVersioningId;
config.rangeStrategy = 'replace';
jest.resetAllMocks();
jest
.spyOn(GitRefsDatasource.prototype, 'getReleases')
.mockResolvedValueOnce({
Expand All @@ -61,7 +60,9 @@ describe('workers/repository/process/lookup/index', () => {
});

// TODO: fix mocks
afterEach(() => httpMock.clear(false));
afterEach(() => {
httpMock.clear(false);
});

describe('.lookupUpdates()', () => {
it('returns null if unknown datasource', async () => {
Expand Down
Loading

0 comments on commit 42eab15

Please sign in to comment.