Reputation: 14835
I'm trying to include in with require.context
of Webpack all the files that should be covered by my Istanbul reporter.
I would like to include/require all the files under app
that have not the .test.js
extension.
// internals/testing/test-bundler.js
const context = require.context('../../app', true, /^.*(?!(\.test|internals.*))\.js$/);
context.keys().forEach(context);
My files structure is:
app/components/
app/containers/
app/decorators/
app/orm/
app/pages/
app/store/
app/tests/
app/utils/
app/app.js
app/reducers.js
app/routes.js
internals/testing/test-bundler.js
Obviously my regex doesn't work because inside the coverage report I see all the .test.js
files and even the internals/testing/test-bundler.js
file.
What am I doing wrong?
Upvotes: 12
Views: 10135
Reputation: 104
I have ensured a more useful regression for excluding files with the suffix *-spec.js
or files inside a test
folder
/^((?<!test\/).(?!-spec))+\.js$/.test('a.js'); // => true
/^((?<!test\/).(?!-spec))+\.js$/.test('a-spec.js'); // => false
/^((?<!test\/).(?!-spec))+\.js$/.test('lib/a.js'); // => true
/^((?<!test\/).(?!-spec))+\.js$/.test('lib/a-spec.js'); // => false
/^((?<!test\/).(?!-spec))+\.js$/.test('lib/test/a.js'); // => false
/^((?<!test\/).(?!-spec))+\.js$/.test('lib/test/a-spec.js'); // => false
/^((?<!test\/).(?!-spec))+\.js$/.test('test/lib/a.js'); // => false
/^((?<!test\/).(?!-spec))+\.js$/.test('test/lib/a-spec.js'); // => false
Upvotes: 0
Reputation: 67
Even if exclude node_modules
from require.context
, webpack
will still look into directories recursively, which is quit time consuming. I've encountered heap out of memory in a similar case.
I think it should be a better way not to install node_modules
for each module. But keep the node_modules
other place and specify the location in wepback
module.resolve
. Save source code and dependencies separated.
Upvotes: 0
Reputation: 29667
You need to be aware after what part the negative lookahead employs it's rejection. If you do it right after the first forward slash it works fine.
And with that you want to reject .*test
after the slash, instead of just test
directly behind it.
/^(?!internals).*\/(?!.*test).*\.js$/
Or more specific not allowing internals
in the path name.
Nor ending with test.js
:
^(?!.*(?:internals|test.js$)).*\.js$
Upvotes: 19
Reputation: 11358
What about simply filtering your paths with a simple use of filter
?
Fiddle:
var paths = [
'app/components/',
'app/containers/',
'app/decorators/',
'app/orm/',
'app/pages/',
'app/store/',
'app/tests/',
'app/utils/',
'app/app.js',
'app/reducers.js',
'app/routes.js',
'internals/testing/test-bundler.js',
'app/blablabla.test.js'
];
var result = paths.filter(function(e,i){
return (e.startsWith('app/') && !e.endsWith('.test.js'))
});
for(var i = 0; i < result.length; i++)
console.log(result[i]);
Upvotes: -1