Closed garrettg123 closed 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.
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.
closing as user was able to resolve build issue.
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.Current Behavior
First off... how does it failing logging in a user that was just created?
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