-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add clearCache cli command #4430
Changes from 10 commits
3036f7e
7615c79
04b495d
d63c815
0a7a2c2
4595517
40efb06
a4b9ea6
613feca
82d01b3
2cc04eb
cf503fb
072428c
4680586
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a false positive. This folder doesn't exist, because Jest didn't run any tests. We need to populate this cache earlier with a sample run, then run it once more. Or just create the dir and make sure it is present prior to running test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I thought the folder was created when calling runJest regardless of whether there are tests or not. Will add a stub test, verify the cache dir has contents, then runJest again with clearCache There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add expect(fs.existsSync(CACHE)).toBe(true); above the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the second test correct? Makes sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct! |
||
expect(stdout).toBe(`Cleared ${CACHE}\n`); | ||
expect(stderr.trim()).toBe(''); | ||
expect(status).toBe(0); | ||
}); | ||
}); |
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)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space before "Default"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. |
||
type: 'boolean', | ||
}, | ||
clearMocks: { | ||
default: undefined, | ||
description: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be able to clear the cache for certain project or all projects, right now it's only for the first one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't sure how this worked: should I just map over the configs array? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yup, should be fine |
||
|
||
await _run( | ||
globalConfig, | ||
configs, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change the license header to be MIT, please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also found
jest/integration_tests/__tests__/pass_with_no_tests.test.js
with BSD license. Should I take care of that here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please