From 23c584c0df460b28052f661f8edb0d1ab31950ec Mon Sep 17 00:00:00 2001 From: Johann Date: Fri, 6 Feb 2015 01:46:33 +0100 Subject: [PATCH 1/2] Add support for Coffeescript __mocks__ --- __tests__/JSMockLoader-test.js | 18 ++++++++++++++++++ lib/loader/JSMockLoader.js | 13 ++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/__tests__/JSMockLoader-test.js b/__tests__/JSMockLoader-test.js index c909644..340ae13 100644 --- a/__tests__/JSMockLoader-test.js +++ b/__tests__/JSMockLoader-test.js @@ -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() { diff --git a/lib/loader/JSMockLoader.js b/lib/loader/JSMockLoader.js index 6197efa..c8e781c 100644 --- a/lib/loader/JSMockLoader.js +++ b/lib/loader/JSMockLoader.js @@ -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; @@ -39,7 +46,7 @@ JSMockLoader.prototype.getResourceTypes = function() { }; JSMockLoader.prototype.getExtensions = function() { - return ['.js']; + return this.options.extensions || ['.js']; }; From 2d586615c1a3161201e063997862f7acc8428b4e Mon Sep 17 00:00:00 2001 From: Johann Date: Sun, 22 Mar 2015 23:33:45 +0100 Subject: [PATCH 2/2] Improve code style --- lib/loader/JSMockLoader.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/loader/JSMockLoader.js b/lib/loader/JSMockLoader.js index c8e781c..f6e1d63 100644 --- a/lib/loader/JSMockLoader.js +++ b/lib/loader/JSMockLoader.js @@ -28,9 +28,9 @@ function JSMockLoader(options) { ResourceLoader.call(this, options); var extensions; - if(options && options.extensions){ + if (options && options.extensions) { extensions = options.extensions; - }else{ + } else { extensions = ['.js']; }