Web Audio API Test Utilities
web-audio-test-api is a JavaScript library designed for testing applications that use the Web Audio API, primarily in CI environments. It provides a mock implementation of the Web Audio API interfaces, which, when imported, replaces the native browser or Node.js-emulated Web Audio API globally. Key features include stricter type checking for audio parameters compared to browser implementations, the ability to serialize an audio graph to a JSON object for assertion and debugging, and tracking of OscillatorNode/BufferSourceNode states. The current stable version is 0.5.2. However, the package appears to be unmaintained, with the last publish occurring over eight years ago, suggesting a very slow or effectively abandoned release cadence. Its primary differentiator is its focus on testability and debuggability through explicit state tracking and serialization, rather than mimicking real-time audio processing.
Common errors
-
ReferenceError: AudioContext is not defined
cause The side-effect import `import 'web-audio-test-api';` was not executed or not executed early enough to expose `AudioContext` globally.fixEnsure `import 'web-audio-test-api';` is the first statement in your test file or in a setup file that runs before any code attempts to instantiate `AudioContext`. -
TypeError: OscillatorNode#frequency is readonly
cause Attempting to assign a value directly to a `AudioParam` property (like `frequency` or `detune`) instead of its `value` property.fixAlways assign values to the `.value` property of `AudioParam` interfaces. Correct: `osc.frequency.value = 880;` Incorrect: `osc.frequency = 880;` -
TypeError: OscillatorNode#type should be an enum { sine, square, sawtooth, triangle }, but got: [some_value]cause Attempting to assign a numeric value or an invalid string to the `type` property of an `OscillatorNode` or similar enum-restricted property.fixEnsure that properties expecting an enum value (like `OscillatorNode.type`) are assigned one of the allowed string values. Correct: `osc.type = 'sawtooth';` Incorrect: `osc.type = 2;`
Warnings
- breaking This package globally replaces the native Web Audio API interfaces (e.g., AudioContext, OscillatorNode) upon import. This can cause conflicts or unexpected behavior if other libraries expect the native API or if you try to use both the mock and real API simultaneously. Ensure it's only imported in test environments where this behavior is desired.
- gotcha web-audio-test-api enforces stricter type checking and validation rules than most browser implementations of the Web Audio API. While this is a feature designed to catch common errors, it means code that might run without error in a browser (due to type coercion or lenient checks) could throw TypeErrors when tested with this library.
- deprecated The `web-audio-test-api` package appears to be unmaintained. The last publish to npm and code commit activity occurred over eight years ago (as of 2024). This means it may not be compatible with newer Node.js versions, updated Web Audio API specifications, or modern JavaScript features. Security vulnerabilities are also unlikely to be patched.
Install
-
npm install web-audio-test-api -
yarn add web-audio-test-api -
pnpm add web-audio-test-api
Imports
- Side Effect Global Install
const WebAudioTestAPI = require('web-audio-test-api');import 'web-audio-test-api';
- WebAudioTestAPI
import 'web-audio-test-api'; // Then, WebAudioTestAPI is available globally WebAudioTestAPI.unuse();
- AudioContext
import { AudioContext } from 'web-audio-test-api';import 'web-audio-test-api'; const audioContext = new AudioContext();
Quickstart
import 'web-audio-test-api';
// Create a test AudioContext instance
const audioContext = new AudioContext();
// Create some nodes and connect them
const osc = audioContext.createOscillator();
const lfo = audioContext.createOscillator();
const amp = audioContext.createGain();
// Assign an ID for easier debugging in the JSON output
lfo.$id = 'LFO';
// Configure node properties
osc.type = 'sawtooth';
osc.frequency.value = 880;
lfo.frequency.value = 2;
// Connect the audio graph
lfo.connect(amp.gain);
osc.connect(amp);
amp.connect(audioContext.destination);
// Output the audio graph as a JSON object for inspection
const graphJson = audioContext.toJSON();
// In a test environment, you would assert on this JSON structure
// For demonstration, we'll log it.
console.log(JSON.stringify(graphJson, null, 2));
/* Expected output (truncated):
{
"name": "AudioDestinationNode",
"inputs": [
{
"name": "GainNode",
"gain": {
"value": 1,
"inputs": [
{
"name": "OscillatorNode#LFO",
"type": "sine",
"frequency": {
"value": 2,
"inputs": []
},
"detune": {
"value": 0,
"inputs": []
},
"inputs": []
}
]
},
"inputs": [
{
"name": "OscillatorNode",
"type": "sawtooth",
"frequency": {
"value": 880,
"inputs": []
},
"detune": {
"value": 0,
"inputs": []
},
"inputs": []
}
]
}
]
}
*/