|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import path = require('path'); |
| 9 | +import * as fs from 'graceful-fs'; |
| 10 | +import {requireOrImportModule} from 'jest-util'; |
| 11 | +import readConfigFileAndSetRootDir from '../readConfigFileAndSetRootDir'; |
| 12 | + |
| 13 | +jest.mock('graceful-fs').mock('jest-util'); |
| 14 | + |
| 15 | +describe('readConfigFileAndSetRootDir', () => { |
| 16 | + describe('JavaScript file', () => { |
| 17 | + test('reads config and sets `rootDir`', async () => { |
| 18 | + jest.mocked(requireOrImportModule).mockResolvedValueOnce({notify: true}); |
| 19 | + |
| 20 | + const rootDir = path.resolve('some', 'path', 'to'); |
| 21 | + const config = await readConfigFileAndSetRootDir( |
| 22 | + path.join(rootDir, 'jest.config.js'), |
| 23 | + ); |
| 24 | + |
| 25 | + expect(config).toEqual({notify: true, rootDir}); |
| 26 | + }); |
| 27 | + |
| 28 | + test('handles exported function', async () => { |
| 29 | + jest |
| 30 | + .mocked(requireOrImportModule) |
| 31 | + .mockResolvedValueOnce(() => ({bail: 1})); |
| 32 | + |
| 33 | + const rootDir = path.resolve('some', 'path', 'to'); |
| 34 | + const config = await readConfigFileAndSetRootDir( |
| 35 | + path.join(rootDir, 'jest.config.js'), |
| 36 | + ); |
| 37 | + |
| 38 | + expect(config).toEqual({bail: 1, rootDir}); |
| 39 | + }); |
| 40 | + |
| 41 | + test('handles exported async function', async () => { |
| 42 | + jest |
| 43 | + .mocked(requireOrImportModule) |
| 44 | + .mockResolvedValueOnce(async () => ({testTimeout: 10000})); |
| 45 | + |
| 46 | + const rootDir = path.resolve('some', 'path', 'to'); |
| 47 | + const config = await readConfigFileAndSetRootDir( |
| 48 | + path.join(rootDir, 'jest.config.js'), |
| 49 | + ); |
| 50 | + |
| 51 | + expect(config).toEqual({rootDir, testTimeout: 10000}); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('JSON file', () => { |
| 56 | + test('reads config and sets `rootDir`', async () => { |
| 57 | + jest.mocked(fs.readFileSync).mockReturnValueOnce('{ "verbose": true }'); |
| 58 | + |
| 59 | + const rootDir = path.resolve('some', 'path', 'to'); |
| 60 | + const config = await readConfigFileAndSetRootDir( |
| 61 | + path.join(rootDir, 'jest.config.json'), |
| 62 | + ); |
| 63 | + |
| 64 | + expect(config).toEqual({rootDir, verbose: true}); |
| 65 | + }); |
| 66 | + |
| 67 | + test('supports comments in JSON', async () => { |
| 68 | + jest |
| 69 | + .mocked(fs.readFileSync) |
| 70 | + .mockReturnValueOnce('{ // test comment\n "bail": true }'); |
| 71 | + |
| 72 | + const rootDir = path.resolve('some', 'path', 'to'); |
| 73 | + const config = await readConfigFileAndSetRootDir( |
| 74 | + path.join(rootDir, 'jest.config.json'), |
| 75 | + ); |
| 76 | + |
| 77 | + expect(config).toEqual({bail: true, rootDir}); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('package.json file', () => { |
| 82 | + test('reads config from "jest" key and sets `rootDir`', async () => { |
| 83 | + jest |
| 84 | + .mocked(fs.readFileSync) |
| 85 | + .mockReturnValueOnce('{ "jest": { "coverage": true } }'); |
| 86 | + |
| 87 | + const rootDir = path.resolve('some', 'path', 'to'); |
| 88 | + const config = await readConfigFileAndSetRootDir( |
| 89 | + path.join(rootDir, 'package.json'), |
| 90 | + ); |
| 91 | + |
| 92 | + expect(config).toEqual({coverage: true, rootDir}); |
| 93 | + }); |
| 94 | + |
| 95 | + test('sets rootDir if "jest" is absent', async () => { |
| 96 | + jest.mocked(fs.readFileSync).mockReturnValueOnce('{ "name": "test" }'); |
| 97 | + |
| 98 | + const rootDir = path.resolve('some', 'path', 'to'); |
| 99 | + const config = await readConfigFileAndSetRootDir( |
| 100 | + path.join(rootDir, 'package.json'), |
| 101 | + ); |
| 102 | + |
| 103 | + expect(config).toEqual({rootDir}); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('sets `rootDir`', () => { |
| 108 | + test('handles frozen config object', async () => { |
| 109 | + jest |
| 110 | + .mocked(requireOrImportModule) |
| 111 | + .mockResolvedValueOnce(Object.freeze({preset: 'some-preset'})); |
| 112 | + |
| 113 | + const rootDir = path.resolve('some', 'path', 'to'); |
| 114 | + const config = await readConfigFileAndSetRootDir( |
| 115 | + path.join(rootDir, 'jest.config.js'), |
| 116 | + ); |
| 117 | + |
| 118 | + expect(config).toEqual({preset: 'some-preset', rootDir}); |
| 119 | + }); |
| 120 | + |
| 121 | + test('keeps the path if it is absolute', async () => { |
| 122 | + const rootDir = path.resolve('some', 'path', 'to'); |
| 123 | + jest.mocked(requireOrImportModule).mockResolvedValueOnce({ |
| 124 | + rootDir, |
| 125 | + testEnvironment: 'node', |
| 126 | + }); |
| 127 | + |
| 128 | + const config = await readConfigFileAndSetRootDir( |
| 129 | + path.join(path.resolve('other', 'path', 'to'), 'jest.config.js'), |
| 130 | + ); |
| 131 | + |
| 132 | + expect(config).toEqual({rootDir, testEnvironment: 'node'}); |
| 133 | + }); |
| 134 | + |
| 135 | + test('resolves the path relative to dirname of the config file', async () => { |
| 136 | + jest.mocked(requireOrImportModule).mockResolvedValueOnce({ |
| 137 | + restoreMocks: true, |
| 138 | + rootDir: path.join('path', 'to'), |
| 139 | + }); |
| 140 | + |
| 141 | + const config = await readConfigFileAndSetRootDir( |
| 142 | + path.join(path.resolve('some'), 'jest.config.js'), |
| 143 | + ); |
| 144 | + |
| 145 | + expect(config).toEqual({ |
| 146 | + restoreMocks: true, |
| 147 | + rootDir: path.resolve('some', 'path', 'to'), |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + test('resolves relative path when the read config object if frozen', async () => { |
| 152 | + jest.mocked(requireOrImportModule).mockResolvedValueOnce( |
| 153 | + Object.freeze({ |
| 154 | + resetModules: true, |
| 155 | + rootDir: path.join('path', 'to'), |
| 156 | + }), |
| 157 | + ); |
| 158 | + |
| 159 | + const config = await readConfigFileAndSetRootDir( |
| 160 | + path.join(path.resolve('some'), 'jest.config.js'), |
| 161 | + ); |
| 162 | + |
| 163 | + expect(config).toEqual({ |
| 164 | + resetModules: true, |
| 165 | + rootDir: path.resolve('some', 'path', 'to'), |
| 166 | + }); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments