Git Product home page Git Product logo

Comments (6)

oliverfarrell avatar oliverfarrell commented on June 25, 2024

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.

  1. 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.
  2. 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.

stefanjudis avatar stefanjudis commented on June 25, 2024

This is kind of a religious question, how to write unit tests. :bowtie:
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.

oliverfarrell avatar oliverfarrell commented on June 25, 2024

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.

stefanjudis avatar stefanjudis commented on June 25, 2024

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. :bowtie:

from perfbudget.

oliverfarrell avatar oliverfarrell commented on June 25, 2024

Have every intention of doing this – been super busy of late. Will take a look ASAP.

from perfbudget.

stefanjudis avatar stefanjudis commented on June 25, 2024

No problem! :bowtie:

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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.