Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelmwu committed Jul 8, 2019
1 parent a501718 commit 809c076
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- `[docs]` Add information about using `jest.doMock` with ES6 imports ([#8573](https://github.com/facebook/jest/pull/8573))
- `[docs]` Fix variable name in custom-matcher-api code example ([#8582](https://github.com/facebook/jest/pull/8582))
- `[docs]` Fix example used in custom environment docs ([#8617](https://github.com/facebook/jest/pull/8617))
- `[docs]` Fix example reference implementation to use Jest with Phabricator

### Performance

Expand Down
62 changes: 40 additions & 22 deletions packages/jest-phabricator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ You need to add the jest unit engine to your .arcconfig:
...
```

Or use the `ArcanistConfigurationDrivenUnitTestEngine` and add an entry to your .arcunit

```json
"jest": {
"type": "jest",
"include": "(\\.jsx?$)"
}
```

In `JestUnitTestEngine` there are a couple of constants you probably need to modify:

- `PROCESSOR` points to the path or the processor
Expand All @@ -26,26 +35,30 @@ If you need to pass to Jest a custom configuration you can either use `JEST_PATH
## Reference implementation

```php
class JestUnitTestEngine extends ArcanistBaseUnitTestEngine {
final class JestUnitTestEngine extends ArcanistUnitTestEngine {
const PROCESSOR = 'jest/packages/jest-phabricator/build/index.js';
const JEST_PATH = 'jest/packages/jest/bin/jest.js';
const TOO_MANY_FILES_TO_COVER = 100;
const GIGANTIC_DIFF_THRESHOLD = 200;

public function getEngineConfigurationName() {
return 'jest';
}

private function getRoot() {
return $this->getWorkingCopy()->getProjectRoot();
}

private function getOutputJSON() {
return $this->getRoot() . '/output.json';
}

private function getFutureResults($future) {
list($stdout, $stderr) = $future->resolvex();
$output_JSON = $this->getOutputJSON();
$report_path_exists = file_exists($output_JSON);
$raw_results = null;

if ($report_path_exists) {
$raw_results = json_decode(
Filesystem::readFile($output_JSON),
Expand All @@ -55,11 +68,11 @@ class JestUnitTestEngine extends ArcanistBaseUnitTestEngine {
} else {
$raw_results = json_decode($stdout, true);
}

if (!is_array($raw_results)) {
throw new Exception("Unit test script emitted invalid JSON: {$stdout}");
}

$results = array();
foreach ($raw_results as $result) {
$test_result = new ArcanistUnitTestResult();
Expand All @@ -70,7 +83,7 @@ class JestUnitTestEngine extends ArcanistBaseUnitTestEngine {
ArcanistUnitTestResult::RESULT_PASS :
ArcanistUnitTestResult::RESULT_FAIL
);

if (isset($result['coverage'])) {
$coverage = array();
$root = $this->getRoot() . '/';
Expand All @@ -85,10 +98,10 @@ class JestUnitTestEngine extends ArcanistBaseUnitTestEngine {
$test_result->setUserData($result['message']);
$results[] = $test_result;
}

return $results;
}

private function runCommands($commands) {
$futures = array();
foreach ($commands as $command) {
Expand All @@ -97,9 +110,9 @@ class JestUnitTestEngine extends ArcanistBaseUnitTestEngine {
$paths = $command['paths'];
$futures[] = new ExecFuture("{$bin} {$options} %Ls", $paths);
}

$console = PhutilConsole::getConsole();

// Pass stderr through so we can give the user updates on test
// status as tests run.
$completed = array();
Expand Down Expand Up @@ -128,20 +141,23 @@ class JestUnitTestEngine extends ArcanistBaseUnitTestEngine {
foreach ($futures as $future) {
$results[] = $this->getFutureResults($future);
}


if (empty($results)) {
return array();
}
return call_user_func_array('array_merge', $results);
}

private function runJSTests() {
$console = PhutilConsole::getConsole();
$root = $this->getRoot();

$result_arrays = [];
$paths = $this->getPaths();
$jest_paths = array();
foreach ($paths as $path) {
$ext = idx(pathinfo($path), 'extension');
if ($ext === 'js' || $ext === 'json') {
if ($ext === 'js' || $ext === 'json' || $ext === 'jsx') {
// Filter deleted modules because Jest can't do anything with them.
if (file_exists("$root/$path")) {
$jest_paths[] = "$root/$path";
Expand All @@ -162,7 +178,7 @@ class JestUnitTestEngine extends ArcanistBaseUnitTestEngine {
'paths' => $jest_paths,
);
}

try {
$result_arrays[] = $this->runCommands($commands);
} catch (Exception $e) {
Expand All @@ -175,32 +191,34 @@ class JestUnitTestEngine extends ArcanistBaseUnitTestEngine {
$result_arrays[] = array($result);
}
}

$console->writeOut("Finished tests.\n");
// $console->writeErr(implode(', ', $result_arrays));
return call_user_func_array('array_merge', $result_arrays);
}

private function getJestOptions($paths) {
$output_JSON = $this->getOutputJSON();
$options = array(
'--colors',
'--findRelatedTests',
'--json',
'--passWithNoTests true',
'--outputFile=' . $output_JSON,
'--testResultsProcessor=' . self::PROCESSOR
);

// Checks for the number of files to cover, in case it's too big skips coverage
// A better solution would involve knowing what's the machine buffer size limit
// for exec and check if the command can stay within it.
if (count($paths) < self::TOO_MANY_FILES_TO_COVER) {
$options[] = '--findRelatedTests ' . join(' ', $paths);
$options[] = '--coverage';
$options[] = '--collectCoverageOnlyFrom '. join(' ', $paths);
}

return $options;
}

/** @Override */
public function run() {
return self::runJSTests();
Expand Down

0 comments on commit 809c076

Please sign in to comment.