[Webpack] File Loaders cause mocha to throw Syntax Error
See original GitHub issueI went to integrate this test setup into my project which is built on Webpack. As such my .js
files sometimes include files loaded with webpack loaders; for example .png
or .scss
. Mocha throws a SyntaxError
on encountering these, so you need a custom compiler.
My custom compiler src/tests/compiler.js
:
'use strict'
var babel = require('babel-core/register');
function noop() {
return null;
}
require.extensions['.scss'] = noop;
require.extensions['.css'] = noop;
And new npm test
command:
mocha --compilers js:./src/tests/compiler.js ./src/tests/*.spec.js
Issue Analytics
- State:
- Created 8 years ago
- Reactions:7
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Mocha testing failed due to css in webpack - Stack Overflow
The problem is the .less loader that I was using in my webpack.config.test.js file. Simply swapping less-loader for null-loader fixed my problem ...
Read more >mocha-loader | webpack - JS.ORG
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >Testing Node.js with Mocha and Chai - LogRocket Blog
Also notice that the sum() call is wrapped in a function to test that an error is thrown when non-number arguments are passed....
Read more >How I solved and debugged my Webpack issue through trial ...
js ) and one of the source files contains an error, the stack trace will simply point to bundle.js . This isn't always...
Read more >Running Mocha Tests as Native ES6 Modules in a Browser
This is exactly what is needed. Adding script-loader to the webpack config fixed the error. chai.js is loaded and available globally. The final ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Update: There is now a package called ignore-styles which seems to accomplish the same goals and also may help with testability.
+1