Skip to content

Commit

Permalink
test: jest isolation (#21406)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMagee committed Apr 10, 2023
1 parent 6f6307a commit b23700d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
41 changes: 20 additions & 21 deletions lib/config/presets/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,33 @@ const gitHub = mocked(_github);

const presetIkatyang = Fixtures.getJson('renovate-config-ikatyang.json');

npm.getPreset.mockImplementation(({ repo, presetName }) => {
if (repo === 'renovate-config-ikatyang') {
return presetIkatyang.versions[presetIkatyang['dist-tags'].latest][
'renovate-config'
][presetName!];
}
if (repo === 'renovate-config-notfound') {
throw new Error(PRESET_DEP_NOT_FOUND);
}
if (repo === 'renovate-config-noconfig') {
throw new Error(PRESET_RENOVATE_CONFIG_NOT_FOUND);
}
if (repo === 'renovate-config-throw') {
throw new Error('whoops');
}
if (repo === 'renovate-config-wrongpreset') {
throw new Error(PRESET_NOT_FOUND);
}
return null;
});

describe('config/presets/index', () => {
describe('resolvePreset', () => {
let config: RenovateConfig;

beforeEach(() => {
config = {};
memCache.init();
npm.getPreset.mockImplementation(({ repo, presetName }) => {
if (repo === 'renovate-config-ikatyang') {
return presetIkatyang.versions[presetIkatyang['dist-tags'].latest][
'renovate-config'
][presetName!];
}
if (repo === 'renovate-config-notfound') {
throw new Error(PRESET_DEP_NOT_FOUND);
}
if (repo === 'renovate-config-noconfig') {
throw new Error(PRESET_RENOVATE_CONFIG_NOT_FOUND);
}
if (repo === 'renovate-config-throw') {
throw new Error('whoops');
}
if (repo === 'renovate-config-wrongpreset') {
throw new Error(PRESET_NOT_FOUND);
}
return null;
});
});

it('returns same if no presets', async () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/config/presets/internal/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { validateConfig } from '../../validation';
import * as npm from '../npm';
import * as internal from '.';

jest.mock('./npm');
jest.mock('../npm');
jest.mock('../../../modules/datasource/npm');

jest.spyOn(npm, 'getPreset').mockResolvedValue(undefined);
Expand Down
4 changes: 4 additions & 0 deletions lib/modules/manager/gradle/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ describe('modules/manager/gradle/extract', () => {
it('logs a warning in case parseGradle throws an exception', async () => {
const filename = 'build.gradle';
const err = new Error('unknown');
const fsMock = {
'build.gradle': '',
};
mockFs(fsMock);

jest.spyOn(parser, 'parseGradle').mockImplementationOnce(() => {
throw err;
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/gradle/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,6 @@ describe('modules/manager/gradle/parser', () => {
const fileContents = {
'foo/bar.gradle': key + ' = "' + value + '"',
};
mockFs(fileContents);

test.each`
def | input | output
Expand Down Expand Up @@ -871,6 +870,7 @@ describe('modules/manager/gradle/parser', () => {
${'base="foo"'} | ${'apply(from = File(base, "bar.gradle"))'} | ${validOutput}
${'base="foo"'} | ${'apply(from = File("${base}", "bar.gradle"))'} | ${validOutput}
`('$def | $input', ({ def, input, output }) => {
mockFs(fileContents);
const { vars } = parseGradle(
[def, input].join('\n'),
{},
Expand Down
11 changes: 6 additions & 5 deletions lib/util/cache/package/decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ describe('util/cache/package/decorator', () => {
const setCache = jest.spyOn(packageCache, 'set');

let count = 1;
const getValue = jest.fn(() => {
const res = String(100 * count + 10 * count + count);
count += 1;
return Promise.resolve(res);
});
const getValue = jest.fn();

beforeEach(async () => {
memCache.init();
await packageCache.init({ cacheDir: os.tmpdir() });
count = 1;
getValue.mockImplementation(() => {
const res = String(100 * count + 10 * count + count);
count += 1;
return Promise.resolve(res);
});
});

it('should cache string', async () => {
Expand Down

0 comments on commit b23700d

Please sign in to comment.