Skip to content

Commit

Permalink
mark test as exclusive run
Browse files Browse the repository at this point in the history
  • Loading branch information
beluamat29 committed Sep 14, 2024
1 parent 76a07d5 commit 43503e1
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/core/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ export class Test {
#body;
#callbacks;
#result;
#isMarkedAsExclusiveForRun;

constructor(name, body, callbacks) {
this.#initializeName(name);
this.#initializeBody(body);
this.#callbacks = callbacks;
this.#result = undefined;
this.#isMarkedAsExclusiveForRun = false;
}

// Execution
Expand Down Expand Up @@ -62,6 +64,14 @@ export class Test {
this.setResult(explicitlySkippedResult);
}

only() {
this.#isMarkedAsExclusiveForRun = true;
}

isMarkedAsExclusiveForRun() {
return this.#isMarkedAsExclusiveForRun;
}

setResult(result) {
if (this.hasNoResult() || this.isSuccess()) {
this.#result = result;
Expand Down
20 changes: 20 additions & 0 deletions lib/core/test_suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@ export class TestSuite {

#evaluateSuiteDefinition() {
this.#body.call();
this.#evaluateExclusiveRunsMarks();
}

#evaluateExclusiveRunsMarks() {
const testsMarkedAsOnlyCount = this.#getTestsMarkedAsExclusive();
if(testsMarkedAsOnlyCount > 0) {

Check failure on line 216 in lib/core/test_suite.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected space(s) after "if"

Check failure on line 216 in lib/core/test_suite.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected space(s) after "if"
this.#skipTestsNotMarkedAsExclusive();
}
}

#getTestsMarkedAsExclusive() {
return this.tests().filter(test => test.isMarkedAsExclusiveForRun()).length;
}

#skipTestsNotMarkedAsExclusive() {
this.tests().forEach(test => {
if(!test.isMarkedAsExclusiveForRun()) {

Check failure on line 227 in lib/core/test_suite.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected space(s) after "if"

Check failure on line 227 in lib/core/test_suite.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected space(s) after "if"
test.skip();
}
})

Check failure on line 230 in lib/core/test_suite.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing semicolon

Check failure on line 230 in lib/core/test_suite.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing semicolon
}

async #runTests(context) {
Expand Down
54 changes: 54 additions & 0 deletions tests/core/test_suite_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,58 @@ suite('test suite behavior', () => {

assert.that(count).isEqualTo(0);
});

test('a suite that has tests marked as only runs exclusively those tests and skips the rest', async () => {

Check failure on line 246 in tests/core/test_suite_test.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected space before function parentheses

Check failure on line 246 in tests/core/test_suite_test.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected space before function parentheses
mySuite.addTest(passingTest);
mySuite.addTest(failingTest);
mySuite.addTest(erroredTest);

passingTest.only();
failingTest.only();

await runner.run();

assert.that(erroredTest.isExplicitlySkipped()).isTrue();

assert.that(mySuite.totalCount()).isEqualTo(3);
assert.that(mySuite.pendingCount()).isEqualTo(0);
assert.that(mySuite.successCount()).isEqualTo(1);
assert.that(mySuite.failuresCount()).isEqualTo(1);
assert.that(mySuite.errorsCount()).isEqualTo(0);
assert.that(mySuite.skippedCount()).isEqualTo(1);
})

Check failure on line 264 in tests/core/test_suite_test.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing semicolon

Check failure on line 264 in tests/core/test_suite_test.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing semicolon

test('a suite that has tests marked as only runs before hooks exclusively for those tests', async () => {

Check failure on line 266 in tests/core/test_suite_test.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected space before function parentheses

Check failure on line 266 in tests/core/test_suite_test.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected space before function parentheses
let count = 0;

mySuite.before(() => {
count += 1;
});

mySuite.addTest(passingTest);
mySuite.addTest(failingTest);

passingTest.only();

await runner.run();

assert.that(count).isEqualTo(1);
});

test('a suite that has tests marked as only runs after hooks exclusively for those tests', async () => {

Check failure on line 283 in tests/core/test_suite_test.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected space before function parentheses

Check failure on line 283 in tests/core/test_suite_test.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected space before function parentheses
let count = 0;

mySuite.after(() => {
count += 1;
});

mySuite.addTest(passingTest);
mySuite.addTest(failingTest);

passingTest.only();

await runner.run();

assert.that(count).isEqualTo(1);
});
});
36 changes: 36 additions & 0 deletions tests/core/test_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,40 @@ suite('tests behavior', () => {
assert.that(calls).isEqualTo(1);
});
});

test('a successful test marked as only runs normally', async () => {

Check failure on line 150 in tests/core/test_test.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected space before function parentheses

Check failure on line 150 in tests/core/test_test.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected space before function parentheses
await withRunner(async(runner, asserter) => {
let calls = 0;

const testToBeMarkedAsOnly = aPassingTest(
asserter, { whenSuccess: () => {
calls += 1;
} });

testToBeMarkedAsOnly.only();

const result = await resultOfASuiteWith(runner, testToBeMarkedAsOnly);

assert.isTrue(result.isSuccess());
assert.that(calls).isEqualTo(1);
});
})

Check failure on line 166 in tests/core/test_test.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing semicolon

Check failure on line 166 in tests/core/test_test.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing semicolon

test('a failed test marked as only runs normally', async () => {

Check failure on line 168 in tests/core/test_test.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected space before function parentheses

Check failure on line 168 in tests/core/test_test.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected space before function parentheses
await withRunner(async(runner, asserter) => {
let calls = 0;

const testToBeMarkedAsOnly = aFailingTest(
asserter, { whenFailed: () => {
calls += 1;
} });

testToBeMarkedAsOnly.only();

const result = await resultOfASuiteWith(runner, testToBeMarkedAsOnly);

assert.isTrue(result.isFailure());
assert.that(calls).isEqualTo(1);
});
})
});

0 comments on commit 43503e1

Please sign in to comment.