![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
webgl-detector
Advanced tools
Test & mock friendly WebGL detection utility.
Install
npm install --save webgl-detector
Usage
import { isWebGLSupported, isWebGL2Supported } from 'webgl-detector';
if (isWebGLSupported()){
// WebGL is supported!
}
// OR for WebGL2
if (isWebGL2Supported()){
// WebGL2 is supported!
}
Mocking - Jest & React
Create a file at __mocks__/webgl-detector.js
. Make sure __mocks__
directory is adjacent to node_modules
.
// __mocks__/webgl-detector.js
const webglDetector = jest.genMockFromModule('webgl-detector');
webglDetector.setValue = value => {
webglDetector.isSupported = value;
};
webglDetector.isWebGLSupported = () => webglDetector.isSupported;
module.exports = webglDetector;
Suppose we have an Container component named AnimationContainer
and a renderer container named MyAnimation
. If WebGL is supported, AnimationContainer
returns MyAnimation
component:
import React from 'react';
import { isWebGLSupported } from 'webgl-detector';
import MyAnimation from './MyAnimation';
const AnimationContainer = props => (
<div>
{isWebGLSupported() ? (
<MyAnimation {...props} />
) : (
<div>WebGL is not supported</div>
)}
</div>
);
export default AnimationContainer;
In our test file:
describe('<AnimationContainer />', () => {
// require mocked module with usual value
beforeEach(() => {
require('webgl-detector').setValue(true);
});
it('should render something if WebGL is supported', () => {
expect(something).toBe(true); // pseudo expect
});
it('should not render something if WebGL is not supported', () => {
// set module mock value for WebGL disabled case
require('webgl-detector').setValue(false);
expect(something).toBe(false); // pseudo expect
});
});
Learn more on mocking Node modules in Jest documentation
MIT © Cengiz Can
FAQs
Test & mock friendly WebGL detection utility.
The npm package webgl-detector receives a total of 288 weekly downloads. As such, webgl-detector popularity was classified as not popular.
We found that webgl-detector demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.