From ae4f6507621fce7aebf7b0fd5fcef653611667b3 Mon Sep 17 00:00:00 2001 From: "Wei-An, Yen" Date: Thu, 25 Mar 2021 01:17:33 +0800 Subject: [PATCH] add test environement esm e2e test --- e2e/__tests__/testEnvironmentEsm.test.ts | 39 ++++++++++++++++++ e2e/test-environment-esm/TestEnvironment.mjs | 40 +++++++++++++++++++ .../__tests__/custom.test.js | 13 ++++++ e2e/test-environment-esm/package.json | 6 +++ 4 files changed, 98 insertions(+) create mode 100644 e2e/__tests__/testEnvironmentEsm.test.ts create mode 100644 e2e/test-environment-esm/TestEnvironment.mjs create mode 100644 e2e/test-environment-esm/__tests__/custom.test.js create mode 100644 e2e/test-environment-esm/package.json diff --git a/e2e/__tests__/testEnvironmentEsm.test.ts b/e2e/__tests__/testEnvironmentEsm.test.ts new file mode 100644 index 000000000000..a980c4802a74 --- /dev/null +++ b/e2e/__tests__/testEnvironmentEsm.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 {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'); + }); +}); diff --git a/e2e/test-environment-esm/TestEnvironment.mjs b/e2e/test-environment-esm/TestEnvironment.mjs new file mode 100644 index 000000000000..995e47676e7d --- /dev/null +++ b/e2e/test-environment-esm/TestEnvironment.mjs @@ -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(); + } +} diff --git a/e2e/test-environment-esm/__tests__/custom.test.js b/e2e/test-environment-esm/__tests__/custom.test.js new file mode 100644 index 000000000000..0bc863baf566 --- /dev/null +++ b/e2e/test-environment-esm/__tests__/custom.test.js @@ -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'); +}); diff --git a/e2e/test-environment-esm/package.json b/e2e/test-environment-esm/package.json new file mode 100644 index 000000000000..a9748a80a5f1 --- /dev/null +++ b/e2e/test-environment-esm/package.json @@ -0,0 +1,6 @@ +{ + "type": "module", + "jest": { + "testEnvironment": "./TestEnvironment.mjs" + } +}