Skip to content

Unit tests run with --bundle does not bundle 3rd party libraries #4477

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

Closed
cowboyd opened this issue Mar 27, 2019 · 2 comments
Closed

Unit tests run with --bundle does not bundle 3rd party libraries #4477

cowboyd opened this issue Mar 27, 2019 · 2 comments
Assignees
Milestone

Comments

@cowboyd
Copy link

cowboyd commented Mar 27, 2019

Environment
✔ Getting NativeScript components versions information...
✔ Component nativescript has 5.4.0-2019-03-21-13075 version and is up to date.
✔ Component tns-core-modules has 5.3.0 version and is up to date.
✔ Component tns-android has 5.3.0 version and is up to date.
✔ Component tns-ios has 5.3.0 version and is up to date.

Describe the bug
Webpack test bundle does not include 3rd party libraries. We're trying to unit test some of our library code that uses the @microstates/union. This works when the app is built, but when we try to run the unit tests, they fail with the error:

TypeError: undefined is not an object (evaluating 'modules[moduleId].call')

Which indicates that the microstate library is not bundled at all. Manually inspecting the built vendor.js, and bundle.js confirms that this is the case.

To Reproduce

  1. import a 3rd party library from within a unit test case. You don't even have to try and use it, just log it to the console.
  2. run yarn test ios --bundle

Expected behavior

  1. it resolves the module and dependencies from the node_modules directory and includes it in your application's vendor.js bundle.
  2. in this case, it should have the microstates and @microstates/union modules since they are being imported by our bluetooth library.

Sample project

@Fatme
Copy link
Contributor

Fatme commented Mar 27, 2019

@cowboyd,

Yes, it doesn't work :(

I've just checked it and found that the problem is here https://github.com/NativeScript/nativescript-dev-webpack/blob/master/unit-testing-config-loader.js#L5.
Actually nativescript-dev-webpack collects the dependencies only from tests/**/.js files. As your test is .ts file, it is not required so its dependencies are not collected. This is the reason why @microstates is not included in vendor.js.

I tried to manually change the above mentioned code from nativescript-dev-webpack:

`const testFilesRegExp = /tests\/.*\.ts/;`

After that I ran tns test ios --bundle and received the following output:

CONSOLE LOG Bluetooth =  [object Module]
CONSOLE LOG file:///app/vendor.js:2797:24: NSUTR: beginning test run
NativeScript / 10.3.1 (10.3.1; iPhone): Executed 0 of 1 SUCCESS (0 secs / 0 secs)
NativeScript / 10.3.1 (10.3.1; iPhone) Bluetooth Service starts out as being off FAILED
	undefined is not an object (evaluating 'bluetooth.isOff')

As you can see the bluetooth object is populated now - CONSOLE LOG Bluetooth = [object Module] but the test still doesn't pass. I assume this is expected as bluetooth.isOff is not exported from bluetooth.ts file. (correct me if I'm mistaken here)

I've played a little bit more with the code and was able to successfully run the tests with the following modifications:

bluetooth.test.ts

import * as bluetooth from '../bluetooth';

console.log('Bluetooth = ', bluetooth);

describe('Bluetooth Service', function () {
  it('starts out as being off', () => {
    const powerOn = bluetooth.ble.onPowerOn();
    expect(powerOn).to.equal(10);
  })
});

bluetooth.ts

import { Store, create } from 'microstates';
import State from "./bluetooth/bluetooth";

class BLE {
  onPowerOn() {
    return 10;
  }

  onPowerOff() {

  }
}

export const ble = new BLE();

export function Bluetooth(fn: (State) => any) {
  return Store(create(State), fn);
}

@cowboyd
Copy link
Author

cowboyd commented Mar 27, 2019

That's seems to fix it.

Don't worry so much about the imports, we were just fooling around with them to see if there was something that we had to change, I changed them back to the way they were, and we can now use the module as expected. In the meantime, we can try to fix it or vendor it.

@Fatme Fatme self-assigned this Mar 29, 2019
@Fatme Fatme added the bug label Mar 29, 2019
@Fatme Fatme added this to the 5.3.1 milestone Mar 29, 2019
Fatme added a commit to NativeScript/nativescript-dev-webpack that referenced this issue Mar 29, 2019
Fatme pushed a commit to NativeScript/nativescript-dev-webpack that referenced this issue Mar 29, 2019
@rosen-vladimirov rosen-vladimirov modified the milestones: 5.3.1, 5.3.2 Apr 3, 2019
Fatme pushed a commit to NativeScript/nativescript-dev-webpack that referenced this issue Apr 3, 2019
Fatme pushed a commit to NativeScript/nativescript-dev-webpack that referenced this issue Apr 5, 2019
* release: cut the 0.21.0 release

* fix: add support for ts files on test command when `--bundle` is provided (#848)

Rel to:
NativeScript/nativescript-cli#4477
NativeScript/nativescript-cli#1798
Fatme pushed a commit to NativeScript/nativescript-dev-webpack that referenced this issue Apr 8, 2019
* release: cut the 0.21.0 release

* fix: add support for ts files on test command when `--bundle` is provided (#848)

Rel to:
NativeScript/nativescript-cli#4477
NativeScript/nativescript-cli#1798

* fix: use correct slashes on windows (#851)
Fatme pushed a commit to NativeScript/nativescript-dev-webpack that referenced this issue Apr 12, 2019
* release: cut the 0.21.0 release

* fix: add support for ts files on test command when `--bundle` is provided (#848)

Rel to:
NativeScript/nativescript-cli#4477
NativeScript/nativescript-cli#1798

* fix: use correct slashes on windows (#851)

* fix: fix "ERROR in Must have a source file to refactor." error from ngCompilerPlugin on `test` command (#859)
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

5 participants