jestjs/jest

[Help] Global checks

Closed this issue · 3 comments

Hi! Can you please tell me how to implement global checks?

I have about 10 thousand cases and I really don't want to copy-paste, I'm looking for a solution similar to a decorator that will check its statuses and headers after each test.

I implemented this thing, but it crashes the VScode extension. Maybe you can tell me what's better?

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  setupFilesAfterEnv: ['./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
  );
};

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.