freeCodeCamp / pantry-for-good

An open source food bank logistics and inventory management tool
Other
395 stars 189 forks source link

How to run the tests correctly? #363

Closed spotlesscoder closed 6 years ago

spotlesscoder commented 6 years ago

When I run the command npm run test on the staging branch, I get the following error:


updating a user
      √ updates the database (47ms)
      √ returns the updated user object (45ms)
      1) admins can set other users as admins
      √ regular users cannot update another user profile (40ms)
    updating profile
      √ regular users can update their own profile
      √ update ignores req.body properties: displayName, provider, salt, resetPasswordToken and roles
      √ regular users cannot make themselves admins

  Volunteer Api
    User routes
      √ creates volunteers
      √ shows a volunteer (38ms)
      √ updates a volunteer (45ms)
    Admin routes
      √ lists volunteers (56ms)
      √ deletes volunteers (64ms)

  Api router
    √ uses subrouters
    √ checks admin/ routes for admin role

  109 passing (5s)
  1 failing

  1) User Api updating a user admins can set other users as admins:
     Error: expected 200 "OK", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:266:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:281:11)

Is this ok or did I miss something the needs to be set up for the tests?

Another question: Is it possible to compile run the tests automatically when a piece of source code is changed?

jspaine commented 6 years ago

There are some issues with the tests lately, I'm not sure what the problem is. You can run in watch mode with npm run test:server or npm run test:client

spotlesscoder commented 6 years ago

OK, thanks

spotlesscoder commented 6 years ago

How can I debug into the tests? I really want to find out the source of the problem because I can't even commit with the failing tests and I don't want to change all the commit hook stuff

jspaine commented 6 years ago

You can do npm run test:server:once -- --inspect after putting debugger in the test you want to break on, then open chrome://inspect.

You can also just commit or push with --no-verify to skip running the tests.

spotlesscoder commented 6 years ago

Thank you very much for all the help you provide. It's a pleasure to work like this.

spotlesscoder commented 6 years ago

I did not succeed doing it. By "putting the debugger in the test" do you mean setting the breakpoint? I don't know how chrome would be able to read the breakpoints I set in vs code.

jspaine commented 6 years ago

Thanks! I mean the debugger statement (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger), sorry I thought it might not have been that clear too.

It's also not great because you have to refresh chrome i think every time you re-run the tests, but I'm not sure there's a better way.

spotlesscoder commented 6 years ago

But I think removing the :once would be correct, right? Otherwise, I don't see anything appearing in chrome://inspect

But even with the test running continuously, it didn't work.

I inserted the debugger like that (in server/tests/user.spec.js):

it('admins can set other users as admins', async function(){
      const user = await User.create({
        firstName: 'first',
        lastName: 'last',
        email: '123@example.com',
        roles: [],
        provider: 'local',
        password: '12345678'
      })

      const adminUser = createTestUser('admin', ADMIN_ROLE)
      const adminSession = await createUserSession(adminUser)
      const adminReq = supertest.agent(adminSession.app)
      debugger;
      await adminReq.put(`/api/admin/users/${user._id}`)
        .send({ isAdmin: true })
        .expect(200)

      const updatedUser = await User.findById(user._id).lean()
      expect(updatedUser).to.have.property('roles')
      expect(updatedUser.roles).to.include(ADMIN_ROLE)
    })

Then I ran npm run test:server -- --inspect

Then, in chrome://inspect, I clicked "inspect" in the red marked area:

image

This window opened:

image

Now im not sure how to continue

spotlesscoder commented 6 years ago

@jspaine: I don't wanna rush you but want to make sure you have seen I'm still having trouble ;) It's totally fine if you don't have time at the moment ... it's not super urgent but would be great to know.

kenjiO commented 6 years ago

I am able to debug the server tests directly in VScode with this configuration in my .vscode/launch.json file.

    {
      "name": "Debug Server Tests",
      "type": "node",
      "request": "launch",
      "protocol": "inspector",
      "cwd": "${workspaceRoot}",
      "runtimeExecutable": null,
      "runtimeArgs": [
        "--nolazy"
      ],
      "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
      "args": [
        "--no-timeouts",
        "--color",
        "--require",
        "babel-register",
        "--require",
        "server/entry.test.js",
        "./server/tests/**/*.spec.js"
      ],
      "smartStep": true,
      "skipFiles": [
        "node_modules/**/*.js",
        "<node_internals>/**/*.js"
      ],
      "env": {
        "NODE_ENV": "test"
      }
    }

There is a problem where the breakpoints get moved to the last line of the file when I launch the tests. So I have to re-add the breakpoints while the test is running each time I debug the tests. There may be a setting with sourcemaps that could fix this.

spotlesscoder commented 6 years ago

Thanks! I'll try it

kenjiO commented 6 years ago

I got the breakpoints working by adding "sourceMaps": true in the .vscode/launch.json configuration. Then I changed the .babelrc file to look like

  "presets" : [
    "flow"
  ],
  "plugins": [
    "transform-es2015-modules-commonjs",
    "transform-async-to-generator",
    "transform-object-rest-spread"
  ],
  "env": {
    "test": {
      "plugins": ["istanbul", "babel-plugin-rewire"],
      "sourceMaps": true,
      "retainLines": true
    }
  }
}
spotlesscoder commented 6 years ago

Great, works for me, too!

spotlesscoder commented 6 years ago

Thanks!

spotlesscoder commented 6 years ago

@jspaine : I think the error is a mongodb related one. Maybe switching to the library referred here will help: https://github.com/chevex-archived/mongoose-auto-increment/issues/74