Remy
Remy

Reputation: 11

TestCafe "fixture is not defined" error when importing fixture and tests from multiple files into main.js file

I am unable to import tests and a fixture into one main.ts file to run all my tests at once. For organization purposes, I have my tests organized into separate folders. I am getting the error:

ERROR Cannot prepare tests due to an error.
ReferenceError: fixture is not defined

I've created a main.ts file to run all my tests. I import Fixture(), Test1() and Test2().

main.ts

import { Fixture } from "../utils/envUtils";
import { Test1 } from "./testSet1/test1";
import { Test2 } from "./testSet2/test2";

Fixture();
Test1();
Test2();

Fixture.ts

export const Fixture = () => {
    fixture("Running tests")
}

Test1.ts

export function Test1() {
    test("Test1", async (t: TestController) => {
         ...
    }
}

Test2.ts

export function Test2() {
    test("Test2", async (t: TestController) => {
         ...
    }
}

If I manually copy and paste the Test1() and Test2() code into the main.ts file, it works. Any help is appreciated. Thanks!

Upvotes: 1

Views: 3109

Answers (2)

JESii
JESii

Reputation: 4937

In case anyone else is running into this problem, the solution is to add the eslint-plugin-testcafe module to your eslint config.

This is referenced in the TestCafe docs here: https://testcafe.io/documentation/402831/guides/basic-guides/organize-tests like this:

Note
If you use eslint in your project, install the TestCafe plugin to
avoid the 'fixture' is not defined and 'test' is not
defined errors.

yarn add -D eslint-plugin-testcafe and update your .eslintrc.json with the following settings:

{
  "plugins": [
    "testcafe"
  ],
  "extends": "plugin:testcafe/recommended"
}

Upvotes: 5

Alex Skorkin
Alex Skorkin

Reputation: 4274

Your 'Fixture' import statement does not look correct. Your 'test' functions are incomplete, they miss closing braces. You also need to install TestCafe locally to your test project.

After you fix these issues, test cases will run correctly:

enter image description here

I attached a complete fixed copy of your tests - this copy runs properly: https://www.dropbox.com/s/pgwp73kf46xuuzm/TypeScriptExample.zip?dl=0

Upvotes: 4

Related Questions