-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "module", | ||
"jest": { | ||
"testEnvironment": "node", | ||
"globalTeardown": "<rootDir>/teardown.mjs" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
} |