Page MenuHomePhabricator

Allow Jest to load ResourceLoader modules
Open, Needs TriagePublic

Description

MediaWiki Vue SFCs sometimes load ResourceLoader module package files with code like

const pageReady = require( 'mediawiki.page.ready' );

When testing such code using Jest, it would be nice if this magically worked instead of failing with an error. Jest optionally mocks external dependencies, but it can't do that unless it can load the external dependency. It needs the actual module so that it knows what shape to mock.

It's possible to work around the error by creating a file like __mocks__/mediawiki.page.ready.js, but this is a poor substitute for making Jest actually be aware of what file it is supposed to be loading.

A custom resolver would probably be able to do the job.

Server generated JSON config would still need to be mocked. I just want Jest to be able to load the main file of a named module. Once it has the main file, it should be able to also find what RL calls private package files, accessed using require('./file.js').

Event Timeline

Doing this automatically would be pretty neat, and a custom resolver indeed sounds like the way to accomplish that (as jest-resolve of course doesn't know about the ResourceLoader module registry).

A mock file works, but if you want the real thing you can do something like:

jest.mock( 'mediawiki.page.ready', () => require( '../../../path/to/entrypoint.js' ), { virtual: true } );

or:

jest.requireActual( '../../../resources/path/to/entrypoint.js' );

Do however bear in mind T390003: MW Core's Jest test suite is very slow. I had suspected this was partly due to us loading full unmocked modules (like jQuery), but perhaps that's not the problem after all. At any rate, whatever we do for this task should ideally not make matters worse.

I would gently push back at this. This sounds like it would encourage the use of Jest for integration tests rather than unit tests (which is one of the things I didn't like about our use of QUnit) and overall lead to a lot of slowdown.

We mock to keep tests focused on the functionality of the module we are testing and make sure they are defined in a way that they can be tested easily without external dependencies.

I think a resolver like this would also only work inside mediawiki core and I think it would be better to encourage the publishing of libraries to npm as needed.

This comment was removed by tstarling.

Resolvers here are the solution but I'm not sure what a global solution to this would look like. Jest configuration is local to an extension so presumably this would mean having to package up the entire frontend of MediaWiki into a Jest plugin - that would be pretty large and hard to maintain? It would likely need to be a npm module defined inside MediaWiki core a la tests/selenium/wdio-mediawiki

FWIW here is an example of Nearby using resolvers, to stub out OO and mediawiki.router with fake modules:
https://github.com/wikimedia/mediawiki-extensions-NearbyPages/blob/master/jest.config.js#L7

The extension has 100% test coverage. It doesn't need the actual functionality of those ResourceLoader modules to write tests.

I would gently push back at this. This sounds like it would encourage the use of Jest for integration tests…

having to package up the entire frontend of MediaWiki into a Jest plugin

I'm guessing you were replying to T390639#10706219. I was commenting knowing our team was working with a new RL module that had functionality we wanted to utilize in tests (which isn't the subject of this task – my apologies). I think we're all on the same page that we want things mocked in Jest.

It seems most use wikimedia/mw-node-qunit's mockMediaWiki.js (or in the case of Core, a clone of it). Everyone has the same problem and it's only going to grow as Jest/Vue/Codex are adopted further. I think the ideal solution here wouldn't require continuous maintenance; We just need to map ResourceLoader module names to their actual paths, right? That and do so dynamically, if feasible. Certainly any change to Resources.php should not require a bump to a NPM package :-P