Skip to content

Commit

Permalink
Change _jestTestFilePath global to method on jest object.
Browse files Browse the repository at this point in the history
- move injected global to method on jest
- add test
- update documentation
  • Loading branch information
Eric O'Connell committed May 20, 2015
1 parent 6ad1f0a commit 9e92c45
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
6 changes: 4 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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).

Expand Down Expand Up @@ -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__'`)

Expand Down
4 changes: 4 additions & 0 deletions src/HasteModuleLoader/HasteModuleLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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/);
});
});
});
2 changes: 1 addition & 1 deletion src/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9e92c45

Please sign in to comment.