diff --git a/integration_tests/__tests__/clear_cache.test.js b/integration_tests/__tests__/clear_cache.test.js new file mode 100644 index 000000000000..a0954fd8edd1 --- /dev/null +++ b/integration_tests/__tests__/clear_cache.test.js @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ +'use strict'; + +const fs = require('fs'); +const os = require('os'); +const path = require('path'); +const runJest = require('../runJest'); + +const CACHE = path.resolve(os.tmpdir(), 'clear_cache_directory'); + +describe('jest --clearCache', () => { + test('normal run results in cache directory being written', () => { + const {status} = runJest('clear_cache', [`--cacheDirectory=${CACHE}`]); + + expect(fs.existsSync(CACHE)).toBe(true); + expect(status).toBe(0); + }); + test('clearCache results in deleted directory and exit status 0', () => { + expect(fs.existsSync(CACHE)).toBe(true); + + const {status, stdout, stderr} = runJest('clear_cache', [ + '--clearCache', + `--cacheDirectory=${CACHE}`, + ]); + + expect(fs.existsSync(CACHE)).toBe(false); + expect(stdout).toBe(`Cleared ${CACHE}\n`); + expect(stderr.trim()).toBe(''); + expect(status).toBe(0); + }); +}); diff --git a/integration_tests/__tests__/pass_with_no_tests.test.js b/integration_tests/__tests__/pass_with_no_tests.test.js index 33b97195d1c8..02598f71e5c7 100644 --- a/integration_tests/__tests__/pass_with_no_tests.test.js +++ b/integration_tests/__tests__/pass_with_no_tests.test.js @@ -1,9 +1,8 @@ /** * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. * * @flow */ diff --git a/integration_tests/clear_cache/__tests__/clear_cache.test.js b/integration_tests/clear_cache/__tests__/clear_cache.test.js new file mode 100644 index 000000000000..61e6e0498f8d --- /dev/null +++ b/integration_tests/clear_cache/__tests__/clear_cache.test.js @@ -0,0 +1,11 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + */ +'use strict'; + +test('stub', () => expect(1).toBe(1)); diff --git a/integration_tests/clear_cache/package.json b/integration_tests/clear_cache/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/integration_tests/clear_cache/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/packages/jest-cli/package.json b/packages/jest-cli/package.json index 6ba1066c5fab..01af8aeb9802 100644 --- a/packages/jest-cli/package.json +++ b/packages/jest-cli/package.json @@ -27,6 +27,7 @@ "micromatch": "^2.3.11", "node-notifier": "^5.1.2", "pify": "^3.0.0", + "rimraf": "^2.5.4", "slash": "^1.0.0", "string-length": "^2.0.0", "strip-ansi": "^4.0.0", diff --git a/packages/jest-cli/src/cli/args.js b/packages/jest-cli/src/cli/args.js index e60f377ce002..018b7cbd619d 100644 --- a/packages/jest-cli/src/cli/args.js +++ b/packages/jest-cli/src/cli/args.js @@ -117,6 +117,13 @@ export const options = { ' prevent snapshots from being written unless explicitly requested.', type: 'boolean', }, + clearCache: { + default: undefined, + description: + 'Clears the configured Jest cache directory and then exits. ' + + 'Default directory can be found by calling jest --showConfig', + type: 'boolean', + }, clearMocks: { default: undefined, description: diff --git a/packages/jest-cli/src/cli/index.js b/packages/jest-cli/src/cli/index.js index 30bddce8ef9a..752fbeb8cf04 100644 --- a/packages/jest-cli/src/cli/index.js +++ b/packages/jest-cli/src/cli/index.js @@ -32,6 +32,7 @@ import Runtime from 'jest-runtime'; import TestWatcher from '../test_watcher'; import watch from '../watch'; import yargs from 'yargs'; +import rimraf from 'rimraf'; export async function run(maybeArgv?: Argv, project?: Path) { const argv: Argv = buildArgv(maybeArgv, project); @@ -68,6 +69,15 @@ export const runCLI = async ( outputStream, ); + if (argv.clearCache) { + configs.forEach(config => { + rimraf.sync(config.cacheDirectory); + process.stdout.write(`Cleared ${config.cacheDirectory}\n`); + }); + + process.exit(0); + } + await _run( globalConfig, configs,