Skip to content

Commit

Permalink
Merge pull request #359 from drd/master
Browse files Browse the repository at this point in the history
Make currently-executing test's file path available as jest.currentTestPath()
  • Loading branch information
amasad committed May 20, 2015
2 parents 2ea7402 + 9e92c45 commit 69229fb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ permalink: docs/api.html
- [`jest.autoMockOff()`](#jest-automockoff)
- [`jest.autoMockOn()`](#jest-automockon)
- [`jest.clearAllTimers()`](#jest-clearalltimers)
- [`jest.currentTestPath()`](#jest-currenttestpath)
- [`jest.dontMock(moduleName)`](#jest-dontmock-modulename)
- [`jest.genMockFromModule(moduleName)`](#jest-genmockfrommodule-modulename)
- [`jest.genMockFunction()`](#jest-genmockfunction)
Expand Down Expand Up @@ -107,6 +108,9 @@ Removes any pending timers from the timer system.

This means, if any timers have been scheduled (but have not yet executed), they will be cleared and will never have the opportunity to execute in the future.

### `jest.currentTestPath()`
Returns the absolute path to the currently executing test file.

### `jest.dontMock(moduleName)`
Indicates that the module system should never return a mocked version of the specified module from `require()` (e.g. that it should always return the real module).

Expand Down
4 changes: 4 additions & 0 deletions src/HasteModuleLoader/HasteModuleLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,10 @@ Loader.prototype.resetModuleRegistry = function() {
this._environment.fakeTimers.clearAllTimers();
}.bind(this),

currentTestPath: function() {
return this._environment.testFilePath;
}.bind(this),

dontMock: function(moduleName) {
var moduleID = this._getNormalizedModuleID(currPath, moduleName);
this._explicitShouldMock[moduleID] = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright (c) 2014, 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';

jest.autoMockOff();

describe('nodeHasteModuleLoader', function() {
describe('currentTestPath', function() {
it('makes the current test path available', function() {
expect(jest.currentTestPath()).toMatch(/currentTestPath-test/);
});
});
});
4 changes: 4 additions & 0 deletions src/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ TestRunner.prototype.runTest = function(testFilePath) {
var consoleMessages = [];
env.global.console = new Console(consoleMessages);

// Pass the testFilePath into the runner, so it can be used to e.g.
// configure test reporter output.
env.testFilePath = testFilePath;

return this._constructModuleLoader(env, config).then(function(moduleLoader) {
// This is a kind of janky way to ensure that we only collect coverage
// information on modules that are immediate dependencies of the test file.
Expand Down

0 comments on commit 69229fb

Please sign in to comment.