The module is temporary workaround for passing context for shallow rendering
npm install shallow-with-context



The module is temporary workaround for passing context in shallow rendering mode. The new React Context API is not supported in shallow rendering mode yet.
It use under hood Legacy Context API and to-aop module.
``bash`
npm i shallow-with-context --save-dev
You don't have to use createContext method from shallow-with-context module for context as object.
` jsx
import { shallow } from 'enzyme';
import { withContext } from 'shallow-with-context';
import React from 'react';
const MyContext = React.createContext({ text: 'default' });
class Component extends React.Component {
static contextType = MyContext;
render() {
return
describe('your description', () => {
it('your spec', () => {
const context = { text: 'new value' };
const ComponentWithContext = withContext(Component, context);
const wrapper = shallow(
expect(wrapper).toMatchInlineSnapshot('
$3
You have to use
createContext method from shallow-with-context module for context as primitive value because Legacy Context API don't support primitive value. ` jsx
import { shallow } from 'enzyme';
import { withContext, createContext } from 'shallow-with-context';
import React from 'react';const MyContext = React.createContext('default');
class Component extends React.Component {
static contextType = MyContext;
render() {
return
{this.context}
}
}describe('your description', () => {
it('your spec', () => {
const context = createContext('new value');
const ComponentWithContext = withContext(Component, context);
const wrapper = shallow( , { context });
expect(wrapper).toMatchInlineSnapshot('
new value');
});
});
`API
$3
#### Parameters-
Component (React.Component|React.PureComponent|function|ReactObject)
- context Object$3
#### Parameters-
value` (*)