strongloop / loopback

LoopBack makes it easy to build modern applications that require complex integrations.
http://loopback.io
Other
13.23k stars 1.2k forks source link

Docs are lying: User.login doesn't exist, and logging in a test user fails #4322

Closed garrettg123 closed 4 years ago

garrettg123 commented 4 years ago

Steps to reproduce

Write a test in jest or whatever testing framework you use. This is what I've come up with from the 0 documentation I've found about testing in LB3. The problem is that the official docs outline methods like User.login that simply don't exist.

import request from 'supertest'
import app from '../server'

global.server = app.listen(12345)

describe('Subscription', () => {
  describe('create', () => {
    it('creates a Subscription', async () => {
      const email = 'test@gmail.com'
      const password = 'test'
      // Why isn't this an instance? I can't use user.createAccessToken()...
      const user = await app.models.User.create({
        email,
        password,
      })

      // const token: any = await app.models.User.login({ email, password }) THIS FUNCTION DOESN'T EXIST
      // THIS ALSO FAILS
      const token: any = await request(global.server)
        .post('/api/Users/login')
        .send({ email, password })

      const response = await request(global.server)
        .post('/api/Subscriptions')
        .send({
          accessToken: token.id,
          userId: user.id,
        })
        .expect(200)

      expect(response.body).toHaveProperty('id')
    })
  })
})

Current Behavior

First off... how does it failing logging in a user that was just created?

Unhandled error for request POST /api/Users/login: Error: login failed
    at /Users/gsquare567/Sites/think/node_modules/loopback/common/models/user.js:272:28

Expected Behavior

I expect it to work. I also expect some sort of documentation on writing tests for an application. The only docs I've seen for testing are for LB4, which is a joke right now because it doesn't have any built-in magic like user signup that LB3 has.

Link to reproduction sandbox

That's a bit ridiculous that the issue will just get closed. We're trying to help by reporting issues. Forking a repo for every bug is time-consuming.

Additional information

├── UNMET DEPENDENCY loopback@3.27 ├── UNMET DEPENDENCY loopback-boot@^2.6.5 ├── UNMET DEPENDENCY loopback-component-explorer@^6.2.0 ├── UNMET DEPENDENCY loopback-connector-postgresql@^3.8.1

I'm guessing this script just doesn't work for yarn workspaces.

Related Issues

Surprisingly, no. Loopback looks nice, but if it's improperly documented and difficult to test, I can see it failing to grow.

See Reporting Issues for more tips on writing good issues

garrettg123 commented 4 years ago

Created reproducible repo: https://github.com/garrett-gottlieb/loopback-testing-example

I'm still working on reproducing the user api login error, but one issue is already there. It's hard to reproduce because I'm also using a yarn workspaces monorepo.

garrettg123 commented 4 years ago

Found the solution, which I'm happy to share to save people digging for multiple hours. Firstly, make sure your test env for babel only ever points to your src folder, since jest doesn't need to see compiled written code. Second, you need to wrap the boot code in a promise and wait for it to finish before starting your tests. If you're using TypeScript, you also need to add the undocumented option to boot called scriptExtensions to process those scripts because of an issue with jest running loopback-boot. Something like:

// server
export const bootPromise = new Promise((resolve, reject) => {
  boot(
    app,
    {
      appRootDir: __dirname,
      scriptExtensions: ['.js', '.json', '.node', '.ejs', '.ts'],
    },
    /* ... */
  )
})

// test
beforeAll(async () => {
  await bootPromise
})

Finally, User.create() returns a pojo, whereas new User() returns an instance with all the nice functions, including createAccessToken, which is incredibly useful for testing instead of running requests through your server for every test.

You're welcome.

deepakrkris commented 4 years ago

closing as user was able to resolve build issue.