Skip to content

Commit 252a476

Browse files
committed
#8708 Copy e2e test of globalTeardown for globalTeardownPerWorker
1 parent 05f5634 commit 252a476

22 files changed

+2201
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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 {createDirectory} from 'jest-util';
12+
import {cleanup, runYarnInstall} from '../Utils';
13+
import runJest, {json as runWithJson} from '../runJest';
14+
15+
const DIR = path.join(tmpdir(), 'jest-global-teardown-per-worker');
16+
const project1DIR = path.join(
17+
tmpdir(),
18+
'jest-global-teardown-per-worker-project-1',
19+
);
20+
const project2DIR = path.join(
21+
tmpdir(),
22+
'jest-global-teardown-per-worker-project-2',
23+
);
24+
const e2eDir = path.resolve(__dirname, '../global-teardown-per-worker');
25+
const esmTmpDir = path.join(tmpdir(), 'jest-global-teardown-per-worker-esm');
26+
27+
beforeAll(() => {
28+
runYarnInstall(e2eDir);
29+
});
30+
31+
beforeEach(() => {
32+
cleanup(DIR);
33+
cleanup(project1DIR);
34+
cleanup(project2DIR);
35+
cleanup(esmTmpDir);
36+
});
37+
afterAll(() => {
38+
cleanup(DIR);
39+
cleanup(project1DIR);
40+
cleanup(project2DIR);
41+
cleanup(esmTmpDir);
42+
});
43+
44+
test('globalTeardownPerWorker is triggered once after all test suites per worker', () => {
45+
createDirectory(DIR);
46+
const teardownPath = path.resolve(e2eDir, 'teardown.js');
47+
const result = runWithJson('global-teardown-per-worker', [
48+
`--globalTeardown=${teardownPath}`,
49+
'--testPathPatterns=__tests__',
50+
]);
51+
52+
expect(result.exitCode).toBe(0);
53+
const files = fs.readdirSync(DIR);
54+
expect(files).toHaveLength(1);
55+
const teardown = fs.readFileSync(path.join(DIR, files[0]), 'utf8');
56+
expect(teardown).toBe('teardown');
57+
});
58+
59+
test('jest throws an error when globalTeardownPerWorker does not export a function', () => {
60+
const teardownPath = path.resolve(e2eDir, 'invalidTeardown.js');
61+
const {exitCode, stderr} = runJest(e2eDir, [
62+
`--globalTeardown=${teardownPath}`,
63+
'--testPathPatterns=__tests__',
64+
]);
65+
66+
expect(exitCode).toBe(1);
67+
expect(stderr).toContain('Jest: Got error running globalTeardown');
68+
expect(stderr).toContain(
69+
`globalTeardown file must export a function at ${teardownPath}`,
70+
);
71+
});
72+
73+
test('globalTeardownPerWorker function gets global config object and project config as parameters', () => {
74+
const teardownPath = path.resolve(e2eDir, 'teardownWithConfig.js');
75+
76+
const result = runJest(e2eDir, [
77+
`--globalTeardown=${teardownPath}`,
78+
'--testPathPatterns=pass',
79+
'--cache=true',
80+
]);
81+
82+
expect(result.stdout).toBe("[ 'pass' ]\ntrue");
83+
});
84+
85+
test('should call globalTeardownPerWorker function of multiple projects', () => {
86+
const configPath = path.resolve(e2eDir, 'projects.jest.config.js');
87+
88+
const result = runWithJson('global-teardown-per-worker', [
89+
`--config=${configPath}`,
90+
]);
91+
92+
expect(result.exitCode).toBe(0);
93+
94+
expect(fs.existsSync(DIR)).toBe(true);
95+
expect(fs.existsSync(project1DIR)).toBe(true);
96+
expect(fs.existsSync(project2DIR)).toBe(true);
97+
});
98+
99+
test('should not call a globalTeardownPerWorker of a project if there are no tests to run from this project', () => {
100+
const configPath = path.resolve(e2eDir, 'projects.jest.config.js');
101+
102+
const result = runWithJson('global-teardown-per-worker', [
103+
`--config=${configPath}`,
104+
'--testPathPatterns=teardown1',
105+
]);
106+
107+
expect(result.exitCode).toBe(0);
108+
109+
expect(fs.existsSync(DIR)).toBe(true);
110+
expect(fs.existsSync(project1DIR)).toBe(true);
111+
expect(fs.existsSync(project2DIR)).toBe(false);
112+
});
113+
114+
test('globalTeardownPerWorker works with default export', () => {
115+
const teardownPath = path.resolve(e2eDir, 'teardownWithDefaultExport.js');
116+
117+
const result = runJest(e2eDir, [
118+
`--globalTeardown=${teardownPath}`,
119+
'--testPathPatterns=pass',
120+
'--cache=true',
121+
]);
122+
123+
expect(result.stdout).toBe("[ 'pass' ]\ntrue");
124+
});
125+
126+
test('globalTeardownPerWorker throws with named export', () => {
127+
const teardownPath = path.resolve(
128+
e2eDir,
129+
'invalidTeardownWithNamedExport.js',
130+
);
131+
132+
const {exitCode, stderr} = runJest(e2eDir, [
133+
`--globalTeardown=${teardownPath}`,
134+
'--testPathPatterns=__tests__',
135+
]);
136+
137+
expect(exitCode).toBe(1);
138+
expect(stderr).toContain('Jest: Got error running globalTeardown');
139+
expect(stderr).toContain(
140+
`globalTeardown file must export a function at ${teardownPath}`,
141+
);
142+
});
143+
144+
test('globalTeardownPerWorker works with ESM modules', () => {
145+
const {exitCode} = runJest('global-teardown-per-worker-esm', ['--no-cache'], {
146+
nodeOptions: '--experimental-vm-modules --no-warnings',
147+
});
148+
149+
expect(exitCode).toBe(0);
150+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 * as os from 'os';
9+
import * as path from 'path';
10+
import fs from 'graceful-fs';
11+
import greeting from '../';
12+
13+
const DIR = path.join(os.tmpdir(), 'jest-global-teardown-per-worker-esm');
14+
15+
test('should not exist teardown file', () => {
16+
expect(fs.existsSync(DIR)).toBe(false);
17+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
export default 'hello!';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "module",
3+
"jest": {
4+
"testEnvironment": "node",
5+
"globalTeardown": "<rootDir>/teardown.js",
6+
"transform": {}
7+
}
8+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
import * as crypto from 'crypto';
8+
import * as os from 'os';
9+
import * as path from 'path';
10+
import fs from 'graceful-fs';
11+
import {createDirectory} from 'jest-util';
12+
13+
const DIR = path.join(os.tmpdir(), 'jest-global-teardown-per-worker-esm');
14+
15+
export default function () {
16+
return new Promise(resolve => {
17+
createDirectory(DIR);
18+
const fileId = crypto.randomBytes(20).toString('hex');
19+
fs.writeFileSync(path.join(DIR, fileId), 'teardown');
20+
resolve();
21+
});
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
'use strict';
8+
9+
const fs = require('fs');
10+
const os = require('os');
11+
const path = require('path');
12+
13+
const DIR = path.join(os.tmpdir(), 'jest-global-teardown-per-worker');
14+
15+
test('should not exist teardown file', () => {
16+
const files = fs.readdirSync(DIR);
17+
expect(files).toHaveLength(0);
18+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
'use strict';
8+
9+
const fs = require('fs');
10+
const os = require('os');
11+
const path = require('path');
12+
13+
const DIR = path.join(os.tmpdir(), 'jest-global-teardown-per-worker');
14+
15+
test('should not exist teardown file', () => {
16+
const files = fs.readdirSync(DIR);
17+
expect(files).toHaveLength(0);
18+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
'use strict';
8+
9+
const fs = require('fs');
10+
const os = require('os');
11+
const path = require('path');
12+
13+
const DIR = path.join(os.tmpdir(), 'jest-global-teardown-per-worker');
14+
15+
test('should not exist teardown file', () => {
16+
const files = fs.readdirSync(DIR);
17+
expect(files).toHaveLength(0);
18+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
module.exports = {
9+
presets: ['@babel/preset-env', '@babel/preset-flow'],
10+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
test('should pass', () => {});

0 commit comments

Comments
 (0)