'jest how to merge two xx.spec.ts to be one?

in order to cover all statements/branch/lines, I need to write two or more fn.spec.ts to test fn.ts, how can I merge fn.spec.ts and fn2.spec.ts to be one file ?

// fn.ts
export const getEnv = () => {
  if (location.href.indexOf('localhost') !== -1 || /\d+\.\d+\.\d+\.\d+/.test(location.href)) {
    return 'Test'
  }
  return 'Product'
}

// fn.spec.ts 
describe('fn getEnv',()=>{
  Object.defineProperty(window, 'location', {
    value: {
      href: 'http://192.168.2.3:9001'
    },
  })
  const { getEnv } = require('./fn')
  test('getEnv',()=>{
    expect(getEnv()).toBe('Test')
  })
})
// fn2.spec.ts
describe('fn getEnv',()=>{
  Object.defineProperty(window, 'location', {
    value: {
      href: 'https://xx.com'
    },
  })
  const { getEnv } = require('./fn')
  test('getEnv',()=>{
    expect(getEnv()).toBe('Product')
  })
})

// jest.config.js
module.exports = {
  testEnvironment: 'jest-environment-jsdom', // browser environment
}


Solution 1:[1]

Just merge it into one file with a clear expectation message.

import { getEnv } from './fn';

describe('fn', () => {
  describe('getEnv', () => {
    test('should return "Test" when window location is an IP address', () => {
      Object.defineProperty(window, 'location', {
        value: {
          href: 'http://192.168.2.3:9001'
        },
      });

      const actual = getEnv();

      expect(actual).toBe('Test');
    });

    test('should return "Product" when window location is a domain', () => {
      Object.defineProperty(window, 'location', {
        value: {
          href: 'https://xx.com'
        },
      });

      const actual = getEnv();

      expect(actual).toBe('Product');
    })
  });
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hoangdv