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
Async/await refactoring for tests/io folder - test.directory.js #1733
Async/await refactoring for tests/io folder - test.directory.js #1733
Conversation
tests/io/test.directory.js
Outdated
|
||
const readStream = await myDirectory.getFileAsStream('dir2/dir3/file3.txt'); | ||
|
||
const readStringFromStream = () => { |
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, there is no need to declare it as a function if we are not going to reuse it,
we could just store the promise:
const onceReadString = new Promise((resolve, reject) => {
...
});
const content = await onceReadString;
...
or we could move the function outside of the test function and pass the readStream as a parameter:
function readStringFromStream(readStream) {
return new Promise((resolve, reject) => {
...
});
}
describe('...', () => {
it('...', async () => {
...
const content = await readStringFromStream(readStream);
...
});
});
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.
@rpl I decided to go with second option because readStringFromStream(readStream) is used in 2 tests
Sorry for the hiccup and weird test breakage, #1749 just got merged and once you rebase your branch to master tests should be working fine again :) |
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.
@hameleonka follows some additional comments related some final changes on this PR (just some code improvements that we can make before leaving this test file and move to the next two from the same dir), I'm pretty sure that is is going to be the last round of changes on this PR.
tests/io/test.directory.js
Outdated
await myDirectory.getFiles(); | ||
try { | ||
await myDirectory.getPath('whatever'); | ||
unexpectedSuccess(); |
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 looks that jest with a version >= 20 supports a expect(...).rejects
assertion that we could use in out tests instead of the try { ... } catch (...) { ... }
:
e.g. this one would become something like:
await expect(
myDirectory.getPath('whatever')
).rejects.toThrow('"whatever" does not exist in this dir.');
and as a plus jest produce a very nice failure message when the rejected promise doesn't match the expected error message (or if it doesn't fail as expected).
There are a number of try { ... } catch (...) { ... }
in this file that we can make shorter and cleaner applying this approach, and it could be worth given that we are already changing this test file.
tests/io/test.directory.js
Outdated
}); | ||
}); | ||
|
||
function readStringFromStream(readStream, transform) { |
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.
This helpers seems to be useful enough to be something exported by the helpers.js module (from the tests/ directory), I would not be surprised if it could be helpful in the xpi and crx test files as well.
tests/io/test.directory.js
Outdated
const readStream = await myDirectory.getFileAsStream('dir2/dir3/file3.txt'); | ||
|
||
const content = await readStringFromStream(readStream); | ||
expect(content).toEqual('123\n'); |
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.
@rpl thanks for the very helpful Jest link. Now I have a couple of suggestions about using Jest methods. For example, maybe here we can replace lines 106, 107 with
await expect(readStringFromStream(readStream).resolves.toBe('123\n');
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.
@hameleonka 👍 sure! go for it!
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.
@rpl I've changed some Jest methods in this file
tests/io/test.directory.js
Outdated
|
||
await myDirectory.getFiles(); | ||
const string = await myDirectory.getFileAsString('dir2/dir3/file3.txt'); | ||
expect(string).toEqual('123\n'); |
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.
@rpl same here
await expect(myDirectory.getFileAsString('dir2/dir3/file3.txt').resolves.toBe('123\n');
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.
@rpl in this PR I've worked on test.directory.js in tests/io folder. Please check it out, waiting for your review.