问题
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