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

build: Add Store unit tests to Bazel #836

Merged
merged 1 commit into from
Feb 18, 2018

Conversation

MikeRyanDev
Copy link
Member

No description provided.

sh_test(
name = "tautology_test",
srcs = [":true.sh"],
filegroup(
Copy link
Member Author

@MikeRyanDev MikeRyanDev Feb 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't clear to me if I should be using the dependencies installed from running Yarn at the root or if I should do something similar to the ngrx_compile_time_deps for tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have both options.
In the case of tests, your users probably don't expect to build/run these from source. At the same time, your users would download and install the ngrx_compiletime_deps so it's not nice to add test-only dependencies there.
I think I'd probably re-use the node_modules directory installed by yarn (note you can use bazel run @yarn//:yarn to do that with Bazel's version of node and yarn).

Copy link
Member Author

@MikeRyanDev MikeRyanDev Feb 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good deal. This PR follows your suggestion and re-uses the top level node_modules directory for test dependencies.

StoreModule.forRoot<TodoAppSchema>({ todos, todoCount } as any)
);
try {
TestBed.resetTestingModule();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When running these tests via jasmine_node_test the TestBed is not automatically reset.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know much about TestBed, but I would expect jasmine_node_test just evaluates all scripts, and wouldn't do anything special to reset test state - that would be the job of whatever framework you are testing?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me understand what layer was doing this job before?

Copy link
Member Author

@MikeRyanDev MikeRyanDev Feb 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently Angular's core testing library owns resetting the testing module: https://github.com/angular/angular/blob/master/packages/core/testing/src/before_each.ts#L25

My best guess is that the beforeEach hook Angular is calling belongs to a different Jasmine environment then the one my test is being run under.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to confirm that the Jasmine environment I'm working with in the bootstrap script is different from the one being used when running my tests. I wasn't able to add a custom reporter in bootstrap, for instance.


def ts_library(tsconfig = None, node_modules = None, **kwargs):
if not tsconfig:
tsconfig = "//:tsconfig.json"
if not node_modules:
node_modules = "@ngrx_compiletime_deps//:node_modules"
_ts_library(tsconfig = tsconfig, node_modules = node_modules, **kwargs)

def ts_test_library(node_modules = None, **kwargs):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an OK pattern of defining my own rules that just set defaults for existing rules similar to what you've done with ts_library? Or should I be more explicit in BUILD files?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a macro (you can tell because you didn't call the rule factory function) and macros can be the gateway to a bad time. In Google, there are a lot of macros that produce surprising combinations of targets, and make the BUILD files look very different from the result of bazel query --output=build.
This usage is okay though, matching the criteria:

  • it should only wrap one rule, not multiple
  • it should just set defaults

This looks clever and reasonable to me :)

if not node_modules:
node_modules = "//:ngrx_test_dependencies"
if not bootstrap:
bootstrap = ["ngrx/tools/testing/bootstrap_node_tests.js"]
Copy link
Member Author

@MikeRyanDev MikeRyanDev Feb 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It surprised me that I couldn't pass a label here and instead had to point it to a build artifact.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It ought to allow "$(location //my:label)" but doesn't yet :(
bazel-contrib/rules_nodejs#32

@MikeRyanDev MikeRyanDev force-pushed the build/add-store-unit-tests-to-bazel branch from 0c45769 to 976c356 Compare February 17, 2018 21:58
@ngrx ngrx deleted a comment from coveralls Feb 17, 2018

require('zone.js/dist/jasmine-patch.js');

TestBed.initTestEnvironment(ServerTestingModule, platformServerTesting());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe in this spot you could install jasmine lifecycle hooks so that you don't need to worry about resetting the TestBed and whatnot?
I've used https://jasmine.github.io/2.1/custom_reporter.html before (but it's smelly to have a reporter messing with test state... maybe there's another way?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got around the issue for now by overriding Angular's TestBed.configureTestingModule to always reset the testing module before reconfiguring it.

@MikeRyanDev MikeRyanDev force-pushed the build/add-store-unit-tests-to-bazel branch from 976c356 to fc09773 Compare February 18, 2018 15:22
@MikeRyanDev MikeRyanDev changed the title [WIP] build: Add Store unit tests to Bazel build: Add Store unit tests to Bazel Feb 18, 2018
@MikeRyanDev MikeRyanDev force-pushed the build/add-store-unit-tests-to-bazel branch from fc09773 to fc199a8 Compare February 18, 2018 15:30
@brandonroberts brandonroberts merged commit f6d816a into master Feb 18, 2018
@brandonroberts brandonroberts deleted the build/add-store-unit-tests-to-bazel branch February 18, 2018 21:16
@brandonroberts
Copy link
Member

LGTM

@alexeagle
Copy link
Contributor

alexeagle commented Feb 20, 2018 via email

@MikeRyanDev
Copy link
Member Author

@alexeagle I do think the issue with Angular's testing package and Jasmine is a real bug. We have a temporary fix working for us right now.

@alexeagle
Copy link
Contributor

alexeagle commented Feb 20, 2018 via email

sandangel pushed a commit to sandangel/platform that referenced this pull request Mar 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants