Skip to content

Commit

Permalink
Add support for async function in setupFilesAfterEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
vkml committed Dec 11, 2023
1 parent e54c0eb commit 66ef648
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@
- `[jest-worker]` [**BREAKING**] Allow only absolute `workerPath` ([#12343](https://github.com/facebook/jest/pull/12343))
- `[jest-worker]` [**BREAKING**] Default to advanced serialization when using child process workers ([#10983](https://github.com/facebook/jest/pull/10983))
- `[pretty-format]` New `maxWidth` parameter ([#12402](https://github.com/facebook/jest/pull/12402))
- `[jest-circus, jest-jasmine2]` Allow `setupFilesAfterEnv` to export an async function ([#10962](https://github.com/jestjs/jest/issues/10962))

### Fixes

Expand Down
44 changes: 44 additions & 0 deletions e2e/__tests__/setupFilesAfterEnvConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,48 @@ describe('setupFilesAfterEnv', () => {
expect(result.json.testResults).toHaveLength(1);
expect(result.exitCode).toBe(0);
});

it('awaits async function returned from the setup file (jest-circus)', () => {
const pkgJson = {
jest: {
setupFilesAfterEnv: ['./setupAsyncFunction.js'],
testRunner: 'jest-circus',
},
};

writeFiles(DIR, {
'package.json': JSON.stringify(pkgJson, null, 2),
});

const result = runWithJson('setup-files-after-env-config', [
'setupAsyncFunction.test.js',
]);

expect(result.json.numTotalTests).toBe(1);
expect(result.json.numPassedTests).toBe(1);
expect(result.json.testResults).toHaveLength(1);
expect(result.exitCode).toBe(0);
});

it('awaits async function returned from the setup file (jest-jasmine2)', () => {
const pkgJson = {
jest: {
setupFilesAfterEnv: ['./setupAsyncFunction.js'],
testRunner: 'jest-jasmine2',
},
};

writeFiles(DIR, {
'package.json': JSON.stringify(pkgJson, null, 2),
});

const result = runWithJson('setup-files-after-env-config', [
'setupAsyncFunction.test.js',
]);

expect(result.json.numTotalTests).toBe(1);
expect(result.json.numPassedTests).toBe(1);
expect(result.json.testResults).toHaveLength(1);
expect(result.exitCode).toBe(0);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

describe('setupFilesAfterEnv', () => {
it('has waited for async function', () => {
expect(globalThis.afterEnvAsyncFunctionFinished).toBe(true);
});
});
10 changes: 10 additions & 0 deletions e2e/setup-files-after-env-config/setupAsyncFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
globalThis.afterEnvAsyncFunctionFinished = false;

module.exports = async () => {
await new Promise(resolve =>
setTimeout(() => {
globalThis.afterEnvAsyncFunctionFinished = true;
resolve();
}, 2000),
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ const jestAdapter = async (
if (esm) {
await runtime.unstable_importModule(path);
} else {
runtime.requireModule(path);
const setupFile = runtime.requireModule(path);
if (typeof setupFile === 'function') {
await setupFile();
}
}
}
const setupAfterEnvEnd = Date.now();
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-jasmine2/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ export default async function jasmine2(
if (esm) {
await runtime.unstable_importModule(path);
} else {
runtime.requireModule(path);
const setupFile = runtime.requireModule(path);
if (typeof setupFile === 'function') {
await setupFile();
}
}
}

Expand Down

0 comments on commit 66ef648

Please sign in to comment.