-
-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow initializing plugin/loader tests #68
Comments
I could have sworn @michael-ciniawsky already did that. It started with something to the effect of ..
|
Ok, cool. So maybe it's more about making this more apparent. |
I try to get the 'experiments' I did together in a terse manner for discussion and open a PR with MVP capabilities. There will be still work required I guess :)
import path from 'path';
import webpack from 'webpack';
// import memoryFS from 'memory-fs';
export function compiler (fixture, options) {
const config = {
target: 'node',
devtool: 'sourcemap',
context: path.resolve(__dirname),
resolve: { extensions: ['.js'] },
entry: `./${fixture}`,
output: {
path: path.resolve(__dirname, 'expect'),
filename: `${path.basename(file)}.bundle.js`,
},
module: {
rules: [
{
test: options.test,
use: [
{
loader: path.resolve('dist', 'cjs.js'),
options: options.loader || {}
}
]
}
]
},
plugins: options.plugins || []
}
if (!options.type) options.type = 'loader'
return new Promise((resolve, reject) => {
return webpack(config, (err, stats) => {
if (err) reject(err);
const result = options.type === 'loader'
? {
err: stats.compilation.errors[0],
src: stats.compilation.modules[0]._source._value,
map: stats.compilation.modules[0]._source._sourceMap
}
: stats.compilation;
resolve(result);
});
});
} TODO
import fixture from './fixtures/[name].[ext]'
import { compiler } from './webpack'
import loader from '..src/'
test('Loader - Basic', () => {
expect(loader).toBeInstanceOf(Function)
// Loader Options etc.
const config = {
test: /\.ext$/,
loader: { options: {...} }
plugins: [] // if plugins are needed to be tested e.g with ETWP/UglifyJSPlugin
}
compiler(fixture, config)
.then((result) => {
expect(result.map).toContain(/\/* #sourceMappingURL=... *\//)
expect(result.map).toMatchSnapshot()
expect(result.src.toEqual(42)
expect(result.src).toMatchSnapshot()
})
.catch((err) => {
expect(err).toThrow(/message/)
expect(err).toThrowErrorMatchingSnapshot()
})
}) |
@TheLarkInn Setup a example loader & @michael-ciniawsky has already done quite a bit of work on the testing boilerplate in the past. Perhaps we can find a happy medium between the above & https://github.com/TheLarkInn/example-webpack-loader |
I have a |
To make it easier to develop plugins/loaders against webpack-defaults, there should be some way to generate initial tests as a starting point.
The text was updated successfully, but these errors were encountered: