Home > @rushstack/webpack-plugin-utilities > Testing > getTestingWebpackCompilerAsync
Testing.getTestingWebpackCompilerAsync() function
This function generates a webpack compiler with default configuration and the output filesystem mapped to a memory filesystem. This is useful for testing webpack plugins/loaders where we do not need to write to disk (which can be costly).
Signature:
export declare function getTestingWebpackCompilerAsync(entry: string, additionalConfig?: Configuration, memFs?: IFs): Promise<(Stats | MultiStats) | undefined>;
Parameters
Parameter | Type | Description |
|---|---|---|
entry | string | The entry point for the webpack compiler |
additionalConfig | Configuration | (Optional) Any additional configuration that should be merged with the default configuration |
memFs | IFs | (Optional) The memory filesystem to use for the output filesystem. Use this option if you want to _inspect_, analyze, or read the output files generated by the webpack compiler. If you do not need to do this, you can omit this parameter and the output files. |
Returns:
Promise<(Stats | MultiStats) | undefined>
- A webpack compiler with the output filesystem mapped to a memory filesystem
Remarks
If you want to be able to read, analyze, access the files written to the memory filesystem, you can pass in a memory filesystem instance to the memFs parameter.
Example 1
import Testing from '@rushstack/webpack-plugin-utilities';
describe('MyPlugin', () => {
it('should run', async () => {
const stats = await Testing.getTestingWebpackCompiler(
`./src/index.ts`,
);
expect(stats).toBeDefined();
});
});
Example 2
import Testing from '@rushstack/webpack-plugin-utilities';
import { createFsFromVolume, Volume, IFs } from 'memfs';
import path from 'path';
describe('MyPlugin', () => {
it('should run', async () => {
const virtualFileSystem: IFs = createFsFromVolume(new Volume());
const stats = await Testing.getTestingWebpackCompiler(
`./src/index.ts`,
{},
virtualFileSystem
);
expect(stats).toBeDefined();
expect(virtualFileSystem.existsSync(path.join(__dirname, 'dist', 'index.js'))).toBe(true);
});
});