Skip to content

Commit

Permalink
Prevent a ENOENT crash by checking for file existence in jest-message…
Browse files Browse the repository at this point in the history
…-util (#5405)

* Prevent a ENOENT crash by checking for existence

Jest tries to load the stack-trace based on the top frame. The problem is, the filename may be provided by the source-map, which is unreliable since it can point to a non-existent file. In such a case, the code would cause a hard crash of "Test suite failed to run" with a stack-trace pointing at "exports.formatStackTrace", which I have corrected here.

* add an integration test for bad-source-map case

* lint CHANGELOG

* use a try-catch rather than fs.existsSync
  • Loading branch information
niieani authored and cpojer committed Jan 28, 2018
1 parent 0e55c7c commit 2f0dec5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 15 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
## master

### features
### Features

* `[jest-mock]` Add util methods to create async functions.
([#5318](https://github.com/facebook/jest/pull/5318))

### Fixes

* `[jest-message-util]` Prevent an `ENOENT` crash when the test file contained a
malformed source-map. ([#5405](https://github.com/facebook/jest/pull/5405)).
* `[jest]` Add `import-local` to `jest` package.
([#5353](https://github.com/facebook/jest/pull/5353))
* `[expect]` Support class instances in `.toHaveProperty()` matcher.
Expand Down
16 changes: 16 additions & 0 deletions integration-tests/__tests__/bad_source_map.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* 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 runJest = require('../runJest');

test('suite with test cases that contain malformed sourcemaps', () => {
const result = runJest('bad-source-map');
expect(result.stderr).not.toMatch('ENOENT');
});
15 changes: 15 additions & 0 deletions integration-tests/bad-source-map/__tests__/bad-source-map.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions integration-tests/bad-source-map/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
39 changes: 25 additions & 14 deletions packages/jest-message-util/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ const trim = string => (string || '').replace(/^\s+/, '').replace(/\s+$/, '');
const trimPaths = string =>
string.match(STACK_PATH_REGEXP) ? trim(string) : string;

const getRenderedCallsite = (fileContent: string, line: number) => {
let renderedCallsite = codeFrameColumns(
fileContent,
{start: {line}},
{highlightCode: true},
);

renderedCallsite = renderedCallsite
.split('\n')
.map(line => MESSAGE_INDENT + line)
.join('\n');

renderedCallsite = `\n${renderedCallsite}\n`;
return renderedCallsite;
};

// ExecError is an error thrown outside of the test suite (not inside an `it` or
// `before/after each` hooks). If it's thrown, none of the tests in the file
// are executed.
Expand Down Expand Up @@ -228,20 +244,15 @@ export const formatStackTrace = (
const filename = topFrame.file;

if (path.isAbsolute(filename)) {
renderedCallsite = codeFrameColumns(
fs.readFileSync(filename, 'utf8'),
{
start: {line: topFrame.line},
},
{highlightCode: true},
);

renderedCallsite = renderedCallsite
.split('\n')
.map(line => MESSAGE_INDENT + line)
.join('\n');

renderedCallsite = `\n${renderedCallsite}\n`;
let fileContent;
try {
// TODO: check & read HasteFS instead of reading the filesystem:
// see: https://github.com/facebook/jest/pull/5405#discussion_r164281696
fileContent = fs.readFileSync(filename, 'utf8');
renderedCallsite = getRenderedCallsite(fileContent, topFrame.line);
} catch (e) {
// the file does not exist or is inaccessible, we ignore
}
}
}

Expand Down

0 comments on commit 2f0dec5

Please sign in to comment.