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

Async/await refactoring for tests/io folder - test.directory.js #1733

Merged
merged 5 commits into from Jan 17, 2018
Merged

Async/await refactoring for tests/io folder - test.directory.js #1733

merged 5 commits into from Jan 17, 2018

Conversation

hameleonka
Copy link
Contributor

@rpl in this PR I've worked on test.directory.js in tests/io folder. Please check it out, waiting for your review.


const readStream = await myDirectory.getFileAsStream('dir2/dir3/file3.txt');

const readStringFromStream = () => {
Copy link
Member

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);
    ...
  });
});

Copy link
Contributor Author

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

@EnTeQuAk
Copy link
Contributor

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 :)

Copy link
Member

@rpl rpl left a 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.

await myDirectory.getFiles();
try {
await myDirectory.getPath('whatever');
unexpectedSuccess();
Copy link
Member

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.

});
});

function readStringFromStream(readStream, transform) {
Copy link
Member

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.

const readStream = await myDirectory.getFileAsStream('dir2/dir3/file3.txt');

const content = await readStringFromStream(readStream);
expect(content).toEqual('123\n');
Copy link
Contributor Author

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');

Copy link
Member

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!

Copy link
Contributor Author

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


await myDirectory.getFiles();
const string = await myDirectory.getFileAsString('dir2/dir3/file3.txt');
expect(string).toEqual('123\n');
Copy link
Contributor Author

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');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Successfully merging this pull request may close these issues.

None yet

3 participants