vitalets / playwright-bdd

BDD testing with Playwright runner
https://vitalets.github.io/playwright-bdd/
MIT License
250 stars 28 forks source link

Question: How to resolve Playwright Test did not expect test() error #171

Closed kishkk84 closed 4 days ago

kishkk84 commented 1 month ago

I am seeing this error any idea how to resolve it?

Error

> @project/testing@1.0.0 test:e2e
> test_env=local NODE_OPTIONS="--loader=ts-node/esm --experimental-specifier-resolution=node --no-warnings" npx bddgen && npx playwright test -c playwright.config.ts

Error: Playwright Test did not expect test() to be called here.
Most common reasons include:
- You are calling test() in a configuration file.
- You are calling test() in a file that is imported by the configuration file.
- You have two different versions of @playwright/test. This usually happens
  when one of the dependencies in your package.json depends on @playwright/test.

   at tests/integration/main-navigation.spec.tsx:3

  1 | import { test, expect } from "../../fixtures/base.fixture.ts";
  2 |
> 3 | test(
    | ^
  4 |   "Login into website",
  5 |   { tag: ["@chrome", "@firefox"] },
  6 |   async ({ mainNavigationComponent, receptionPage }) => {

    at TestTypeImpl._currentSuite (/Users/kisj/Projects/mer/node_modules/@playwright/test/node_modules/playwright/lib/common/testType.js:71:13)
    at TestTypeImpl._createTest (/Users/kisj/Projects/mer/node_modules/@playwright/test/node_modules/playwright/lib/common/testType.js:80:24)
    at test (/Users/kisj/Projects/mer/node_modules/@playwright/test/node_modules/playwright/lib/transform/transform.js:256:12)
    at /Users/kisj/Projects/merpackages/testing/tests/integration/main-navigation.spec.tsx:3:1
"devDependencies": {
    "@cucumber/cucumber": "^10.8.0",
    "@playwright/experimental-ct-react": "^1.44.0",
    "@playwright/test": "^1.44.0",
    "@types/node": "^20.12.7",
    "dotenv": "^16.4.5",
    "eslint": "^8.55.0",
    "monocart-reporter": "^2.4.6",
    "playwright-bdd": "^6.6.0",
    "prettier": "3.3.2",
    "ts-node": "^10.9.2"
  }

base.fixture.ts

import { mergeTests } from "@playwright/test";
import { test as baseTest } from "./base-test.fixture.ts";
import { test as page } from "./page.fixture.ts";
import { test as bdd, createBdd } from "playwright-bdd";

export const test = mergeTests(baseTest, page, bdd);
export const expect = test.expect;
export const { Given, When, Then } = createBdd(test);

Playwright.config.ts

projects: [
    {
      name: "Integration Tests",
      testDir: "./tests/integration",
      //testMatch: /.*.spec.tsx/,
    },
    {
      name: "e2e BDD Tests",
      //testMatch: [/.*.step.tsx/, /.*.spec.js/],
      testDir: defineBddConfig({
        paths: ["./tests/e2e/features/"],
        importTestFrom: "./fixtures/base.fixture",
        require: ["./tests/e2e/steps/*.tsx"],
      }),
    },
    {
      name: "setup",
      testMatch: /.*\.setup\.tsx/,
    },
    {
      name: "chromium",
      grep: [/@all/, /@chrome/],
      use: {
        ...devices["Desktop Chrome"],
        deviceScaleFactor: undefined,
        viewport: null,
        storageState: path.join(
          __dirname,
          "./authentication/storageState.json",
        ),
      },
      dependencies: ["setup"],
    },]
vitalets commented 3 weeks ago

@kishkk84

samixchoumi commented 4 days ago

Hello @vitalets I've the same issue after upgrading playwright to "playwright": "^1.45.3", and after upgrading playwright-bdd to "playwright-bdd": "^7.1.0",

I changed only this :

const testDir = defineBddConfig({
  tags: 'not @ignore',
  features: 'tests/*/*.feature',
  steps: 'tests/*/*/*.ts'
});

and I just tried to launch npx bddgen && npx playwright test --ui

My package.json looks like that now :

"devDependencies": {
    "@faker-js/faker": "^8.4.1",
    "@types/node": "^22.0.0",
    "dotenv": "^16.4.5",
    "fs-extra": "^11.2.0",
    "moment": "^2.30.1",
    "playwright": "^1.45.3",
    "playwright-bdd": "^7.1.0",
    "playwright-slack-report": "^1.1.81",
    "reflect-metadata": "^0.2.2",
    "ts-node": "^10.9.2"
  }

My playwright config looks like that :

import { defineConfig, devices } from '@playwright/test';
import dotenv from 'dotenv';
import { defineBddConfig } from 'playwright-bdd';

dotenv.config();

const testDir = defineBddConfig({
  tags: 'not @ignore',
  features: 'tests/*/*.feature',
  steps: 'tests/*/*/*.ts'
});

/**
 * See https://playwright.dev/docs/test-configuration.
 */
export default defineConfig({
  testDir,
  /* Run tests in files in parallel */
  fullyParallel: true,
  /* Fail the build on CI if you accidentally left test.only in the source code. */
  forbidOnly: !!process.env.CI,
  /* Retry on CI only */
  retries: process.env.CI ? 2 : 0,
  /* Opt out of parallel tests on CI. */
  workers: process.env.CI ? 4 : 5,
  /* Reporter to use. See https://playwright.dev/docs/test-reporters */
  reporter: [
    [
      './node_modules/playwright-slack-report/dist/src/SlackReporter.js',
      {
        sendResults: 'off'
      }
    ],
    ['blob'], ['list']
  ],
  /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
  use: {
    baseURL: 'https://someUrl.com',
    trace: 'off',
    navigationTimeout: 60000,
    actionTimeout: 10000,
    screenshot: 'only-on-failure'
  },
  expect: {
    timeout: 30000
  },
  timeout: 480000,
  /* Configure projects for major browsers */
  projects: [
    {
      name: 'setup',
      testMatch: /.*\.setup\.ts/,
      testDir: './setup'
    },
    {
      name: 'chrome_pre-prod',
      use: {
        ...devices['Desktop Chrome'],
        contextOptions: {
          screen: {
            width: 1920,
            height: 1024
          }
        },
        viewport: {
          width: 1920,
          height: 1024
        },
        storageState: '.auth/storageState.json'
      },
      dependencies: ['setup']
    }
  ]
});

Is it enough for you ?

Edit : I'm pretty sure it's not linked to playwright-bdd as when downgrading playwright version to 1.44.0 it works fine.

samixchoumi commented 4 days ago

My error :

Error: Playwright Test did not expect test() to be called here.
Most common reasons include:
- You are calling test() in a configuration file.
- You are calling test() in a file that is imported by the configuration file.
- You have two different versions of @playwright/test. This usually happens
  when one of the dependencies in your package.json depends on @playwright/test.
    at TestTypeImpl._currentSuite (/Users/IdeaProjects/poc-playwright/node_modules/@playwright/test/node_modules/playwright/lib/common/testType.js:71:13)
    at TestTypeImpl._createTest (/Users/IdeaProjects/poc-playwright/node_modules/@playwright/test/node_modules/playwright/lib/common/testType.js:80:24)
    at /Users/IdeaProjects/poc-playwright/node_modules/@playwright/test/node_modules/playwright/lib/transform/transform.js:256:12
    at Object.<anonymous> (/Users/IdeaProjects/poc-playwright/setup/auth.setup.ts:6:6)

And my auth.setup.ts file :

import { test as setup } from '@playwright/test';
import { utils } from '../model/Utils/utils';

const authFile = '.auth/storageState.json';

setup('authenticate', async ({ page }) => {
  const utils = new utils(page);
  await utils.authenticate();
  await utils.assertNoPopUpPresent();
  await page.context().storageState({ path: authFile });
});

Another one base on .features-gen folder :

Error: Playwright Test did not expect test.describe() to be called here.
Most common reasons include:
- You are calling test.describe() in a configuration file.
- You are calling test.describe() in a file that is imported by the configuration file.
- You have two different versions of @playwright/test. This usually happens
  when one of the dependencies in your package.json depends on @playwright/test.
    at TestTypeImpl._currentSuite (/Users/IdeaProjects/poc-playwright/node_modules/@playwright/test/node_modules/playwright/lib/common/testType.js:71:13)
    at TestTypeImpl._describe (/Users/IdeaProjects/poc-playwright/node_modules/@playwright/test/node_modules/playwright/lib/common/testType.js:104:24)
    at Function.describe (/Users/IdeaProjects/poc-playwright/node_modules/@playwright/test/node_modules/playwright/lib/transform/transform.js:256:12)
    at Object.<anonymous> (/Users/IdeaProjects/poc-playwright/.features-gen/tests/Test/Test.feature.spec.js:4:6)
/** Generated from: tests/Test/Test.feature */
import { test } from "playwright-bdd";
test.describe("Test Test functionality", () => {
  test("A test", async ({ Given, page, When, Then }) => {
    await Given("I am logged in", null, { page });
    await When("I am in \"Test\" section", null, { page });
    await Then("all created tests should be displayed in a table list", null, { page });
  });

  test.describe("A small Test <number>", () => {
    test("A small Test 1", async ({ Given, page, When, And, Then }) => {
      await Given("I am logged in", null, { page });
      await When("I am in \"Test\" section", null, { page });
      await And("I types a search by \"name\" on the search field", null, { page });
      await Then("the list should be auto-refreshed based on the search request", null, { page });
    });

    test("A small Test 2", async ({ Given, page, When, And, Then }) => {
      await Given("I am logged in", null, { page });
      await When("I am in \"Test\" section", null, { page });
      await And("I types a search by \"url\" on the search field", null, { page });
      await Then("the list should be auto-refreshed based on the search request", null, { page });
    });
  });
});

// == technical section ==

test.use({
  $test: ({}, use) => use(test),
  $uri: ({}, use) => use("tests/Test/Test.feature"),
  $bddFileMeta: ({}, use) => use(bddFileMeta),
});

const bddFileMeta = {
  "A test": {"pickleLocation":"3:3"},
  "A small Test <number>|A small Test 1": {"pickleLocation":"16:7"},
  "A small Test <number>|A small Test 2": {"pickleLocation":"17:7"},
};
vitalets commented 4 days ago

I see in devDependencies "playwright": "^1.45.3", not @playwright/test. Could you try the following:

npm uninstall playwright
npm install -D @playwright/test
samixchoumi commented 4 days ago

Hello @vitalets it seem to works better ! But for my own knowledge, what is the difference? (Given that it worked in version 1.44)

vitalets commented 4 days ago

playwright package is just a browser manipulation library, while @playwright/test is playwright + test runner. Here are more details on that: https://playwright.dev/docs/library