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

Testing hapi routes with Tape #64

Closed
jackcarlisle opened this issue Sep 13, 2016 · 13 comments
Closed

Testing hapi routes with Tape #64

jackcarlisle opened this issue Sep 13, 2016 · 13 comments

Comments

@jackcarlisle
Copy link
Member

I've written a hapi server and now I'm trying to test it. I've been able to write tests that pass for one of my routes but have been unsuccessful for the other two. Here are the routes I'm trying to test:

server.route([
    {
      method: 'GET',
      path: '/',
      handler: function (request, reply) {
        console.log(request)
        reply.file('./public/index.html')
      }
    },
    {
      method: 'GET',
      path: '/s3_credentials',
      handler: function (request, reply) {
        if (request.query.filename) {
          var filename =
          crypto.randomBytes(8).toString('hex') +
          path.extname(request.query.filename)
          reply(s3.getS3Credentials(s3Config, filename))
        } else {
          reply('Filename required')
        }
      }
    },
    {
      method: 'GET',
      path: '/{param*}',
      handler: {
        directory: {
          path: 'public',
          listing: true,
          index: false
        }
      }
    }
  ])

And here are my tests that pass for the '/s3_credentials' route:

test('checks our /s3_credentials GET endpoint', function (t) {
  var options = {
    method: 'GET',
    url: '/s3_credentials'
  }
  Server.start((err, server) => {
    if (err) {
      console.log(err)
    }
    server.inject(options, function (response) {
      t.equal(response.statusCode, 200, '200 status code returned - ✅')
      server.stop(t.end)
    })
  })
})

test('POST request to /s3_credentials should return 404', function (t) {
  var options = {
    method: 'POST',
    url: '/s3_credentials'
  }
  Server.start((err, server) => {
    if (err) {
      console.log(err)
    }
    server.inject(options, function (response) {
      console.log(response.result)
      t.equal(response.statusCode, 404, '404 status code returned - ✅')
      server.stop(t.end)
    })
  })
})

If anyone has any experience with testing similar routes I'd really appreciate your advice!
Thanks!
Link to the source code

@adam-beck
Copy link

adam-beck commented Sep 13, 2016

I pulled down the source code your provided and was able to run all 3 tests successfully. Can you provide any more information on the errors you are seeing?

Now, I'm only seeing 3 tests and you mentioned 4 in your question but you only provided the working ones. I do have 1 failing test but it is still able to run without error. I'm assuming it fails because the call to server.inject is incorrect in test('check our GET request for our index.html').

For instance, you would want to follow the pattern in the first 2 tests:

var options = {
  method: 'GET',
  path: '/'
};

server.inject(options, function (response) { ...
  ...
  ...
});

@jackcarlisle
Copy link
Member Author

jackcarlisle commented Sep 14, 2016

@adam-beck The routes I'm trying to test are GET requests to '/', '/s3_credentials' and '/{param*}'. I added a test for the index.html but I keep receiving a 404 fail when I try to run it. Here's the new test:

test('checks GET request for our index.html', function (t) {
  var options = {
    method: 'GET',
    url: '/'
  }
  Server.start((err, server) => {
    if (err) {
      console.log(err)
    }
    server.inject(options, function (response) {
      t.equal(response.statusCode, 200, '200 status code returned - ✅')
      server.stop(t.end)
    })
  })
})

And here's the fail message:

TAP version 13
# checks our /s3_credentials GET endpoint
ok 1 200 status code returned - ✅
# checks POST to /s3_credentials returns 404
ok 2 404 status code returned - ✅
# checks GET request for our index.html
not ok 3 200 status code returned - ✅
  ---
    operator: equal
    expected: 200
    actual:   404
  ...

@adam-beck
Copy link

adam-beck commented Sep 14, 2016

I don't think it's your test that is incorrect then. The actual implementation is what's causing the test to fail. Have you made any changes to lib/routes.js? That is what the test file is relying on (https://github.com/dwyl/image-uploads/blob/serverTest/examples/direct-upload/test/server.test.js).

@jackcarlisle
Copy link
Member Author

I'll take a look at the routes and see if I've changed anything that might be the problem. Will let you know!

@adam-beck
Copy link

I just pulled in the latest changes from the testServer branch of https://github.com/dwyl/image-uploads/tree/serverTest/examples/direct-upload and it appears to be working.

@jackcarlisle
Copy link
Member Author

As in all 3 tests are now passing?

@adam-beck
Copy link

Yes. And this is with no changes to the testServer branch.

  • node version: v5.4.1

@jackcarlisle
Copy link
Member Author

hmm that's strange! Must be something to do with my laptop. I switched to node v5.4.1 but the problem still persists on my end. I'll look into it further. Thanks for taking the time to help!

@nelsonic
Copy link
Member

I did:

git clone git@github.com:dwyl/image-uploads.git
cd image-uploads/
git checkout serverTest
npm install
npm test

And got:
image-uploads-test-fails

@nelsonic
Copy link
Member

@jackcarlisle git pull on the branch...
test-pass-coverage-not-great

Tests pass. do you need help with getting Coverage up?

@nelsonic
Copy link
Member

nelsonic commented Sep 14, 2016

@jackcarlisle https://travis-ci.org/dwyl/image-uploads/builds/159896238#L288
amen-brother-squirrel

@adam-beck
Copy link

adam-beck commented Sep 14, 2016

@nelsonic why does the git pull step fix this?

Especially since when I ran git pull from the serverTest branch I received:

Already up-to-date.

@iteles
Copy link
Member

iteles commented Sep 21, 2016

@adam-beck It was @nelsonic's commit dwyl/imgup@9400254 that made the final test pass 👍

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

No branches or pull requests

4 participants