jest-community / vscode-jest

The optimal flow for Jest based testing in VS Code
MIT License
2.84k stars 294 forks source link

[BUG] Extension stops detecting tests after 2 launches #1189

Open NeilRiver opened 1 month ago

NeilRiver commented 1 month ago

Describe the bug It's simple, I develop tests, and finally decided to check them, an error occurred, I click on the icon 2 times and on the 3rd time all the icons in the tests disappear.

Screenshots 1) all ok image 2) one more click and everyone reload the page if you want the icon to appear image 3) and it is not clear why he started to consider import as a test? image

Environment (please complete the following information):

at the moment i run tests by clicking on the icon

NeilRiver commented 1 month ago

I found out what the matter was, the problem was in my global handler of basic checks.

I really don't like duplicating code, inserting these usual checks and I found a way to check. Question... why does the extension crash from this?

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  setupFilesAfterEnv: ['./jest.setup.js']
};
// ./jest.setup.js
const originalTest = global.test;

global.test = (name, fn, timeout) => {
  return originalTest(
    name,
    async () => {
      const result = await fn();
      if (result && typeof result === 'object' && 'status' in result) {
        const isSuccessStatus = (status) => status >= 200 && status < 300;
        expect(isSuccessStatus(result.status)).toBeTruthy();
        expect(result.duration).toBeLessThan(1000);
        expect(result.headers['content-type']).toMatch(/application\/json/);
        expect(result.headers['connection']).toBe('keep-alive');
        expect(() => JSON.parse(JSON.stringify(result.data))).not.toThrow();
      }
      return result;
    },
    timeout
  );
};
connectdotz commented 2 weeks ago

Hmm... It is not clear to me why the global setup will trigger this behavior... Do you have a sample repo that we can try to repro your issue?

NeilRiver commented 2 weeks ago

Hmm... It is not clear to me why the global setup will trigger this behavior... Do you have a sample repo that we can try to repro your issue?

Sorry, this is a working project i already abandoned this solution because there is duplication here... i call the async request 2 times... before the test and in the test itself