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

Write some tests #15

Closed
amcewen opened this issue Sep 1, 2020 · 12 comments
Closed

Write some tests #15

amcewen opened this issue Sep 1, 2020 · 12 comments
Assignees

Comments

@amcewen
Copy link
Member

amcewen commented Sep 1, 2020

For the API server at least.

(Once we've got them we can hook up the Github CI to run them too)

@amcewen
Copy link
Member Author

amcewen commented Sep 1, 2020

We discussed possibly using Cucumber (or a NodeJS equivalent).

When I've used it in the past I made these notes - copying them in here in case they're useful. This was on a Ruby on Rails, so it some of it might not apply to a NodeJS setup...

@JackiePease
Copy link
Contributor

Looked at this tutorial: https://github.com/denford/tutecumber which introduces basic concepts

@amcewen
Copy link
Member Author

amcewen commented Jan 3, 2021

Seems Simon Willison is also looking into testing and javascript, and writing up his exploration in simonw/datasette#1165 (he's also favouring Cypress, but then he's also been talking to Dracos, one of @zarino's colleagues :-)

@JackiePease
Copy link
Contributor

"Quality Assurance and Testing with Chai" here: https://www.freecodecamp.org/learn
Lots of examples, but doesn't give much of an overview of the testing framework you'd run it in

@JackiePease
Copy link
Contributor

I've installed Cypress (as an npm module) and ran a simple test to open the Booking System main page. This seems much easier than in Selenium, so I'll continue to explore Cypress.

@JakeWalker23 JakeWalker23 self-assigned this Jan 23, 2021
@JakeWalker23
Copy link

Added some integration tests to branch feature/issue15-testframeworkexperiments which tests the /api/resources endpoint.

There is a small example of how to unit test a function that was in calendar.js.

Also looking at Github workflows that add CI into this project. It will be a good addition to the testing.

@JackiePease
Copy link
Contributor

JackiePease commented Feb 1, 2021 via email

@JakeWalker23
Copy link

Hi Jackie,

Not tonight I have some other work to do. Give it a go, you might have to refactor the function so it does what you expect.

We can catch up tomorrow.

@JackiePease
Copy link
Contributor

JackiePease commented Feb 2, 2021 via email

@amcewen
Copy link
Member Author

amcewen commented Feb 2, 2021

@JakeWalker23 Good to see you've made some progress on it. Agreed that hooking in the CI would be good, feel free to spin that out as a separate issue (and start working on it too if you want), as I think it'd make more sense to run alongside the testing?

@JackiePease
Copy link
Contributor

info from Jake:
Testing 'pyramid' gives context to what we are doing with our app, and as explained unit tests are what we want to implment because they are faster to run and cheaper to implement when it comes to debugging.

Integration:

An integration test is testing the integration of a request sent to our endpoint and the response it sends back. An example is sending a HTTP request to www.google.co.uk and Google then sending a response back with the content. A successful HTTP request returns a 200 HTTP response which is what we test for in our code attached.

In our code example we make a GET request to our endpoint (/api/resources/) which, after a slight refactor to the app.js file, sends back a 200 HTTP status for us to test for a successful request.

We can therefore test for the opposite with a bad endpoint (/api/resources/abcdefg) which returns a 404 HTTP status which equates to 'not found'.

Each endpoint created can be integration tested so there is potential for the other few endpoints to be implement this.

Unit:

A unit test is testing just one unit of code, most likely a function, to test that this piece of code returns what we want. For example if we had a function multiplyBy2() and passed in 2, we would assert/expect the output to be 4. This is an example of a unit testing a function.

code:

function multiplyBy2(number) {
return number * 2
}

We could then test this function similar to how we have done in the image attached with comments. It starts the same as our usual flow.

import our testing libraries (chai etc)

import our function to be tested (Lets use multiplyBy2() for now)

describe block {
it (tests that multiplyBy2 returns 2) {
// Given I have a value
let value = 2

// When I call multiplyBy2
 let result = multiplyBy2(value)


// Then I expect this value to be 4
expect(result).to.be.equal(4)

}
}

This is an example of a small unit test, but together these units add up to form a substantial testing suite. Each function ideally should have a test associated to it, so that is something to relay in the meetup!

To run the current tests associated on our branch, run npm t.

stuartedward added a commit that referenced this issue Jun 26, 2021
@JackiePease
Copy link
Contributor

Initial tests written with Mocha and Chai have been merged with the Development branch

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

No branches or pull requests

3 participants