Jest Cannot Create Objects from Imported Classes

懵懂的女人 提交于 2019-12-24 06:24:51

问题


In ChangesFactory-test.js I am importing the class I want to test like so:

import ChangesFactory from 'path/to/src/ChangesFactory'

I am trying to create an instance in a test case like this:

describe('Changes', function () {
it ('testConstructor', function() {
    // test that a username can be passed in
    let changesFactory = new ChangesFactory(USERNAME);

However, running the above code leaves the value of changesFactory undefined.

The constructor of the ChangesFactory class is written as:

export default class ChangesFactory {
constructor(value) {
    if (typeof value === 'string') {
        this.changes = new Changes(value);
    } else if (value instanceof Changes) {
        this.changes = changes;
    }
};

and I am running the command npm test ChangesFactory

I am relatively new to Jest and ES6 and am wondering if I am missing something. What could I do to test the ChangesFactory class?

Edit

Using Webstorm's 'Evaluate' tool, I was able to look at changesFactory and found that the properties of changesFactory exist. I tried calling some of the functions in Evaluate and they seemed to return the desired objects.

changesFactory.getChanges();
------
result: Changes { username: 'uname', ... }

However, the method I wanted to call is supposed to return an object, but will only do so in the evaluator. The problem is that the following value is not being assigned when it should:

// test that {Changes} can be passed in
let changes = changesFactory.getChanges(); // changes does not get assigned here?!

来源:https://stackoverflow.com/questions/49226551/jest-cannot-create-objects-from-imported-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!