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

How to run LB3 app tests when the app is mounted in a LB4 project #3978

Closed
kevintc opened this issue Oct 21, 2019 · 11 comments
Closed

How to run LB3 app tests when the app is mounted in a LB4 project #3978

kevintc opened this issue Oct 21, 2019 · 11 comments

Comments

@kevintc
Copy link

kevintc commented Oct 21, 2019

Steps to reproduce

Migration from lb3 to lb4, our legacy app lives in ./lb3app and tests (using Mocha) are in ./lb3app/tests

Current Behavior

In ./lb3app/tests/main.js, we have const app = require('../server/server').
When trying to run the tests using mocha ./lb3app/tests/main.js app is undefined.

I tried typescript-require and:

const { ApiApplication } = require('../../src/index.ts')
const app = new ApiApplication()

Expected Behavior

I expected a way to call the new lb4 app object from lb3 tests framework.
Or do we need to write all the tests again in .ts?

Link to reproduction sandbox

Our current setup is close to this one: https://github.com/strongloop/loopback-next/tree/master/examples/lb3-application
I'll see if I can add mocha to it.

Additional information

node -e 'console.log(process.platform, process.arch, process.versions.node)':

darwin x64 10.16.0

npm ls --prod --depth 0 | grep loopback:

├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]

Related Issues

See Reporting Issues for more tips on writing good issues

@kevintc kevintc added the bug label Oct 21, 2019
@dhmlau
Copy link
Member

dhmlau commented Oct 22, 2019

@nabdelgadir, could you please take a look? Thanks!

@nabdelgadir
Copy link
Contributor

Hi @kevintc, can you try:

const {ApiApplication} = require('../../')
const app = new ApiApplication()

@bajtos
Copy link
Member

bajtos commented Nov 1, 2019

@kevintc thank you for starting this discussion.

First of all, why would you like to access LB4 application from your LB3 tests? In my mind, this creates a cyclic dependency (LB4 app depends on LB3 app, but your LB3 tests want to depend on LB4 app).

Having said that, I think we didn't consider how to run LB3 app tests when the app is mounted in a LB4 project, so I am not surprised there are rough edges now.

@bajtos bajtos self-assigned this Nov 1, 2019
@romain-ortega
Copy link

Hey, thanks for your answers @bajtos @nabdelgadir !

We managed to make tests work by booting both LB4 app (@nabdelgadir method 👍) and LB3 app (via require ../server/server)

With LB4 Application we can retrieve open API spec and execute our end-to-end tests.

However, we were wondering if there is a way to access our LB3 methods directly from LB4 application, without requiring LB3 server manually ?

@nabdelgadir
Copy link
Contributor

However, we were wondering if there is a way to access our LB3 methods directly from LB4 application, without requiring LB3 server manually ?

Sorry for the super delayed response! Do you have an example of a LB3 method that you'd want to access directly?

@bajtos bajtos added the 2019Q2 label Mar 9, 2020
@agnes512 agnes512 changed the title Access lb4 app object from legacy lb3 tests framework How to run LB3 app tests when the app is mounted in a LB4 project Mar 10, 2020
@dhmlau dhmlau added this to the April 2020 milestone Mar 17, 2020
@dhmlau
Copy link
Member

dhmlau commented Mar 17, 2020

@bajtos, could you please add acceptance criteria? thanks!

@bajtos
Copy link
Member

bajtos commented Mar 31, 2020

could you please add acceptance criteria?

Hmm, I think we need to clarify what user stories we want to document. Here is what I am thinking about:

As a user migrating from LB3 to LB4, I created a new LB4 project and mounted my LB3 application as explained in https://github.com/strongloop/loopback-next/tree/master/examples/lb3-application

Now I want to run my existing LB3 tests as part of LB4 project's npm test command. How can I accomplish that? We should have a documentation for this step, I think README of lb3-application is a good place for such docs.

There are two kinds of tests we need to support. Ideally, there should be a way how to run these tests from the original JavaScript files stored in the LB3 project, so that users don't have to migrate all test files to LB4.

  1. Acceptance-level tests making HTTP calls to invoke application logic, e.g. POST /users/login. I think this is easier because the tests need just the port number where the top-level app is listening on, this app can be the LB4 (or Express) app.

  2. Integration-level tests that are using JS API to call application logic, e.g. MyModel.create(). These tests need a way how to access LB3 artifacts of the LB3 application booted by LB4. I think may be the issue @kevintc has run into.

As a bonus, let's take a look at accessing LB3 models from LB4 app (e.g. from LB4 controllers). In LB3, models are access via app.models.MyModel or MyModel.app.models.AnotherModel. Now the question is how to access the LB3 app object that was booted by LB4? Maybe we need to enhance the booter to bind the LB3 app object to LB4 application context, so that we can use regular dependency injection to access that app object from other LB4 code? Alternatively, it may be better to bind all LB3 models instead of the LB3 app object, so that we can inject only specific LB3 models to LB4 code, in the same way we are injecting only specific LB4 repositories to LB4 controllers.

Acceptance criteria for the spike: investigate the following four tasks and propose a solution for each one. Create follow-up stories to document them and make any necessary runtime improvements.

  1. How to include tests from LB3 app in LB4 test suite. This may be as easy as adding lb3app/tests/**/*.js to mocha arguments. LB3 tests are not expected to pass yet, see the steps below.
  2. How to change acceptance-level tests to use the outer LB4 app as the server to talk to, not the LB3 app. With the proposed changes in place, the acceptance-level tests should pass now.
  3. How to change integration-level tests to accommodate for the new LB4+LB3 and pass.
  4. How to access LB3 models (and datasources?) from LB4 code using dependency injection.

@agnes512
Copy link
Contributor

Thank you so much @bajtos ! The spike is created in #5004.

@dhmlau
Copy link
Member

dhmlau commented May 12, 2020

Spike #5004 is done. The follow up tasks are planned for May milestone: #5301.

@dhmlau
Copy link
Member

dhmlau commented May 12, 2020

Turning this issue as an epic.

@dhmlau
Copy link
Member

dhmlau commented May 20, 2020

All the tasks in this epic are done. Closing this as done!

@dhmlau dhmlau closed this as completed May 20, 2020
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

6 participants