diff --git a/packages/cli-tools/src/__tests__/cacheManager.test.ts b/packages/cli-tools/src/__tests__/cacheManager.test.ts index 29105e19d..30c1287ab 100644 --- a/packages/cli-tools/src/__tests__/cacheManager.test.ts +++ b/packages/cli-tools/src/__tests__/cacheManager.test.ts @@ -1,34 +1,37 @@ import fs from 'fs'; import path from 'path'; import cacheManager from '../cacheManager'; +import {cleanup, getTempDirectory} from '../../../../jest/helpers'; +const DIR = getTempDirectory('.react-native-cli/cache'); const projectName = 'Project1'; -const cachePath = '.react-native-cli/cache'; -const fullPath = path.join(cachePath, projectName); +const fullPath = path.join(DIR, projectName); describe('cacheManager', () => { beforeEach(() => { - jest.resetAllMocks(); + jest.restoreAllMocks(); }); - test('should remove cache if it exists', () => { - jest.spyOn(fs, 'existsSync').mockReturnValue(true); + afterEach(() => { + cleanup(DIR); + }); + + test('should not remove cache if it does not exist', () => { + jest.spyOn(fs, 'existsSync').mockReturnValue(false); jest.spyOn(fs, 'rmSync').mockImplementation(() => {}); - jest - .spyOn(path, 'resolve') - .mockReturnValue(path.join(cachePath, projectName)); cacheManager.removeProjectCache(projectName); - expect(fs.rmSync).toHaveBeenCalledWith(fullPath, {recursive: true}); + expect(fs.rmSync).not.toHaveBeenCalled(); }); - test('should not remove cache if it does not exist', () => { - jest.spyOn(fs, 'existsSync').mockReturnValue(false); + test('should remove cache if it exists', () => { + jest.spyOn(fs, 'existsSync').mockReturnValue(true); jest.spyOn(fs, 'rmSync').mockImplementation(() => {}); + jest.spyOn(path, 'resolve').mockReturnValue(fullPath); cacheManager.removeProjectCache(projectName); - expect(fs.rmSync).not.toHaveBeenCalled(); + expect(fs.rmSync).toHaveBeenCalledWith(fullPath, {recursive: true}); }); });