Skip to content

Commit

Permalink
add esm globalTeardown e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
WeiAnAn committed Mar 24, 2021
1 parent ffcefa0 commit e574c98
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
39 changes: 39 additions & 0 deletions e2e/__tests__/esmGlobalTeardown.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 {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');
});
});
18 changes: 18 additions & 0 deletions e2e/esm-global-teardown/__tests__/teardown.test.js
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);
});
7 changes: 7 additions & 0 deletions e2e/esm-global-teardown/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "module",
"jest": {
"testEnvironment": "node",
"globalTeardown": "<rootDir>/teardown.mjs"
}
}
22 changes: 22 additions & 0 deletions e2e/esm-global-teardown/teardown.mjs
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();
});
}

0 comments on commit e574c98

Please sign in to comment.