Skip to content

Commit

Permalink
fix: fixed regression with using tests and fixture methods (#7491)
Browse files Browse the repository at this point in the history
<!--
Thank you for your contribution.

Before making a PR, please read our contributing guidelines at

https://github.com/DevExpress/testcafe/blob/master/CONTRIBUTING.md#code-contribution

We recommend creating a *draft* PR, so that you can mark it as 'ready
for review' when you are done.
-->

[closes #7482]

## Purpose
_Describe the problem you want to address or the feature you want to
implement._

## Approach
_Describe how your changes address the issue or implement the desired
functionality in as much detail as possible._

## References
#7482

## Pre-Merge TODO
- [ ] Write tests for your proposed changes
- [ ] Make sure that existing tests do not fail
  • Loading branch information
Aleksey28 authored Feb 3, 2023
1 parent f977b30 commit f426301
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
13 changes: 5 additions & 8 deletions src/api/structure/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class Fixture extends TestingUnit {
public globalBeforeFn: Function | null;
public globalAfterFn: Function | null;

public constructor (testFile: TestFile, baseUrl?: string) {
public constructor (testFile: TestFile, baseUrl?: string, returnApiOrigin = true) {
const pageUrl = baseUrl || SPECIAL_BLANK_PAGE;

super(testFile, UnitType.fixture, pageUrl, baseUrl);
Expand All @@ -41,15 +41,12 @@ export default class Fixture extends TestingUnit {
this.globalBeforeFn = null;
this.globalAfterFn = null;

return this.apiOrigin as unknown as Fixture;
if (returnApiOrigin)
return this.apiOrigin as unknown as Fixture;
}

public static init (initOptions: FixtureInitOptions, name: string, ...rest: unknown[]): Fixture | null {
const { testFile, baseUrl } = initOptions;

const fixture = new Fixture(testFile, baseUrl);

return (fixture as unknown as Function)(name, ...rest);
public static init ({ testFile, baseUrl }: FixtureInitOptions): Fixture {
return super.init(Fixture, testFile, baseUrl) as unknown as Fixture;
}

protected _add (name: string, ...rest: unknown[]): Function {
Expand Down
13 changes: 5 additions & 8 deletions src/api/structure/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class Test extends TestingUnit {
private readonly _isCompilerService: boolean;
public readonly esmRuntime: string;

public constructor (testFile: TestFile, isCompilerServiceMode = false, baseUrl?: string) {
public constructor (testFile: TestFile, isCompilerServiceMode = false, baseUrl?: string, returnApiOrigin = true) {
// NOTE: 'fixture' directive can be missing
const fixture = testFile.currentFixture as Fixture;
const pageUrl = fixture?.pageUrl || SPECIAL_BLANK_PAGE;
Expand All @@ -57,15 +57,12 @@ export default class Test extends TestingUnit {
// @ts-ignore
this.esmRuntime = global[ESM_RUNTIME_HOLDER_NAME] || null;

return this.apiOrigin as unknown as Test;
if (returnApiOrigin)
return this.apiOrigin as unknown as Test;
}

public static init (initOptions: TestInitOptions, name: string, fn: Function): Test {
const { testFile, baseUrl, isCompilerServiceMode } = initOptions;

const test = new Test(testFile, isCompilerServiceMode, baseUrl);

return (test as unknown as Function)(name, fn);
public static init ({ testFile, baseUrl, isCompilerServiceMode }: TestInitOptions): Test {
return TestingUnit.init(Test, testFile, isCompilerServiceMode, baseUrl) as unknown as Test;
}

private _initFixture (testFile: TestFile): void {
Expand Down
19 changes: 19 additions & 0 deletions src/api/structure/testing-unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ export default abstract class TestingUnit extends BaseUnit {
//@ts-ignore
ChildClass.API_LIST = TestingUnit.API_LIST.concat(getDelegatedAPIList(ChildClass.prototype));
}

public static init (ChildClass: unknown, ...initProps: unknown[]): TestingUnit {
const fn = (...args: unknown[]) : unknown => {
//@ts-ignore
const apiOrigin = new ChildClass(...initProps) as unknown as Function;

return apiOrigin(...args);
};

const getHandler = (): unknown => {
//@ts-ignore
return new ChildClass(...initProps, false);
};

//@ts-ignore
delegateAPI(fn, ChildClass.API_LIST, { getHandler });

return fn as unknown as TestingUnit;
}
}

// @ts-ignore
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/test-file/add-export-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export default function (testFile: TestFile, exportableLibExports: any, {
baseUrl,
}: OptionalCompilerArguments = {}): void {
Object.defineProperty(exportableLibExports, 'fixture', {
get: () => Fixture.init.bind(Fixture, { testFile, baseUrl }),
get: () => Fixture.init({ testFile, baseUrl }),
configurable: true,
});

Object.defineProperty(exportableLibExports, 'test', {
get: () => Test.init.bind(Test, { testFile, isCompilerServiceMode, baseUrl }),
get: () => Test.init({ testFile, isCompilerServiceMode, baseUrl }),
configurable: true,
});
}
5 changes: 5 additions & 0 deletions test/functional/fixtures/regression/gh-7482/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('[Regression](GH-7482)', () => {
it("Shouldn't throw errors if test and fixtures methods are executed before test and fixtures", async () => {
return runTests('./testcafe-fixtures/index.js', '', { only: ['chrome'] });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fixture.meta('dev', 'true')('Fixture');

test.meta('dev', 'true')('Test', async t => {
await t.expect(true).ok();
});

0 comments on commit f426301

Please sign in to comment.