diff --git a/e2e/__tests__/esmGlobalTeardown.test.ts b/e2e/__tests__/esmGlobalTeardown.test.ts new file mode 100644 index 000000000000..6c2696f6a071 --- /dev/null +++ b/e2e/__tests__/esmGlobalTeardown.test.ts @@ -0,0 +1,39 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import {tmpdir} from 'os'; +import * as path from 'path'; +import * as fs from 'graceful-fs'; +import {onNodeVersions} from '@jest/test-utils'; +import {createDirectory} from 'jest-util'; +import {cleanup} from '../Utils'; +import {json as runWithJson} from '../runJest'; + +const DIR = path.join(tmpdir(), 'jest-esm-global-teardown'); +const e2eDir = path.resolve(__dirname, '../esm-global-teardown'); + +beforeEach(() => { + cleanup(DIR); +}); +afterAll(() => { + cleanup(DIR); +}); + +onNodeVersions('^12.16.0 || >=13.7.0', () => { + test('globalTeardown is triggered once after all test suites', () => { + createDirectory(DIR); + const result = runWithJson(e2eDir, [], { + nodeOptions: '--experimental-vm-modules', + }); + + expect(result.exitCode).toBe(0); + const files = fs.readdirSync(DIR); + expect(files).toHaveLength(1); + const teardown = fs.readFileSync(path.join(DIR, files[0]), 'utf8'); + expect(teardown).toBe('teardown'); + }); +}); diff --git a/e2e/esm-global-teardown/__tests__/teardown.test.js b/e2e/esm-global-teardown/__tests__/teardown.test.js new file mode 100644 index 000000000000..3debeaad4923 --- /dev/null +++ b/e2e/esm-global-teardown/__tests__/teardown.test.js @@ -0,0 +1,18 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +'use strict'; + +import os from 'os'; +import path from 'path'; +import fs from 'graceful-fs'; + +const DIR = path.join(os.tmpdir(), 'jest-esm-global-teardown'); + +test('should not exist teardown file', () => { + const files = fs.readdirSync(DIR); + expect(files).toHaveLength(0); +}); diff --git a/e2e/esm-global-teardown/package.json b/e2e/esm-global-teardown/package.json new file mode 100644 index 000000000000..65879a0f7ce9 --- /dev/null +++ b/e2e/esm-global-teardown/package.json @@ -0,0 +1,7 @@ +{ + "type": "module", + "jest": { + "testEnvironment": "node", + "globalTeardown": "/teardown.mjs" + } +} diff --git a/e2e/esm-global-teardown/teardown.mjs b/e2e/esm-global-teardown/teardown.mjs new file mode 100644 index 000000000000..bd592ca7c65e --- /dev/null +++ b/e2e/esm-global-teardown/teardown.mjs @@ -0,0 +1,22 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +import crypto from 'crypto'; +import os from 'os'; +import path from 'path'; +import fs from 'graceful-fs'; +import {createDirectory} from 'jest-util'; + +const DIR = path.join(os.tmpdir(), 'jest-esm-global-teardown'); + +export default function () { + return new Promise((resolve, reject) => { + createDirectory(DIR); + const fileId = crypto.randomBytes(20).toString('hex'); + fs.writeFileSync(path.join(DIR, fileId), 'teardown'); + resolve(); + }); +}