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

Tests for logger #93

Merged
merged 3 commits into from
Mar 8, 2020
Merged

Tests for logger #93

merged 3 commits into from
Mar 8, 2020

Conversation

waynevanson
Copy link
Contributor

@waynevanson waynevanson commented Mar 3, 2020

#80 Tests for the logger.

I've used jest.resetModules() alongside beforeEach() and afterEach() to change process.env.NODE_ENV from "test" to "production", as logger[name] relies on NODE_ENV not to be in test mode.

Test coverage is 100% for this module.

Can I have someone check out these tests? I've never written tests like this before and am unsure what the standard.

I made the helper functions because I'll use them for the other log functions.

I extended this branch off a previous pull, which is why there is all this other stuff. I'll get rid of it later before opening up.

@sQVe
Copy link
Collaborator

sQVe commented Mar 3, 2020

I do not think it is a good idea to manipulate NODE_ENV.

If we refactor the individual methods in logger like this:

export const info = (msg: string) => {
  const text = chalk`{green.bold INFO:} ${msg}`;

  if (process.env.NODE_ENV !== "test") {
    console.log(text);
  }

  return text;
};

We could instead just test the return value easily, either manually or via a snapshot.

@waynevanson
Copy link
Contributor Author

I like that a lot better! I'll add that in.

@waynevanson
Copy link
Contributor Author

waynevanson commented Mar 3, 2020

@sQVe If we are using NODE_ENV as a conditional, we can't test the case outside of test without changing it.

I think as long as we're mindful that we're changing it, it's okay. Otherwise we won't have the code coverage for it. All we'd be checking for is the colour of the text, which is half it's function.

What about something like this? It changes the env, calls the function then sets the previous environment back.

function useNodeEnv(
  env: "development" | "test" | "production",
  handler: () => void
) {
  const defaultEnv = process.env.NODE_ENV.toString();
  process.env.NODE_ENV = env;
  handler();
  process.env.NODE_ENV = defaultEnv;
}

useNodeEnv('production',() => {/* test in environment */})

@sQVe
Copy link
Collaborator

sQVe commented Mar 3, 2020

I think it is fine that the NODE_ENV === 'test' are not covered and can be excluded from coverage. If your going to mutate NODE_ENV that should be done in a beforeEach and afterEach with proper cleanup via jest.resetModules() or something similar.

@waynevanson
Copy link
Contributor Author

Okay, I think I can do that after reading some stuff online. Thank you for the tips!

@waynevanson waynevanson marked this pull request as ready for review March 4, 2020 22:18
Copy link
Collaborator

@sQVe sQVe left a comment

Choose a reason for hiding this comment

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

Cleanup PR from moveFiles changes.

@waynevanson waynevanson requested a review from sQVe March 5, 2020 20:40
@codecov
Copy link

codecov bot commented Mar 5, 2020

Codecov Report

Merging #93 into develop will increase coverage by 2.83%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop      #93      +/-   ##
===========================================
+ Coverage    90.99%   93.82%   +2.83%     
===========================================
  Files           18       19       +1     
  Lines          422      470      +48     
  Branches       106      114       +8     
===========================================
+ Hits           384      441      +57     
+ Misses          38       29       -9
Impacted Files Coverage Δ
src/shared/logger.ts 100% <100%> (+50%) ⬆️
src/index/generateTrees.ts 100% <0%> (ø) ⬆️
src/index/shared/findImports.ts 90% <0%> (ø) ⬆️
src/index/generateTrees/printTree.ts 100% <0%> (ø) ⬆️
src/index/shared/detect-lonely-files.ts 100% <0%> (ø)
src/index/generateTrees/toFractalTree.ts 93.75% <0%> (+0.59%) ⬆️
src/index.ts 70.58% <0%> (+2.4%) ⬆️
src/index/generateTrees/buildGraph.ts 100% <0%> (+3.03%) ⬆️
src/index/formatFileStructure/moveFiles.ts 88.88% <0%> (+3.17%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8d4305a...1bf4967. Read the comment docs.

src/shared/logger.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@AnatoleLucet AnatoleLucet left a comment

Choose a reason for hiding this comment

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

Some minor changes and we're good!

tests/logger.test.ts Outdated Show resolved Hide resolved
tests/logger.test.ts Outdated Show resolved Hide resolved
tests/logger.test.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@AnatoleLucet AnatoleLucet left a comment

Choose a reason for hiding this comment

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

Great, nice work! 🎉

@AnatoleLucet AnatoleLucet merged commit 70b3d6f into benawad:develop Mar 8, 2020
@github-actions
Copy link

🎉 This PR is included in version 0.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

3 participants