Skip to content

Commit

Permalink
reset everything
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas committed Feb 25, 2022
1 parent 0fd4e16 commit 3ee2adf
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/Configuration.md
Expand Up @@ -925,6 +925,12 @@ Default: `[]`

A list of paths to modules that run some code to configure or set up the testing environment. Each setupFile will be run once per test file. Since every test runs in its own environment, these scripts will be executed in the testing environment before executing [`setupFilesAfterEnv`](#setupfilesafterenv-array) and before the test code itself.

:::note

If your setup script is a CJS module, it may export an async function. Jest will call the function and will await for the result. This might be useful to fetch some data asynchronously. In case of ESM module, simply use top-level await to achieve the same result.

:::

### `setupFilesAfterEnv` \[array]

Default: `[]`
Expand Down
13 changes: 13 additions & 0 deletions e2e/__tests__/setupFiles.test.ts
@@ -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.
*/

import runJest from '../runJest';

test('invokes async function exported from `setupFiles` module', () => {
const result = runJest('setup-files');
expect(result.exitCode).toBe(0);
});
12 changes: 12 additions & 0 deletions e2e/setup-files/__tests__/setup.test.js
@@ -0,0 +1,12 @@
/**
* 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.
*/

const fetchedData = require('../fetched-data');

test('fetches mock data', () => {
expect(fetchedData.RESPONSE).toBe('mock-fetched-data');
});
10 changes: 10 additions & 0 deletions e2e/setup-files/fetched-data.js
@@ -0,0 +1,10 @@
/**
* 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.
*/

module.exports = {
RESPONSE: 'not-yet-received',
};
7 changes: 7 additions & 0 deletions e2e/setup-files/package.json
@@ -0,0 +1,7 @@
{
"jest": {
"setupFiles": [
"./setup-fetchdata.js"
]
}
}
21 changes: 21 additions & 0 deletions e2e/setup-files/setup-fetchdata.js
@@ -0,0 +1,21 @@
/**
* 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.
*/

const fetchedData = require('./fetched-data');

function mockFetchData(mockData) {
return new Promise(resolve => {
setTimeout(() => {
resolve(mockData);
}, 2000);
});
}

module.exports = async () => {
const response = await mockFetchData('mock-fetched-data');
fetchedData.RESPONSE = response;
};
5 changes: 5 additions & 0 deletions packages/jest-repl/src/cli/runtime-cli.ts
Expand Up @@ -116,8 +116,13 @@ export async function run(
await runtime.unstable_importModule(path);
} else {
runtime.requireModule(path);
const setupFile = runtime.requireModule(path);
if (typeof setupFile === 'function') {
await setupFile();
}
}
}

const esm = runtime.unstable_shouldLoadAsEsm(filePath);

if (esm) {
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-runner/src/runTest.ts
Expand Up @@ -207,7 +207,10 @@ async function runTestInternal(
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 3ee2adf

Please sign in to comment.