Comments (6)
I'm just gonna open a discussion on this so that I'm clear what we're actually capable of testing considering the API requires a valid key to get anything valuable returned. I'm all new to this kind of testing so need somewhere to dump some thoughts.
- How would you go about writing these tests so that either an API isn't required to run the tests successfully or we create a config file that requires users to add their own API key before running tests? In this case we could use this file.
- How much of this can we actually test seen as most of the functionality is actually on the WPT API. As mentioned here we shouldn't really be testing the WPT API. Should be be checking that all the flags for on the CLI (but again that requires a valid API key and we'd need to test the values returned are the ones that match the flags).
Just a mind dump for now. Thoughts/input appreciated.
from perfbudget.
This is kind of a religious question, how to write unit tests.
But I'd love to share my opinion with you.
How would you go about writing these tests so that either an API isn't required to run the tests successfully or we create a config file that requires users to add their own API key before running tests? In this case we could use this file.
I simply wouldn't. Our tests should ( at least in my opionion ) test our functionality and not more.
E.g.
var depA = require( 'dep' );
var myModule = {
doSomething : function() {
var result = depA.something();
if ( result ) {
console.log( 'yeah' );
} else {
console.log( 'noooo' );
}
}
}
When you look at this tiny snippet, the functionality of myModule
is not that complicated. The tests for the function doSomething
have to cover two things. Log yeah
when this dependency function call returns something truthy and log noooo
if it's falsy.
That's it. So I'd go with mocking the dependency and control what this function will return to see if our code is behaving as it should.
For mocking dependencies two options come to my mind:
Either loading all the dependencies with a dependency injection system. Angular does it this way, and that's why it's so easy to test. You just inject mocked objects and can perfectly check how you code behaves in given cases. Needs some bigger refactoring.
Using something like https://www.npmjs.com/package/proxyquire. So changing the require( 'webpagetest' )
and mock the function call and see what our snippets does. :) Might work out of the box.
Anyways starting writing tests usually also leads to refactoring because testing a function most often shows problems that were not thought through.
I hope this explanation helps... And I can give it a shot if you like. :)
from perfbudget.
Buy that man a beer! 😄
Thanks for the explanation it's definitely what I needed – I'm gonna have a go at writing these this weekend and will let you know how I get on.
from perfbudget.
Buy that man a beer!
Haha. Maybe on a conf or something. :)
Glad my explanation helped.
I just recently tried proxyquire and it worked fine. So I'd give it also a try here.
from perfbudget.
Have every intention of doing this – been super busy of late. Will take a look ASAP.
from perfbudget.
No problem!
It's your project. I'm just here to help, if needed. ;)
And if I can support let me know. :)
from perfbudget.
Related Issues (4)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from perfbudget.