Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chai .to.have.all.keys() conversion fails #60

Open
WebDevMichael opened this issue Jun 28, 2017 · 7 comments
Open

Chai .to.have.all.keys() conversion fails #60

WebDevMichael opened this issue Jun 28, 2017 · 7 comments

Comments

@WebDevMichael
Copy link

In Chai when we want to assert that a JSON object has specific keys we can use .to.have.all.keys(). As you can see in the code below, the output is incorrect and the current output compares the complete JSON object to expect.arrayContaining()

const foo = { bar: true };
// input
expect(foo).to.have.all.keys('bar');

// current output
expect(foo).toEqual(expect.arrayContaining(['bar']));
// expected output
expect(Object.keys(foo)).toEqual(expect.arrayContaining(['bar']));
@skovhus
Copy link
Owner

skovhus commented Jun 28, 2017

Thanks for reporting this. : )

Do you want to work on a PR for this?

@WebDevMichael
Copy link
Author

Yes I would like to give it a try 😉

@skovhus
Copy link
Owner

skovhus commented Jun 28, 2017

Sound good. Let me know if you need any help. : )

@WebDevMichael
Copy link
Author

src/transformers/chai-should.js:406
On this line node.type equals Identifier, the condition passes if node.type equals ObjectExpression. I want the if condition to pass so we can surround the constant with Object.keys. Do you have a suggestion for this?

I already added tests in my fork: WebDevMichael@39dbb22

@skovhus
Copy link
Owner

skovhus commented Jun 29, 2017

It sounds right to me.

@WebDevMichael
Copy link
Author

How can I see if the Identifier (constant JSON object) is of the type ObjectExpression?
Because Identifier can also be any other possible variable, i.e. an array.

@skovhus
Copy link
Owner

skovhus commented Jun 29, 2017

So that is the challenging part.

Short answer: you can't.

Longer answer: if the identifier is assigned inside the file you can search the AST for assignment to this specific identifier... But it might be assigned multiple places. AND the identifier might be defined in another file.

I cases like this, I do a best guess and log a warning that the transformation might not be right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants