nvim-neotest / neotest-jest

MIT License
124 stars 80 forks source link

Neotest-jest not working with describe.each #124

Open tweiss777 opened 3 months ago

tweiss777 commented 3 months ago

I'm trying to run some unit tests using neotest-jest, and I can't see the test results for some reason when using describe.each.

When running a test, such as:

import { sum } from '../src/AlgorithmsInTypescript/math'

describe('test sum module',() =>{
   test('1 + 2 should equal 3',()=> {
    const [a,b] = [1,2]
    expect(sum(a,b)).toBe(3)
  })
})

I can see the tests running fine.

Screenshot 2024-07-31 at 15 54 17

However, if I run the following test:

import groupAnagrams from '../src/AlgorithmsInTypescript/GroupAnagrams'

const testCases = [
  {
    input: ['abc', 'cba', 'bca', 'xyz', 'yxz', 'zxy'],
    output: [
      ['abc', 'cba', 'bca'],
      ['xyz', 'yxz', 'zxy'],
    ],
  },
  {
    input: ['rat', 'tar', 'art', 'star', 'tars', 'rats'],
    output: [
      ['rat', 'tar', 'art'],
      ['star', 'tars', 'rats'],
    ],
  },
  {
    input: ['no', 'on', 'is', 'si', 'at', 'ta'],
    output: [
      ['no', 'on'],
      ['is', 'si'],
      ['at', 'ta'],
    ],
  },
  {
    input: ['bored', 'robed', 'debit card', 'bad credit', 'earth', 'heart'],
    output: [
      ['bored', 'robed'],
      ['debit card', 'bad credit'],
      ['earth', 'heart'],
    ],
  },
  {
    input: [''],
    output: [['']],
  },
  {
    input: ['a', 'b', 'c', 'a', 'b', 'c'],
    output: [
      ['a', 'a'],
      ['b', 'b'],
      ['c', 'c'],
    ],
  },
  {
    input: ['apple', 'paple', 'pale', 'leap', 'peal'],
    output: [
      ['apple', 'paple'],
      ['pale', 'leap', 'peal'],
    ],
  },
]

describe.each(testCases)('Test group anagrams', ({ input, output }) => {
  it(`The input: ${input} should return ${output}`, () => {
    const result = groupAnagrams(input)
    const resultGroups = result.map((group) => new Set(group))
    const expectedGroups = output.map((group) => new Set(group))

    expectedGroups.forEach((expectedGroup) => {
      expect(resultGroups).toEqual(
        expect.arrayContaining([
          expect.objectContaining({
            size: expectedGroup.size,
            ...expectedGroup,
          }),
        ]),
      )
    })

    resultGroups.forEach((resultGroup) => {
      expect(expectedGroups).toEqual(
        expect.arrayContaining([
          expect.objectContaining({ size: resultGroup.size, ...resultGroup }),
        ]),
      )
    })
  })
})

I don't see the result if I run it via Neo-Test Jest, and it gives me a blue icon in the test explorer. It will work if I run the test via CLI.

Screenshot 2024-07-31 at 15 56 12

Below is my lua config with lazy for Neotest jest showing how I configured it.

return {
    -- Main testing dependency
    "nvim-neotest/neotest",
    dependencies = {
        "nvim-neotest/nvim-nio",
        "nvim-lua/plenary.nvim",
        "antoinemadec/FixCursorHold.nvim",
        "nvim-treesitter/nvim-treesitter",
        "nvim-neotest/neotest-jest",
        "Issafalcon/neotest-dotnet"
    },
    config = function()
        require("neotest").setup({
            adapters = {
                require("neotest-dotnet")({
                    discovery_root = 'solution'
                }),
                require("neotest-jest")({
                    jestCommand = "yarn test", -- Adjust this based on your npm command
                    jestConfigFile = "jest.config.js", -- If you have a Jest config file
                    env = { CI = true },

                    cwd = function(path)
                        -- Assuming tests are in a 'tests' folder
                        local relative_path = vim.fn.fnamemodify(path, ":~:.")
                        if string.match(relative_path, "^tests/") then
                            return vim.fn.getcwd()
                        end
                        return vim.fn.getcwd()
                    end,
                }),
            },
        })
    end,
}
mikaoelitiana commented 1 month ago

Hello @tweiss777, did you manage to make this work? Have you tried setting jest_test_discovery ?

require('neotest-jest')({
      ...,
      jest_test_discovery = true,
    }),

https://github.com/nvim-neotest/neotest-jest?tab=readme-ov-file#parameterized-tests

mikaoelitiana commented 1 month ago

Actually I have the same issue, even with jest_test_discovery set, the tests are not displayed as expected