Skip to content

Commit

Permalink
rebase and squash - add clearCache cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
tabrindle committed Oct 11, 2017
1 parent 1ece140 commit 6f51489
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 0 deletions.
37 changes: 37 additions & 0 deletions integration_tests/__tests__/clear_cache.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* 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.
*
* @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', () => {
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);
});
});
10 changes: 10 additions & 0 deletions integration_tests/clear_cache/__tests__/clear_cache.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* 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.
*/
'use strict';

test('stub', () => expect(1).toBe(1));
5 changes: 5 additions & 0 deletions integration_tests/clear_cache/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
1 change: 1 addition & 0 deletions packages/jest-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions packages/jest-cli/src/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 6f51489

Please sign in to comment.