Reputation: 1309
I have a factory. It's purpose is to facilitate dependency injection. It looks something like:
import SomeImportantObject from "./SomeImportantObject"
import DataInterface from "./DataInterface"
class NoodleFactory {
this.dependency: SomeImportantObject
constructor(dependency: SomeImportantObject) {
// Dependencies.
this.dependency = dependency;
}
create(data: DataInterface) {
return new Noodle(data, this.dependency);
}
}
I was creating a test for it to ensure that it is correctly creating objects. The test is very similar to:
data = {
// Data.
}
mockDependency = "a mocked dependency."
testNoodleFactory = new NoodleFactory(mockDependency);
const expected = new Noodle(data, mockDependency);
const actual = testNoodleFactory.create(data);
test("Factory creates noodles", () => {
expect(actual).toMatchObject(expected);
});
When I run the test, I get the following error:
@@ -1,6 +1,6 @@
- Noodle {
+ Object {
All internal data matches.
What is causing the object types to be different?
Upvotes: 0
Views: 46
Reputation: 4877
Use console.log
to inspect what gets returned from each call.
Reasoning about your code (without executing it), I can see no explanation for this.
I had a quick look over the Jest source code. The object matcher is here: https://github.com/facebook/jest/blob/master/packages/expect/src/matchers.ts#L878
It uses the iterableEquality
and subsetEquality
tests from here:
https://github.com/facebook/jest/blob/master/packages/expect/src/utils.ts
toMatchObject
checks that it is an object, that they have the same constructor (that looks like the failing test), and then that the object you compare it with has a subset of the keys of the object you are testing.
I would look at the constructor of the object returned from the Noodle factory in the first instance.
If you create an MRE in a GitHub repo, I'll check it out and trace the code execution (or you could do that - just inject console.log
statements in the Jest in the node_modules
).
Upvotes: 1