-
-
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
98 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 {cleanup} from '../Utils'; | ||
import runJest from '../runJest'; | ||
|
||
const DIR = tmpdir() + '/jest-test-environment-esm'; | ||
|
||
beforeEach(() => cleanup(DIR)); | ||
afterAll(() => cleanup(DIR)); | ||
|
||
onNodeVersions('^12.16.0 || >=13.7.0', () => { | ||
it('triggers setup/teardown hooks', () => { | ||
const testDir = path.resolve( | ||
__dirname, | ||
'..', | ||
'test-environment-esm', | ||
'__tests__', | ||
); | ||
const testFile = path.join(testDir, 'custom.test.js'); | ||
|
||
const result = runJest('test-environment-esm', [], { | ||
nodeOptions: '--experimental-vm-modules --no-warnings', | ||
}); | ||
expect(result.exitCode).toBe(0); | ||
expect(result.stdout).toContain(`TestEnvironment.setup: ${testFile}`); | ||
|
||
const teardown = fs.readFileSync(DIR + '/teardown', '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,40 @@ | ||
/** | ||
* 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 fs from 'graceful-fs'; | ||
import JSDOMEnvironment from 'jest-environment-jsdom'; | ||
import {createDirectory} from 'jest-util'; | ||
|
||
const DIR = os.tmpdir() + '/jest-test-environment-esm'; | ||
|
||
export default class TestEnvironment extends JSDOMEnvironment { | ||
constructor(config, context) { | ||
super(config, context); | ||
this.context = context; | ||
} | ||
|
||
setup() { | ||
console.info('TestEnvironment.setup:', this.context.testPath); | ||
return super.setup().then(() => { | ||
this.global.setup = 'setup'; | ||
}); | ||
} | ||
|
||
teardown() { | ||
return super.teardown().then(() => { | ||
createDirectory(DIR); | ||
fs.writeFileSync(DIR + '/teardown', 'teardown'); | ||
}); | ||
} | ||
|
||
getVmContext() { | ||
return super.getVmContext(); | ||
} | ||
} |
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,13 @@ | ||
/** | ||
* 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'; | ||
/* eslint-env browser*/ | ||
|
||
test('setup', () => { | ||
expect(global.setup).toBe('setup'); | ||
}); |
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,6 @@ | ||
{ | ||
"type": "module", | ||
"jest": { | ||
"testEnvironment": "./TestEnvironment.mjs" | ||
} | ||
} |