Skip to content

Commit

Permalink
fix jasmine_node_test with sharding doesn't fail
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Mar 27, 2019
1 parent ef6ea7b commit 428e53f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
38 changes: 28 additions & 10 deletions packages/jasmine/src/jasmine_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,34 @@ function main(args) {
});

if (TOTAL_SHARDS) {
// Partition the specs among the shards.
// This ensures that the specs are evenly divided over the shards.
// Also it keeps specs in the same order and prefers to keep specs grouped together.
// This way, common beforeEach/beforeAll setup steps aren't repeated as much over different
// shards.
const allSpecs = getAllSpecs(jasmine.getEnv());
const start = allSpecs.length * SHARD_INDEX / TOTAL_SHARDS;
const end = allSpecs.length * (SHARD_INDEX + 1) / TOTAL_SHARDS;
const enabledSpecs = allSpecs.slice(start, end);
jasmine.getEnv().configure({specFilter: (s) => enabledSpecs.includes(s.id)});
// Since we want to collect all the loaded specs, we have to do this after
// loadSpecs() in jasmine/lib/jasmine.js
// However, we must add our filter before the runnable specs are calculated
// so that our filtering is applied.
// The jasmineStarted() callback is called by the "inner" execute function
// in jasmine-core, which is too late.
// Patch the inner execute function to do our filtering first.
const env = jasmine.getEnv();
const originalExecute = env.execute.bind(env);
env.execute = () => {
const allSpecs = getAllSpecs(env);
// Partition the specs among the shards.
// This ensures that the specs are evenly divided over the shards.
// Also it keeps specs in the same order and prefers to keep specs grouped together.
// This way, common beforeEach/beforeAll setup steps aren't repeated as much over different
// shards.
const start = allSpecs.length * SHARD_INDEX / TOTAL_SHARDS;
const end = allSpecs.length * (SHARD_INDEX + 1) / TOTAL_SHARDS;
const enabledSpecs = allSpecs.slice(start, end);
env.configure({specFilter: (s) => enabledSpecs.includes(s.id)});
originalExecute();
};
// Special case!
// To allow us to test sharding, always run the specs in the order they are declared
if (process.env['TEST_WORKSPACE'] === 'npm_bazel_jasmine' &&
process.env['BAZEL_TARGET'] === '//test:sharding_test') {
jrunner.randomizeTests(false);
}
}

jrunner.execute();
Expand Down
19 changes: 15 additions & 4 deletions packages/jasmine/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,24 @@ jasmine_node_test(
deps = ["//:jasmine_runner"],
)

jasmine_node_test(
name = "failing_sharding_test",
srcs = ["failing_sharded_test.js"],
expected_exit_code = 3,
jasmine = "@npm//jasmine",
shard_count = 2,
deps = ["//:jasmine_runner"],
)

jasmine_node_test(
name = "filtering_test",
srcs = ["filtering_test.js"],
jasmine = "@npm//jasmine",
# Only run this test when explicitly included in the target patterns
# It will fail because usage of `fit` and `fdescribe` cause Jasmine
# This test will fail because usage of `fit` and `fdescribe` cause Jasmine
# to return a 'incomplete' status
tags = ["manual"],
# TODO(alexeagle): find a way to assert that the right things were filtered
# maybe sniff the stdout for Ran 1 of 3 specs
# or change the exit code for Jasmine 'incomplete' status
expected_exit_code = 3,
jasmine = "@npm//jasmine",
deps = ["//:jasmine_runner"],
)
8 changes: 8 additions & 0 deletions packages/jasmine/test/failing_sharded_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
describe('test failures with sharding', () => {
it('should fail', () => {
expect(false).toBeTruthy();
});
it('should fail', () => {
expect(false).toBeTruthy();
});
});

0 comments on commit 428e53f

Please sign in to comment.