|
| 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 {cleanup, runYarnInstall} from '../Utils'; |
| 12 | +import {json as runWithJson} from '../runJest'; |
| 13 | + |
| 14 | +const DIR = path.join(tmpdir(), 'jest-global-teardown-and-per-worker'); |
| 15 | +const project1DIR = path.join( |
| 16 | + tmpdir(), |
| 17 | + 'jest-global-teardown-and-per-worker-project-1', |
| 18 | +); |
| 19 | +const project2DIR = path.join( |
| 20 | + tmpdir(), |
| 21 | + 'jest-global-teardown-and-per-worker-project-2', |
| 22 | +); |
| 23 | +const e2eDir = path.resolve(__dirname, '../global-teardown-and-per-worker'); |
| 24 | + |
| 25 | +beforeAll(() => { |
| 26 | + runYarnInstall(e2eDir); |
| 27 | +}); |
| 28 | + |
| 29 | +beforeEach(() => { |
| 30 | + cleanup(DIR); |
| 31 | + cleanup(project1DIR); |
| 32 | + cleanup(project2DIR); |
| 33 | +}); |
| 34 | + |
| 35 | +afterAll(() => { |
| 36 | + cleanup(DIR); |
| 37 | + cleanup(project1DIR); |
| 38 | + cleanup(project2DIR); |
| 39 | +}); |
| 40 | + |
| 41 | +test('globalTeardown triggered once + globalTeardownPerWorker triggered once per worker', () => { |
| 42 | + const teardownPath = path.join(e2eDir, 'teardown.js'); |
| 43 | + const teardownPerWorkerPath = path.join(e2eDir, 'teardown-per-worker.js'); |
| 44 | + const result = runWithJson(e2eDir, [ |
| 45 | + '--maxWorkers=2', |
| 46 | + '--workerIdleMemoryLimit=100MB', |
| 47 | + `--globalTeardown=${teardownPath}`, |
| 48 | + `--globalTeardownPerWorker=${teardownPerWorkerPath}`, |
| 49 | + '--testPathPatterns=__tests__', |
| 50 | + ]); |
| 51 | + |
| 52 | + expect(result.exitCode).toBe(0); |
| 53 | + const files = fs.readdirSync(DIR); |
| 54 | + expect(files).toHaveLength(3); |
| 55 | + const content = files.map(file => { |
| 56 | + const data = fs.readFileSync(path.join(DIR, file), 'utf8'); |
| 57 | + return data.split('\n'); |
| 58 | + }); |
| 59 | + for (const [firstLine, secondLine] of content) { |
| 60 | + if (secondLine) { |
| 61 | + expect(firstLine).toBe('teardown-per-worker'); |
| 62 | + } else { |
| 63 | + expect(firstLine).toBe('teardown'); |
| 64 | + } |
| 65 | + } |
| 66 | + const secondLines = content.map(([, secondLine]) => secondLine); |
| 67 | + secondLines.sort(); |
| 68 | + expect(secondLines).toEqual(['1', '2', undefined]); |
| 69 | +}); |
| 70 | +test('globalTeardown + globalTeardownPerWorker with worker threads', () => { |
| 71 | + const teardownPath = path.join(e2eDir, 'teardown.js'); |
| 72 | + const teardownPerWorkerPath = path.join(e2eDir, 'teardown-per-worker.js'); |
| 73 | + const result = runWithJson(e2eDir, [ |
| 74 | + '--maxWorkers=2', |
| 75 | + '--workerIdleMemoryLimit=100MB', |
| 76 | + `--globalTeardown=${teardownPath}`, |
| 77 | + `--globalTeardownPerWorker=${teardownPerWorkerPath}`, |
| 78 | + '--testPathPatterns=__tests__', |
| 79 | + '--workerThreads', |
| 80 | + ]); |
| 81 | + |
| 82 | + expect(result.exitCode).toBe(0); |
| 83 | + const files = fs.readdirSync(DIR); |
| 84 | + expect(files).toHaveLength(3); |
| 85 | + const content = files.map(file => { |
| 86 | + const data = fs.readFileSync(path.join(DIR, file), 'utf8'); |
| 87 | + return data.split('\n'); |
| 88 | + }); |
| 89 | + for (const [firstLine, secondLine] of content) { |
| 90 | + if (secondLine) { |
| 91 | + expect(firstLine).toBe('teardown-per-worker'); |
| 92 | + } else { |
| 93 | + expect(firstLine).toBe('teardown'); |
| 94 | + } |
| 95 | + } |
| 96 | + const secondLines = content.map(([, secondLine]) => secondLine); |
| 97 | + secondLines.sort(); |
| 98 | + expect(secondLines).toEqual(['1', '2', undefined]); |
| 99 | +}); |
| 100 | + |
| 101 | +test('multiple projects', () => { |
| 102 | + const configPath = path.resolve(e2eDir, 'projects.jest.config.js'); |
| 103 | + |
| 104 | + const result = runWithJson(e2eDir, [ |
| 105 | + '--maxWorkers=2', |
| 106 | + '--workerIdleMemoryLimit=100MB', |
| 107 | + `--config=${configPath}`, |
| 108 | + ]); |
| 109 | + |
| 110 | + expect(result.exitCode).toBe(0); |
| 111 | + |
| 112 | + expect(fs.existsSync(DIR)).toBe(true); |
| 113 | + expect(fs.existsSync(project1DIR)).toBe(true); |
| 114 | + expect(fs.existsSync(project2DIR)).toBe(true); |
| 115 | +}); |
0 commit comments