mock-mediawiki
Honest MediaWiki JS interface mocking in Node.js.
No unnecessary changes to the original source files. Directly copied from MediaWiki core with bare minimum modifications.
Licensed under the Lesser General Public License. Can be used from any repository regardless of license. (Note that on the other hand, MW source code copied directly may only be pasted into repos with GPL-compatible licenses).
How to use
Use with Jest
If you're using Jest's default test environment jest-environment-jsdom
, just tweak your jest.config.js
to include:
"setupFilesAfterEnv": ["mock-mediawiki"]
Done! All your Jest tests will now have access to mw
and $
as globals. This setup works with both CommonJS module format and ESM.
Jest exposes globally most browser-only globals available via jsdom. So if your gadget code includes references to HTMLSpanElement
or XMLDocument
et al, they'll just work!
Use with other test runners
Other test runners don't usually have jsdom integrated. You need to install jsdom separately (npm i -D jsdom
) and then include the following in your test files:
CommonJS:
require('mock-mediawiki/with-jsdom');
ESM:
import 'mock-mediawiki/with-jsdom';
It is assumed that ESM tests undergo transformation to CommonJS as part of some build step. Use of this package with native Node.js ESM packages is not supported because of its internal reliance on require()
.
If your tests are in TypeScript, you'll need to additionally have types-mediawiki. However, note that types-mediawiki covers type definitions for more modules, so TypeScript-based IntelliSense can be misleading.
TODO
[ PRs Welcome ]