-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Adds unit tests to the Keep Markup plugin #1646
Adds unit tests to the Keep Markup plugin #1646
Conversation
@@ -96,4 +96,4 @@ | |||
env.highlightedCode = env.element.innerHTML; | |||
} | |||
}); | |||
}()); | |||
}(self, document)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I know, this change is mandatory otherwise we can't fake self
and document
in the unit tests.
In a browser environment both self
and document
are global and will be resolved.
43f91a1
to
a6aeef3
Compare
I need to think about this a bit. I'm not in love with mocking |
If you have a better idea, I'm more than interested! 😉
I don't think we can reuse much (or anything at all). For instance Custom Class checks: if ((typeof self === 'undefined' || !self.Prism) && (typeof global === 'undefined' || !global.Prism)) {
return;
} But copy to clipboard checks: if (typeof self === 'undefined' || !self.Prism || !self.document) {
return;
} And this plugin checks: if (typeof self === 'undefined' || !self.Prism || !self.document || !document.createRange) {
return;
}
Node.js has a cache but it's possible to invalidate the cache so every delete require.cache[require.resolve('../../../prism')]
require('../../../prism') We could create a tiny wrapper to require Prim in the tests: const util = require('../plugin-test-util')
util.requirePrism() |
@RunDevelopment I've removed Node.js 4 and Node.js 9 (EOL) and added |
I'm on board with this, and we can massage it into place as we add additional tests. I would however prefer to keep all tests in the root |
plugins/keep-markup/tests/test.js
Outdated
expect(result.end.length).to.equal(1) | ||
expect(result.nodes.length).to.equal(1) | ||
expect(result.nodes[0].nodeName).to.equal('SPAN') | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I almost think snapshot tests would be better here than all these assertions. The other thing I've done is something like:
expect(result.innerHtml).to.equal('<some>html</some>');
If we didn't want to add a new dep & auto-generate them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I almost think snapshot tests would be better here than all these assertions. The other thing I've done is something like:
You tell me 😉
Do you know a good snapshot testing library ? In my opinion we should do what you suggest:
expect(result.innerHtml).to.equal('<some>html</some>');
Keep it simple.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like this simple approach.
We can always add a more elaborate testing scheme later on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, no additional lib needed then. Alternatively: https://github.com/suchipi/chai-jest-snapshot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively: https://github.com/suchipi/chai-jest-snapshot
Maybe I don't get the library but don't they separate the expected result from the actual input? Wouldn't it be better to have actual and expected in the same file in our case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just get back to the code and it won't work because we are using a dummy implementation of createRange
(since jsdom
doesn't implement it).
So innerHTML
will be empty/undefined.
091bd91
to
96e7ebb
Compare
Conflicts resolved and I've moved the tests to the |
Last nit: please remove the package-lock.json, as we don't have one of those at this time. |
96e7ebb
to
09ac826
Compare
Done 👍 |
Thank you for the contribution! If you fixup #1622, we can get that in now too. |
Adds unit tests on the Keep Markup plugin using JSDOM to create a "fake" DOM.
To run the tests, open a terminal and type:
Sample output: