-
Notifications
You must be signed in to change notification settings - Fork 15
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
{PROD4POD-1931} Add tests for poly-importer #1204
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "\u267B-PROD4POD-1930"
Conversation
And set up properly jest to work in a jsdom environment
Up to 20% coverage now
Seems the right one for an abstract base class :white_check_mark: too
Added dep for testing
But this is going to need some more mock code, so will leave it like this
# ✍️ Description Assigning a value to an attribute violated encapsulation, which is easy to do in JS, but still. This is a cleaner API, with a private `changeListener` which is assigned when the object is built. ### 🏗️ Works with PROD4POD-1931 Still cleaning up and refactoring the importer, looking at improving coverage. Related to #1209 and #1204, but wanted it to be considered on its own. Also it's a change of API, which is better if considered on its own.
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.
Pretty good start I'd say!
@@ -2,26 +2,24 @@ class PolyImportError extends Error { | |||
constructor(type, cause) { | |||
super(`Failed to ${type} file`); | |||
this.cause = cause; | |||
this.name = this.constructor.name; |
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.
❤️
}); | ||
}); | ||
|
||
describe("Can remove archives ", () => { |
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.
Looking at the code - it doesn't remove any archives, does it? Isn't it more it("ignores non-existent archives")
? (I also think this is more of an it
to "File storage" rather than its own describe
, but that part is a nit.)
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.
It's quite incomplete at the moment, I'm afraid. But yes, this specific one could be renamed and instantiated as you say.
@@ -3,6 +3,7 @@ | |||
* | |||
* @class | |||
*/ | |||
|
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.
Nit: We usually don't have empty lines between JSDoc comment and declaration.
# ✍️ Description The mock for zip-files is more useful in the common `poly-import` module. That way it can also be used in the module itself to mock zipfiles, and imported from any module that needs to mock zips. While we were at it, boost a bit coverage of said test. ### 🏗️ Fixes PROD4POD-1931 It is a precondition for #1204, since `Storage` needs zip files to be tested. ## ℹ️ Other information Some minor refactoring also made. Might include some refactoring of tests, mainly to reduce boilerplate. Not for the time being. ### Additional functionality * ✅ adds tests in parts that were not covered, for instance, file name analysis in Facebook
}); | ||
}); | ||
|
||
describe("File storage", () => { |
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.
Nit: Figure these two describe blocks could be merged, but not that important.
@@ -0,0 +1,11 @@ | |||
import { Importer } from "../src"; | |||
|
|||
describe("Importer can't be instantiated ", () => { |
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.
Nit: What's with the trailing space here in the string? Also below.
@@ -44,6 +44,7 @@ describe("Report metadata analysis", () => { | |||
), | |||
], | |||
]); | |||
console.log(zipFile); |
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.
Nit: Forgotten?
fileStorage = new FeatureFileStorage(pod); | ||
}); | ||
it("ignores non-existent archives", () => { | ||
expect(fileUri).toMatch(polyProtocolRegex); |
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.
Why export the regex and test it? I think it'd be better to leave it as an implementation detail and write a dedicated test for handling of invalid URLs. That regex is never used directly in production code, so no point testing it directly IMHO.
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.
Actually, testing the fileUri against the protocol. The problem with leaving it as an implementation details is that we might end up having incompatible implementation details, and causing issues such as this one https://jira.polypoly.eu/browse/PROD4POD-1133 So it's not so much an implementation details as a single source of truth, because eventually we will be interchanging files between different implementations.
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.
That's a proposed API change - quite a different story. If we make that change we can test it, because the API would guarantee us to local paths. Right now it gives us internal URLs. You can test that it's a valid URL, but that's really as far as we should go. Testing that some internal regex used in the code matches the output makes little sense to me. What if the regex is wrong, and that causes the issue? I'd be OK with duplicating the regex in the tests, that way we at least don't have to expose implementation details...
const polyProtocol = "polypod://"; | ||
const polyProtocolRegex = new RegExp(`^${polyProtocol}`); | ||
export const polyProtocol = "polypod://"; | ||
export const polyProtocolRegex = new RegExp(`^${polyProtocol}`); |
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.
See above - I'm not a fan of exposing things for test purposes, the ability to mock components not under test being the only exception. Otherwise, things should be as private as possible and tests should make an effort to test code paths through the public API.
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.
Not really for testing; maybe I should have factored out this change as I have done with the rest. Main intention here is to make the polyProtocol a single source for all the literals that are used all over.
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.
Just realised that you could just call isPolypodUri with no need to export these internals.
That said, I'm still not sure why the unit tests for poly-import need to ensure that importArchive works as expected... They can't even, only in the test feature can we test API across platforms. The poly-import tests are for testing code in this library, nothing outside it.
# ✍️ Description While doing #1204 a bit of refactoring is needed; and I realized this return value is never used. Giving it its own PR since it's a change in API, and want it to be tested with the rest. Also, some refactoring mainly eliminating `await` and `return` where they're not actually needed. ### 🏗️ Fixes PROD4PROD-1931 It contributes to cleaning the repository, and improve test coverage in `importer`; eliminating code that's not used will no doubt contribute to that.
# ✍️ Description While doing #1204, the need to refactor mocks arose. It became a bit hefty for an already heavy PR, so it's better taken outside. ### 🏗️ Working with PROD4POD-1931 ## ℹ️ Other information I'll leave it as draft for the time being, since some functions might be unneeded after this. Feedback welcome anyway.
it("Returns increasing elapsed time", () => { | ||
const oldTime = telemetry.elapsedTime(); | ||
expect(oldTime).toBeGreaterThan(0); | ||
expect(telemetry.elapsedTime()).toBeGreaterThan(oldTime); | ||
}); |
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.
nit: just for its readability
it("Returns increasing elapsed time", () => { | |
const oldTime = telemetry.elapsedTime(); | |
expect(oldTime).toBeGreaterThan(0); | |
expect(telemetry.elapsedTime()).toBeGreaterThan(oldTime); | |
}); | |
test("elapsedTime() returns increasing elapsed time", () => { | |
const oldTime = telemetry.elapsedTime(); | |
expect(oldTime).toBeGreaterThan(0); | |
const newTime = telemetry.elapsedTime(); | |
expect(newTime).toBeGreaterThan(oldTime); | |
}); |
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.
Not really... In this case I don't think readability would add much to it, just verbosity.
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.
mostly meant it for the test title - not just the separated var for newTime...
classData.forEach(([testClass, testMsg]) => { | ||
const testCause = "test"; |
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 this checks the classData
(defined here and not in the real implementation), it seems to me it creates more tech-debt and potential issues if Error types are refactored at some point or more are added.
We might want to fill this object dynamically from PolyImportError
somehow and not on our own? (or any other alternative)
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.
Well, it checks that it's effectively closed for modification, as in the open/closed principle. I'll check anyway if it can be improved through encapsulation. And any way thanks for the suggestion, since this is still a draft.
Co-authored-by: irstavr <[email protected]>
Co-authored-by: irstavr <[email protected]>
Co-authored-by: irstavr <[email protected]>
✍️ Description
Increase coverage of core tests.
poly-importer
is used by all features, and coverage was rather dimThe scope of this PR will be defined on the run. For the time being, it's 🚧 and tries to increase it beyond original 13.68%
🏗️ Fixes 1931
ℹ️ Other information
Refactoring will be made as needed. For starters, it needed configuration of the jest jsdom environment.