thenbe / neotest-playwright

A playwright adapter for neotest.
MIT License
43 stars 5 forks source link

Feature request: support project dependencies #28

Closed catgoose closed 5 months ago

catgoose commented 6 months ago

Using Neovim 0.10 and playwright 1.44.1

I select 'chromium' using NeotestPlaywrightProject then try to run playwright tests:

image image

My lazy.nvim config:

return {
  "nvim-neotest/neotest",
  dependencies = {
    "nvim-neotest/nvim-nio",
    "nvim-lua/plenary.nvim",
    "antoinemadec/FixCursorHold.nvim",
    "nvim-treesitter/nvim-treesitter",
    "marilari88/neotest-vitest",
    "thenbe/neotest-playwright",
  },
  config = function()
    require("neotest").setup({
      adapters = {
        require("neotest-vitest")({
          filter_dir = function(name)
            return name ~= "node_modules" and name ~= "e2e"
          end,
        }),
        require("neotest-playwright").adapter({
          options = {
            persist_project_selection = true,
            enable_dynamic_test_discovery = true,
            is_test_file = function(file_path)
              return string.match(file_path, "e2e/tests")
            end,
          },
        }),
      },
      consumers = {
        playwright = require("neotest-playwright.consumers").consumers,
      },
    })
  end,
  cmd = {
    "Neotest",
    "NeotestPlaywrightProject",
    "NeotestPlaywrightPreset",
    "NeotestPlaywrightRefresh",
  },
  keys = {
    m("<leader>m", "Neotest summary"),
    m("<leader>n", "Neotest run file"),
  },
}

My playwright.config.ts

...
  reporter: ci
    ? [['list', { printSteps: true }]]
    : [
        ['html', { outputFolder: 'e2e/report' }],
        ['json', { outputFile: 'e2e/test-results.json' }],
      ],
  use: {
    actionTimeout: 0,
    baseURL,
    trace: 'on-first-retry',
    headless: true,
  },
  outputDir: 'e2e/test-results',
  projects: [
    {
      name: 'setup',
      testMatch: msalEnabled ? /.*\.setup\.ts/ : undefined,
    },
    {
      name: 'chromium',
      use: {
        ...devices['Desktop Chrome'],
        storageState: 'e2e/.auth/user.json',
      },
      dependencies,
    },
    {
      name: 'firefox',
      use: {
        ...devices['Desktop Firefox'],
        storageState: 'e2e/.auth/user.json',
      },
      dependencies,
    },
    {
      name: 'webkit',
      use: {
        ...devices['Desktop Safari'],
        storageState: 'e2e/.auth/user.json',
      },
      dependencies,
    },
  ],
...
catgoose commented 6 months ago

Similar to https://github.com/thenbe/neotest-playwright/issues/21 but I can never get them to pass. I am not modifying the test file.

thenbe commented 6 months ago

I wonder if this due to the latest version. Can you reproduce this issue on an older version of playwright (like 1.41.2)?

catgoose commented 5 months ago

I've tried 1.41, 1.40, 1.39 and same issue. It doesn't seem to be updating workspace diagnostics at all. If I add something to a test like:

expect(false).toBeTruthy();

I can view the output of the test, but it does not show diagnostics for that line:

image

As a comparison here is vitest:

image

thenbe commented 5 months ago

I'm having difficulty reproducing this.

Btw, you should update your config. it_test_file should be more strict to avoid slurping up unrelated files (e.g. .png from a snapshot test). In your example, it should be something like:

local is_test_file = function(file_path)
    local result = file_path:find('e2e/tests/.*%.test%.[jt]s$') ~= nil
    return result
end

See 985ec2649831 for more examples.

catgoose commented 5 months ago

Thanks, I added that to my config.

Does neotest-playwright get back the results of a test with the json reporter? Does this have anything to do with the reporter setup in the playwright config? It seems like it's using the list reporter for the output floats in my screenshots.

Here is my full playwright config:

import { defineConfig, devices } from '@playwright/test';
import { config } from 'dotenv';

config();

const baseURL = process.env.PLAYWRIGHT_BASE_URL || 'http://localhost:7373';
const msalEnabled = process.env.MSAL_AUTH_ENABLED === 'true';
const dependencies = msalEnabled ? ['setup'] : [];
const ci = process.env.CI === 'true';

export default defineConfig({
  testDir: './e2e/tests',
  fullyParallel: true,
  timeout: 30 * 1000,
  expect: {
    timeout: 5000,
  },
  forbidOnly: ci,
  retries: ci ? 2 : 0,
  workers: ci ? 1 : undefined,
  reporter: ci
    ? [['list', { printSteps: true }]]
    : [
        ['list', { printSteps: true }],
        ['html', { outputFolder: 'e2e/report', open: 'never' }],
        ['json', { outputFile: 'e2e/test-results.json' }],
      ],
  use: {
    actionTimeout: 0,
    baseURL,
    trace: 'on-first-retry',
    headless: true,
  },
  outputDir: 'e2e/test-results',
  projects: [
    {
      name: 'setup',
      testMatch: msalEnabled ? /.*\.setup\.ts/ : undefined,
    },
    {
      name: 'chromium',
      use: {
        ...devices['Desktop Chrome'],
        storageState: 'e2e/.auth/user.json',
      },
      dependencies,
    },
    {
      name: 'firefox',
      use: {
        ...devices['Desktop Firefox'],
        storageState: 'e2e/.auth/user.json',
      },
      dependencies,
    },
    {
      name: 'webkit',
      use: {
        ...devices['Desktop Safari'],
        storageState: 'e2e/.auth/user.json',
      },
      dependencies,
    },
  ],
  webServer: {
    command: 'npm run dev-ci',
    url: baseURL,
    reuseExistingServer: true,
  },
});
thenbe commented 5 months ago

That might be the cause actually. Apparently I started working on a solution in this PR but never got around to finishing it up.

See this line in particular: https://github.com/thenbe/neotest-playwright/blob/e2ab01cc4fe40f2d71b0860480b65116c1170096/README.md?plain=1#L72

If you un-declare the outputFile of the json reporter, is the issue fixed?

-        ['json', { outputFile: 'e2e/test-results.json' }],
+        ['json'],

What about if you remove the json reporter entirely?

catgoose commented 5 months ago

The problem has to do with the dependencies on each project:

    {
      name: 'setup',
      testMatch: msalEnabled ? /.*\.setup\.ts/ : undefined,
    },
    {
      name: 'chromium',
      use: {
        ...devices['Desktop Chrome'],
        storageState: 'e2e/.auth/user.json',
      },
      dependencies: 'setup',
    },

The auth step logs in the user, so I think when that step is completed neotest-playright thinks that the test was successful. If I disable the dependency, I can pass the test:

image

catgoose commented 5 months ago

Do you know if neotest-playwright can handle dependency projects or should I disable my dependencies temporarily?

thenbe commented 5 months ago

Honestly, I never got around to using dependency projects so I can't say for sure. But given the issue you're facing, some changes might be required in neotest-playwright in order to report the correct results when dependency projects are used.

catgoose commented 5 months ago

I've decided to enable the setup dependency only in CI environment. If I need get auth cookies, I just run the setup project manually to save auth state to user.json.