How do I import sinon using ES6

416 views
Skip to first unread message

tanay tandon

unread,
Jan 30, 2017, 9:18:43 AM1/30/17
to Sinon.JS
I'm trying to use sinon.spy in my test case. However I get the error TypeError: _sinon2.default.spy is not a function.  I've used npm to install sinon, the version of sinon is 1.17.7.

tanay tandon

unread,
Jan 30, 2017, 9:26:01 AM1/30/17
to Sinon.JS
I intend to test whether a particular function that is passed as props to the React Component is called when the onChange method of the React Component is called. The component code is 

import React, {Component} from 'react';
import {Checkbox} from 'react-bootstrap';

class PrimaryCheckBox extends Component {

    constructor(props) {
        super(props);
        this.onCheckBoxChanged = this.onCheckBoxChanged.bind(this);
    }

    onCheckBoxChanged(event) {
        console.log(this.props.value);
        this.props.checkboxToggledListener(this.props.data, event.target.value);
    }

    render() {
        return (

            <Checkbox onChange={this.onCheckBoxChanged}
                      checked={this.props.isChecked}
                      className="c-checkbox col-lg-4">
                <em className="fa fa-check"></em>
                {this.props.text}
            </Checkbox>

        );
    }

}

PrimaryCheckBox.propTypes = {
    data: React.PropTypes.object.isRequired,
    checkboxToggledListener: React.PropTypes.func,
    isChecked: React.PropTypes.bool.isRequired,
    text: React.PropTypes.string.isRequired
};


export default PrimaryCheckBox;


I want to test whether checkboxToggledListener is called on change. How do I test it. I'm using sinon, mocha and enzyme. 

Carl Erik Kopseng

unread,
Mar 19, 2017, 4:10:52 PM3/19/17
to Sinon.JS
I know this is probably too late, but the README for Sinon says explicitly that Sinon 1.x does not support module bundlers, which you must be using if you are using ES6. That is fully supported in Sinon 2, so just try upgrading to Sinon 2.0
Reply all
Reply to author
Forward
0 new messages