diff --git a/docs/API.md b/docs/API.md index 322ac34371e2..78162172cd22 100644 --- a/docs/API.md +++ b/docs/API.md @@ -11,6 +11,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) @@ -105,6 +106,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). @@ -319,8 +323,6 @@ The path to a module that runs some code to configure or set up the testing fram For example, Jest ships with several plug-ins to `jasmine` that work by monkey-patching the jasmine API. If you wanted to add even more jasmine plugins to the mix (or if you wanted some custom, project-wide matchers for example), you could do so in this module. -The currently executing test's file path is available as an injected global in this file as `_jestTestFilePath`. - ### `config.testDirectoryName` [string] (default: `'__tests__'`) diff --git a/src/HasteModuleLoader/HasteModuleLoader.js b/src/HasteModuleLoader/HasteModuleLoader.js index cfe8c3030829..1f1325c5ce90 100644 --- a/src/HasteModuleLoader/HasteModuleLoader.js +++ b/src/HasteModuleLoader/HasteModuleLoader.js @@ -966,6 +966,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; diff --git a/src/HasteModuleLoader/__tests__/HasteModuleLoader-currentTestPath-test.js b/src/HasteModuleLoader/__tests__/HasteModuleLoader-currentTestPath-test.js new file mode 100644 index 000000000000..9e975bd80239 --- /dev/null +++ b/src/HasteModuleLoader/__tests__/HasteModuleLoader-currentTestPath-test.js @@ -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/); + }); + }); +}); \ No newline at end of file diff --git a/src/TestRunner.js b/src/TestRunner.js index ebd95aaa3ea3..d269d9cb2d21 100644 --- a/src/TestRunner.js +++ b/src/TestRunner.js @@ -329,7 +329,7 @@ TestRunner.prototype.runTest = function(testFilePath) { // Pass the testFilePath into the runner, so it can be used to e.g. // configure test reporter output. - env.global._jestTestFilePath = testFilePath; + 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