|
| 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 {tmpdir} from 'os'; |
| 9 | +import * as path from 'path'; |
| 10 | +import * as fs from 'graceful-fs'; |
| 11 | +import { |
| 12 | + cleanup, |
| 13 | + createEmptyPackage, |
| 14 | + runYarnInstall, |
| 15 | + writeFiles, |
| 16 | +} from '../Utils'; |
| 17 | +import runJest, {json as runWithJson} from '../runJest'; |
| 18 | + |
| 19 | +const DIR = path.join(tmpdir(), 'jest-global-setup-per-worker'); |
| 20 | +const project1DIR = path.join( |
| 21 | + tmpdir(), |
| 22 | + 'jest-global-setup-per-worker-project-1', |
| 23 | +); |
| 24 | +const project2DIR = path.join( |
| 25 | + tmpdir(), |
| 26 | + 'jest-global-setup-per-worker-project-2', |
| 27 | +); |
| 28 | +const customTransformDIR = path.join( |
| 29 | + tmpdir(), |
| 30 | + 'jest-global-setup-per-worker-custom-transform', |
| 31 | +); |
| 32 | +const nodeModulesDIR = path.join( |
| 33 | + tmpdir(), |
| 34 | + 'jest-global-setup-per-worker-node-modules', |
| 35 | +); |
| 36 | +const rejectionDir = path.join( |
| 37 | + tmpdir(), |
| 38 | + 'jest-global-setup-per-worker-rejection', |
| 39 | +); |
| 40 | +const e2eDir = path.resolve(__dirname, '../global-setup-per-worker'); |
| 41 | +const esmTmpDir = path.join(tmpdir(), 'jest-global-setup-per-worker-esm'); |
| 42 | + |
| 43 | +beforeAll(() => { |
| 44 | + runYarnInstall(e2eDir); |
| 45 | +}); |
| 46 | + |
| 47 | +beforeEach(() => { |
| 48 | + cleanup(DIR); |
| 49 | + cleanup(project1DIR); |
| 50 | + cleanup(project2DIR); |
| 51 | + cleanup(customTransformDIR); |
| 52 | + cleanup(nodeModulesDIR); |
| 53 | + cleanup(rejectionDir); |
| 54 | + cleanup(esmTmpDir); |
| 55 | +}); |
| 56 | + |
| 57 | +afterAll(() => { |
| 58 | + cleanup(DIR); |
| 59 | + cleanup(project1DIR); |
| 60 | + cleanup(project2DIR); |
| 61 | + cleanup(customTransformDIR); |
| 62 | + cleanup(nodeModulesDIR); |
| 63 | + cleanup(rejectionDir); |
| 64 | + cleanup(esmTmpDir); |
| 65 | +}); |
| 66 | + |
| 67 | +test('globalSetupPerWorker is triggered once before all test suites per worker', () => { |
| 68 | + const setupPath = path.join(e2eDir, 'setup.js'); |
| 69 | + const result = runWithJson(e2eDir, [ |
| 70 | + `--globalSetupPerWorker=${setupPath}`, |
| 71 | + '--testPathPatterns=__tests__', |
| 72 | + ]); |
| 73 | + |
| 74 | + expect(result.exitCode).toBe(0); |
| 75 | + const files = fs.readdirSync(DIR); |
| 76 | + expect(files).toHaveLength(1); |
| 77 | + const setup = fs.readFileSync(path.join(DIR, files[0]), 'utf8'); |
| 78 | + expect(setup).toBe('setup'); |
| 79 | +}); |
| 80 | + |
| 81 | +test('jest throws an error when globalSetupPerWorker does not export a function', () => { |
| 82 | + const setupPath = path.resolve( |
| 83 | + __dirname, |
| 84 | + '../global-setup-per-worker/invalidSetup.js', |
| 85 | + ); |
| 86 | + const {exitCode, stderr} = runJest(e2eDir, [ |
| 87 | + `--globalSetupPerWorker=${setupPath}`, |
| 88 | + '--testPathPatterns=__tests__', |
| 89 | + ]); |
| 90 | + |
| 91 | + expect(exitCode).toBe(1); |
| 92 | + expect(stderr).toContain('Jest: Got error running globalSetupPerWorker'); |
| 93 | + expect(stderr).toContain( |
| 94 | + `globalSetupPerWorker file must export a function at ${setupPath}`, |
| 95 | + ); |
| 96 | +}); |
| 97 | + |
| 98 | +test('globalSetupPerWorker function gets global config object and project config as parameters', () => { |
| 99 | + const setupPath = path.resolve(e2eDir, 'setupWithConfig.js'); |
| 100 | + |
| 101 | + const result = runJest(e2eDir, [ |
| 102 | + `--globalSetupPerWorker=${setupPath}`, |
| 103 | + '--testPathPatterns=pass', |
| 104 | + '--cache=true', |
| 105 | + ]); |
| 106 | + |
| 107 | + expect(result.stdout).toBe("[ 'pass' ]\ntrue"); |
| 108 | +}); |
| 109 | + |
| 110 | +test('should call globalSetupPerWorker function of multiple projects', () => { |
| 111 | + const configPath = path.resolve(e2eDir, 'projects.jest.config.js'); |
| 112 | + |
| 113 | + const result = runWithJson(e2eDir, [`--config=${configPath}`]); |
| 114 | + |
| 115 | + expect(result.exitCode).toBe(0); |
| 116 | + |
| 117 | + expect(fs.existsSync(DIR)).toBe(true); |
| 118 | + expect(fs.existsSync(project1DIR)).toBe(true); |
| 119 | + expect(fs.existsSync(project2DIR)).toBe(true); |
| 120 | +}); |
| 121 | + |
| 122 | +test('should not call a globalSetupPerWorker of a project if there are no tests to run from this project', () => { |
| 123 | + const configPath = path.resolve(e2eDir, 'projects.jest.config.js'); |
| 124 | + |
| 125 | + const result = runWithJson(e2eDir, [ |
| 126 | + `--config=${configPath}`, |
| 127 | + '--testPathPatterns=setup1', |
| 128 | + ]); |
| 129 | + |
| 130 | + expect(result.exitCode).toBe(0); |
| 131 | + |
| 132 | + expect(fs.existsSync(DIR)).toBe(true); |
| 133 | + expect(fs.existsSync(project1DIR)).toBe(true); |
| 134 | + expect(fs.existsSync(project2DIR)).toBe(false); |
| 135 | +}); |
| 136 | + |
| 137 | +test('should not call any globalSetupPerWorker if there are no tests to run', () => { |
| 138 | + const configPath = path.resolve(e2eDir, 'projects.jest.config.js'); |
| 139 | + |
| 140 | + const result = runWithJson(e2eDir, [ |
| 141 | + `--config=${configPath}`, |
| 142 | + '--onlyChanged', |
| 143 | + ]); |
| 144 | + |
| 145 | + expect(result.exitCode).toBe(0); |
| 146 | + |
| 147 | + expect(fs.existsSync(DIR)).toBe(false); |
| 148 | + expect(fs.existsSync(project1DIR)).toBe(false); |
| 149 | + expect(fs.existsSync(project2DIR)).toBe(false); |
| 150 | +}); |
| 151 | + |
| 152 | +test('globalSetupPerWorker works with default export', () => { |
| 153 | + const setupPath = path.resolve(e2eDir, 'setupWithDefaultExport.js'); |
| 154 | + |
| 155 | + const result = runJest(e2eDir, [ |
| 156 | + `--globalSetupPerWorker=${setupPath}`, |
| 157 | + '--testPathPatterns=pass', |
| 158 | + '--cache=true', |
| 159 | + ]); |
| 160 | + |
| 161 | + expect(result.stdout).toBe("[ 'pass' ]\ntrue"); |
| 162 | +}); |
| 163 | + |
| 164 | +test('globalSetupPerWorker throws with named export', () => { |
| 165 | + const setupPath = path.resolve(e2eDir, 'invalidSetupWithNamedExport.js'); |
| 166 | + |
| 167 | + const {exitCode, stderr} = runJest(e2eDir, [ |
| 168 | + `--globalSetupPerWorker=${setupPath}`, |
| 169 | + '--testPathPatterns=__tests__', |
| 170 | + ]); |
| 171 | + |
| 172 | + expect(exitCode).toBe(1); |
| 173 | + expect(stderr).toContain('Jest: Got error running globalSetupPerWorker'); |
| 174 | + expect(stderr).toContain( |
| 175 | + `globalSetupPerWorker file must export a function at ${setupPath}`, |
| 176 | + ); |
| 177 | +}); |
| 178 | + |
| 179 | +test('should not transpile the transformer', () => { |
| 180 | + const {exitCode} = runJest('global-setup-per-worker-custom-transform', [ |
| 181 | + '--no-cache', |
| 182 | + ]); |
| 183 | + |
| 184 | + expect(exitCode).toBe(0); |
| 185 | +}); |
| 186 | + |
| 187 | +test('should transform node_modules if configured by transformIgnorePatterns', () => { |
| 188 | + const {exitCode} = runJest('global-setup-per-worker-node-modules', [ |
| 189 | + '--no-cache', |
| 190 | + ]); |
| 191 | + |
| 192 | + expect(exitCode).toBe(0); |
| 193 | +}); |
| 194 | + |
| 195 | +test('properly handle rejections', () => { |
| 196 | + createEmptyPackage(rejectionDir, { |
| 197 | + jest: {globalSetupPerWorker: '<rootDir>/setup.js'}, |
| 198 | + }); |
| 199 | + writeFiles(rejectionDir, { |
| 200 | + 'setup.js': ` |
| 201 | + module.exports = () => Promise.reject(); |
| 202 | + `, |
| 203 | + 'test.js': ` |
| 204 | + test('dummy', () => { |
| 205 | + expect(true).toBe(true); |
| 206 | + }); |
| 207 | + `, |
| 208 | + }); |
| 209 | + |
| 210 | + const {exitCode, stderr} = runJest(rejectionDir, ['--no-cache']); |
| 211 | + |
| 212 | + expect(exitCode).toBe(1); |
| 213 | + expect(stderr).toContain( |
| 214 | + 'Error: Jest: Got error running globalSetupPerWorker', |
| 215 | + ); |
| 216 | + expect(stderr).toContain('reason: undefined'); |
| 217 | +}); |
| 218 | + |
| 219 | +test('globalSetupPerWorker works with ESM modules', () => { |
| 220 | + const {exitCode} = runJest('global-setup-per-worker-esm', ['--no-cache'], { |
| 221 | + nodeOptions: '--experimental-vm-modules --no-warnings', |
| 222 | + }); |
| 223 | + |
| 224 | + expect(exitCode).toBe(0); |
| 225 | +}); |
0 commit comments