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

increase code coverage to 100% #267

Merged
merged 1 commit into from Jul 24, 2020
Merged

increase code coverage to 100% #267

merged 1 commit into from Jul 24, 2020

Conversation

ghost
Copy link

@ghost ghost commented Jul 12, 2020

Currently, 4 out of 4 files have 100% coverage:

File % Stmts % Branch % Funcs % Lines Uncovered Line #
All files 100 100 100 100
dynamic.js 100 100 100 100
index.js 100 100 100 100
routes.js 100 100 100 100
static.js 100 100 100 100

Fixes: #258

Checklist

  • run npm run test and npm run benchmark
  • tests and/or benchmarks are included
  • commit message and code follows Code of conduct

@ghost ghost mentioned this pull request Jul 12, 2020
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Good work, I've left a few nits!

static.js Outdated Show resolved Hide resolved
test/static.js Outdated Show resolved Hide resolved
test/static.js Outdated Show resolved Hide resolved
test/static.js Outdated Show resolved Hide resolved
test/static.js Outdated Show resolved Hide resolved
test/static.js Outdated Show resolved Hide resolved
test/static.js Outdated Show resolved Hide resolved
routes.js Outdated Show resolved Hide resolved
test/static.js Outdated Show resolved Hide resolved
test/static.js Outdated
const next = () => console.log()

// eslint-disable-next-line
fastify.register(Function(fastifySwagger(fastify, null, next)))
Copy link
Member

Choose a reason for hiding this comment

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

Why the Function?

Copy link
Author

Choose a reason for hiding this comment

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

If we don't use a Function constructor, avvio throws an error saying that:

 FAIL  test/static.js
✖ plugin must be a function or a promise
node_modules/avvio/boot.js                                                                       
  192 | function assertPlugin (plugin) {                                                           
  193 |   if (!(plugin && (typeof plugin === 'function' || typeof plugin.then === 'function'))) {  
> 194 |     throw new Error('plugin must be a function or a promise')                              
      | ----------^                                                                                
  195 |   }                                                                                        
  196 | }                                                                                          
  197 | 

So, if we remove the Function for this test, it'll fail

@mcollina
Copy link
Member

** I have some issues with dynamic.js would be nice if you can help me out with it.**

what issues?

@ghost
Copy link
Author

ghost commented Jul 12, 2020

** I have some issues with dynamic.js would be nice if you can help me out with it.**

what issues?

Yeah, I've spent a couple of days trying to cover all the remaining lines/statements/branches inside dynamic.js file but I just can't reach them. So, it would be nice to have some new ideas from you or someone else how I can do it.
Additionally, there are 8 uncovered branches (approx. half of them do not have an else block). What should I do with that half? Add some kind of else { console.log() } or we can ignore them?

@mcollina
Copy link
Member

Which lines are unreacheable? You can paste links to the line in the issue.


Additionally, there are 8 uncovered branches (approx. half of them do not have an else block). What should I do with that half? Add some kind of else { console.log() }

I don't understand why adding an else and a console.log helps. You need to add a test where the main condition in the if is not triggered.

@ghost
Copy link
Author

ghost commented Jul 12, 2020

Which lines are unreacheable? You can paste links to the line in the issue.

Additionally, there are 8 uncovered branches (approx. half of them do not have an else block). What should I do with that half? Add some kind of else { console.log() }

I don't understand why adding an else and a console.log helps. You need to add a test where the main condition in the if is not triggered.

I'll try more then

dynamic.js Outdated Show resolved Hide resolved
dynamic.js Outdated Show resolved Hide resolved
dynamic.js Outdated Show resolved Hide resolved
@ghost ghost changed the title increase code coverage to 98.68% increase code coverage to >99.0% Jul 14, 2020
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Good job! I think you have found a few dead code paths!

dynamic.js Outdated Show resolved Hide resolved
dynamic.js Outdated Show resolved Hide resolved
dynamic.js Outdated Show resolved Hide resolved
routes.js Outdated Show resolved Hide resolved
@mcollina
Copy link
Member

how are we for code coverage now?

@ghost
Copy link
Author

ghost commented Jul 17, 2020

Hey @mcollina

We're approx. at the same point. I'm currently trying to cover the remaining statements and branches :)
Hopefully, I'll push them today or maybe tomorrow

dynamic.js Outdated Show resolved Hide resolved
dynamic.js Outdated Show resolved Hide resolved
const localReference = jsonSchema.$ref.split('/')[2]
return localRefResolve(externalSchemas[localReference], externalSchemas)
}
return jsonSchema
Copy link
Author

Choose a reason for hiding this comment

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

We don't need this "default" return statement. In this function, the flow goes either into the first if or into the second one due to the schema validation rules as far as I understood.
Therefore, I refactored things a bit.

@@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"prepare": "node prepare-swagger-ui",
"test": "standard && tap test/*.js && npm run typescript",
"test": "standard && tap --100 test/*.js && npm run typescript",
Copy link
Author

Choose a reason for hiding this comment

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

To enforce 100% percent coverage in the future

Copy link
Member

Choose a reason for hiding this comment

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

Perfect!

dynamic.js Outdated Show resolved Hide resolved
@@ -106,22 +106,20 @@ module.exports = function (fastify, opts, next) {
swaggerObject.externalDocs = externalDocs
}

if (!ref) {
Copy link
Author

Choose a reason for hiding this comment

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

I've played with code a bit more and found out that we don't need this if clause. The swagger function is called only once at the very beginning, and the second call does not allow us to trigger it once again. Therefore, we're quite safe to remove it, it should not break any logic.

@ghost ghost changed the title increase code coverage to >99.0% increase code coverage to 100% Jul 18, 2020
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm, really good work!

@ghost
Copy link
Author

ghost commented Jul 24, 2020

Hey @mcollina

Should I add something or this can be merged to the main branch? Do I need to request some more reviews?

@mcollina mcollina merged commit adf0c95 into fastify:master Jul 24, 2020
@mcollina
Copy link
Member

I was waiting for more reviews.. but not really needed! Landed!

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

Successfully merging this pull request may close these issues.

100% code coverage
1 participant