Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Add support for generic file extension __mocks__ #21

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions __tests__/JSMockLoader-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ describe('JSMockLoader', function() {
expect(loader.matchPath('a/1.css')).toBe(false);
});

it('should match mocks with the specified file extensions', function() {
var loader = new JSMockLoader({ extensions: ['.coffee', '.js'] });
expect(loader.matchPath('__mocks__/x.js')).toBe(true);
expect(loader.matchPath('__mocks__/x.jsx')).toBe(false);
expect(loader.matchPath('__mocks__/x.coffee')).toBe(true);
expect(loader.matchPath('__mocks__/x.md')).toBe(false);
expect(loader.matchPath('__mocks__/x.litcoffee')).toBe(false);
});

it('should match mocks with the default file extension', function() {
var loader = new JSMockLoader();
expect(loader.matchPath('__mocks__/x.js')).toBe(true);
expect(loader.matchPath('__mocks__/x.jsx')).toBe(false);
expect(loader.matchPath('__mocks__/x.coffee')).toBe(false);
expect(loader.matchPath('__mocks__/x.md')).toBe(false);
expect(loader.matchPath('__mocks__/x.litcoffee')).toBe(false);
});

var testData = path.join(__dirname, '..', '__test_data__', 'JSMock');

it('should extract dependencies', function() {
Expand Down
13 changes: 10 additions & 3 deletions lib/loader/JSMockLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ var JSMock = require('../resource/JSMock');
function JSMockLoader(options) {
ResourceLoader.call(this, options);

var extensions;
if (options && options.extensions) {
extensions = options.extensions;
} else {
extensions = ['.js'];
}

this.pathRe = this.options.matchSubDirs ?
/(?:[\\/]|^)__mocks__[\\/](.+)\.js$/ :
/(?:[\\/]|^)__mocks__[\\/]([^\/]+)\.js$/;
new RegExp("(?:[\\\\/]|^)__mocks__[\\\\/](.+)(?:\\" + extensions.join("|\\") + ")$") :
new RegExp("(?:[\\\\/]|^)__mocks__[\\\\/]([^\\/]+)(?:\\" + extensions.join("|\\") + ")$");
}
inherits(JSMockLoader, ResourceLoader);
JSMockLoader.prototype.path = __filename;
Expand All @@ -39,7 +46,7 @@ JSMockLoader.prototype.getResourceTypes = function() {
};

JSMockLoader.prototype.getExtensions = function() {
return ['.js'];
return this.options.extensions || ['.js'];
};


Expand Down