https://github.com/privatenumber/webpack-test-utils
Utility functions to test Webpack loaders/plugins
https://github.com/privatenumber/webpack-test-utils
loader plugin test util utils webpack
Last synced: 4 months ago
JSON representation
Utility functions to test Webpack loaders/plugins
- Host: GitHub
- URL: https://github.com/privatenumber/webpack-test-utils
- Owner: privatenumber
- License: mit
- Created: 2021-10-30T19:20:58.000Z (almost 4 years ago)
- Default Branch: develop
- Last Pushed: 2022-12-15T07:58:06.000Z (almost 3 years ago)
- Last Synced: 2025-03-18T16:04:15.085Z (7 months ago)
- Topics: loader, plugin, test, util, utils, webpack
- Language: TypeScript
- Homepage:
- Size: 556 KB
- Stars: 9
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Utility functions to test Webpack loaders/plugins
### Features
- **In-memory builds** Uses an in-memory file-system for faster builds that don't pollute your disk
- **TypeScript support** Get type hints and develop with confidence
- **Sensible defaults** Builds in production-mode by default with minification disabled
- **Webpack 4 & 5 support** Both versions are supported## 🚀 Install
```bash
npm i -D webpack-test-utils
```## 👨🏫 Usage
### Build
```js
import { build } from 'webpack-test-utils'test('build', async () => {
// Create in-memory file-system
const volume = {
'/src/index.js': 'export default "12345"'
}// Run Webpack build
const built = await build(volume)// Verify successful build
expect(built.stats.hasErrors()).toBe(false)
expect(built.stats.hasWarnings()).toBe(false)// Run the code to verify result
expect(built.require('/dist/index.js')).toBe('12345')
})
```### Watch
```js
import { watch } from 'webpack-test-utils'test('watch', async () => {
// Create in-memory file-system
const volume = {
'/src/index.js': 'export default "12345"'
}// Create Webpack watcher
const watching = watch(volume)// Create build
let stats = await watching.build()// Verify result
expect(stats.hasWarnings()).toBe(false)
expect(watching.require('/dist')).toBe('12345')// Update source code
watching.fs.writeFileSync('/src/index.js', 'export default "54321"')// Rebuild
stats = await watching.build()expect(stats.hasWarnings()).toBe(false)
// Delete cache and re-require to validate changed result
delete watching.require.cache[watching.require.resolve('/dist')]
expect(watching.require('/dist')).toBe('54321')// Close watcher
await watching.close()
})
```## ⚙️ API
### build(volume, configHook)
Returns: `Promise`
Run a single Webpack build.
#### volumeType: `{ [filePath: string]: string }`
Required
An object where the key is the absolute path, and the value is the content of the path.
#### configurationHook
Type: `(config: WebpackConfiguration) => void`
A function that receives the Webpack configuration object for configuration before running the build.
### watch(volume, configHook)
Returns: `Promise`
Run a single Webpack build.
#### volumeType: `{ [filePath: string]: string }`
Required
An object where the key is the absolute path, and the value is the content of the path.
#### configurationHook
Type: `(config: WebpackConfiguration) => void`
A function that receives the Webpack configuration object for configuration before running the build.
### Default Webpack configuration
See [`src/utils/get-default-webpack-config.ts`](/src/utils/get-default-webpack-config.ts).