-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6359e65
commit 539ba8e
Showing
9 changed files
with
146 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {SuiteDecoratedMethod} from "./suite.decorator"; | ||
import {TestDecoratedMethod} from "./test.decorator"; | ||
|
||
/** | ||
* Declares a focused test. | ||
* If there are some focused @test(s) or @suite(s), all of them will be run but nothing else. | ||
*/ | ||
export const only = () => function(originalMethod: any, context?: any) { | ||
if ((originalMethod as SuiteDecoratedMethod)?.suiteDecorator) { | ||
originalMethod.suiteDecorator.only = true; | ||
return; | ||
} | ||
|
||
if ((originalMethod as TestDecoratedMethod)?.testDecorator) { | ||
originalMethod.testDecorator.only = true; | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Mock object method. | ||
* Returns cleanup function to restore original method. | ||
* @param target - target object. | ||
* @param targetProperty - name of property from target object. | ||
* @param mock - mock function to be called instead of original one. | ||
*/ | ||
export const mockFn = <T>(target: T, targetProperty: keyof T, mock: (...args: any[]) => void): () => void => { | ||
const originalFn = target[targetProperty]; | ||
|
||
// @ts-ignore | ||
target[targetProperty] = mock; | ||
|
||
return () => { | ||
target[targetProperty] = originalFn; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import playwright, {expect} from "@playwright/test"; | ||
import {suite, test, only} from "../lib"; | ||
import {mockFn} from "./__mocks__/mockFn"; | ||
|
||
playwright.describe('@only decorator', () => { | ||
playwright.describe('with @suite', () => { | ||
const called: string[] = []; | ||
const cleanup = mockFn(playwright.describe, 'only', () => called.push('playwright.describe.only()')); | ||
|
||
playwright.afterAll(() => cleanup()); | ||
|
||
@only() | ||
@suite() | ||
class FocusedSuite { | ||
} | ||
|
||
// Unfortunately, we cannot call real `playwright.describe.only()` because other tests will not be run, so call needs to be mocked. | ||
// As the result, we can only check if mocked `playwright.describe.only` was called. | ||
playwright('@only decorator should run `playwright.describe.only()`', () => { | ||
expect(called).toEqual(["playwright.describe.only()"]); | ||
cleanup(); | ||
}); | ||
}); | ||
|
||
playwright.describe('with @test', () => { | ||
const called: string[] = []; | ||
const cleanup = mockFn(playwright, 'only', (fnName, test) => { | ||
called.push('playwright.only()'); | ||
test(); | ||
}); | ||
|
||
playwright.afterAll(() => cleanup()); | ||
|
||
@suite() | ||
class FocusedSuite { | ||
@only() | ||
@test() | ||
async focusedTest() { | ||
called.push('focusedTest'); | ||
} | ||
|
||
@only() | ||
@test() | ||
async focusedTest2() { | ||
called.push('focusedTest2'); | ||
} | ||
|
||
@test() | ||
async test() { | ||
called.push('test'); | ||
} | ||
} | ||
|
||
// Unfortunately, we cannot call real `playwright.only()` because other tests will not be run, so call needs to be mocked. | ||
// As the result, we can only check if mocked `playwright.only` was called. | ||
playwright('@only decorator should run `playwright.only()` before each decorated test', () => { | ||
expect(called).toEqual(['playwright.only()', 'focusedTest', 'playwright.only()', 'focusedTest2', 'test']); // playwright.only() is called before @only tests: focusedTest & focusedTest2, but not before test | ||
cleanup(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters