Skip to content

Commit

Permalink
add test environement esm e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
WeiAnAn committed Mar 24, 2021
1 parent e574c98 commit ae4f650
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
39 changes: 39 additions & 0 deletions e2e/__tests__/testEnvironmentEsm.test.ts
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');
});
});
40 changes: 40 additions & 0 deletions e2e/test-environment-esm/TestEnvironment.mjs
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();
}
}
13 changes: 13 additions & 0 deletions e2e/test-environment-esm/__tests__/custom.test.js
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');
});
6 changes: 6 additions & 0 deletions e2e/test-environment-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "module",
"jest": {
"testEnvironment": "./TestEnvironment.mjs"
}
}

0 comments on commit ae4f650

Please sign in to comment.