flashbackzoo
flashbackzoo

Reputation: 358

Handling Browserify externals in Jest

I have a React project which uses Browserify externals. What's the best approach for testing components which reference external dependencies in Jest? For example - I have something like this...

import React from 'react';
import someExternal from 'someExternal';

class MyClass extends React.Component {
    methodToTest() {
        ...
        someExternal.doStuff();
        ...
    }
}

This works as expected in the browser, the bundle which exposes someExternal is available, so someExternal's path can be resolved.

However in my unit test, the path to someExternal can't be resolved, because the bundle which contains someExternal is not available.

Sooo what's the best approach here?

Upvotes: 2

Views: 455

Answers (1)

flashbackzoo
flashbackzoo

Reputation: 358

OK solved. You can use manual mocks, which Jest loads before it tries to load the actual module.

https://facebook.github.io/jest/docs/manual-mocks.html

Upvotes: 1

Related Questions