diff --git a/CHANGELOG.md b/CHANGELOG.md index 365621c5e1a0..02409d4dec1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixes - `[pretty-format]` Print `BigInt` as a readable number instead of `{}` [#8138](https://github.com/facebook/jest/pull/8138) +- `[jest-core]` Fix ability to transform dependencies required from globalSetup script [#8143](https://github.com/facebook/jest/pull/8143) ### Chore & Maintenance diff --git a/e2e/__tests__/globalSetup.test.ts b/e2e/__tests__/globalSetup.test.ts index 4ed55175c4b2..7eacf5830326 100644 --- a/e2e/__tests__/globalSetup.test.ts +++ b/e2e/__tests__/globalSetup.test.ts @@ -10,7 +10,7 @@ import fs from 'fs'; import os from 'os'; import path from 'path'; import runJest, {json as runWithJson} from '../runJest'; -import {cleanup} from '../Utils'; +import {cleanup, run} from '../Utils'; const DIR = path.join(os.tmpdir(), 'jest-global-setup'); const project1DIR = path.join(os.tmpdir(), 'jest-global-setup-project-1'); @@ -19,18 +19,21 @@ const customTransformDIR = path.join( os.tmpdir(), 'jest-global-setup-custom-transform', ); +const nodeModulesDIR = path.join(os.tmpdir(), 'jest-global-setup-node-modules'); beforeEach(() => { cleanup(DIR); cleanup(project1DIR); cleanup(project2DIR); cleanup(customTransformDIR); + cleanup(nodeModulesDIR); }); afterAll(() => { cleanup(DIR); cleanup(project1DIR); cleanup(project2DIR); cleanup(customTransformDIR); + cleanup(nodeModulesDIR); }); test('globalSetup is triggered once before all test suites', () => { @@ -166,3 +169,12 @@ test('should not transpile the transformer', () => { expect(status).toBe(0); }); + +test('should transform node_modules if configured by transformIgnorePatterns', () => { + const testDir = path.resolve(__dirname, '..', 'global-setup-node-modules'); + run('yarn', testDir); + + const {status} = runJest('global-setup-node-modules', [`--no-cache`]); + + expect(status).toBe(0); +}); diff --git a/e2e/global-setup-node-modules/__tests__/test.js b/e2e/global-setup-node-modules/__tests__/test.js new file mode 100644 index 000000000000..fe1ea73516c9 --- /dev/null +++ b/e2e/global-setup-node-modules/__tests__/test.js @@ -0,0 +1,20 @@ +/** + * 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'; + +const fs = require('fs'); +const os = require('os'); +const path = require('path'); + +const DIR = path.join(os.tmpdir(), 'jest-global-setup-node-modules'); + +test('should exist setup file', () => { + const files = fs.readdirSync(DIR); + expect(files).toHaveLength(1); + const setup = fs.readFileSync(path.join(DIR, files[0]), 'utf8'); + expect(setup).toBe('setup function'); +}); diff --git a/e2e/global-setup-node-modules/babel.config.js b/e2e/global-setup-node-modules/babel.config.js new file mode 100644 index 000000000000..f037a1a2a147 --- /dev/null +++ b/e2e/global-setup-node-modules/babel.config.js @@ -0,0 +1,12 @@ +module.exports = { + presets: [ + [ + '@babel/preset-env', + { + targets: { + node: 'current', + }, + }, + ], + ], +}; diff --git a/e2e/global-setup-node-modules/package.json b/e2e/global-setup-node-modules/package.json new file mode 100644 index 000000000000..55fb042a9a07 --- /dev/null +++ b/e2e/global-setup-node-modules/package.json @@ -0,0 +1,13 @@ +{ + "devDependencies": { + "lodash-es": "^4.17.11" + }, + "jest": { + "testEnvironment": "node", + "globalSetup": "/setup.js", + "transformIgnorePatterns": [ + "/node_modules/(?!(?:lodash-es)/)", + "/packages/" + ] + } +} diff --git a/e2e/global-setup-node-modules/setup.js b/e2e/global-setup-node-modules/setup.js new file mode 100644 index 000000000000..c0dee9f62cf9 --- /dev/null +++ b/e2e/global-setup-node-modules/setup.js @@ -0,0 +1,23 @@ +/** + * 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 {compact} from 'lodash-es'; +const crypto = require('crypto'); +const fs = require('fs'); +const {createDirectory} = require('jest-util'); +const os = require('os'); +const path = require('path'); + +const DIR = path.join(os.tmpdir(), 'jest-global-setup-node-modules'); + +module.exports = function() { + return new Promise(resolve => { + createDirectory(DIR); + const fileId = crypto.randomBytes(20).toString('hex'); + fs.writeFileSync(path.join(DIR, fileId), `setup ${typeof compact}`); + resolve(); + }); +}; diff --git a/e2e/global-setup-node-modules/yarn.lock b/e2e/global-setup-node-modules/yarn.lock new file mode 100644 index 000000000000..6febeb67c34b --- /dev/null +++ b/e2e/global-setup-node-modules/yarn.lock @@ -0,0 +1,8 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +lodash-es@^4.17.11: + version "4.17.11" + resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.11.tgz#145ab4a7ac5c5e52a3531fb4f310255a152b4be0" + integrity sha512-DHb1ub+rMjjrxqlB3H56/6MXtm1lSksDp2rA2cNWjG8mlDUYFhUj3Di2Zn5IwSU87xLv8tNIQ7sSwE/YOX/D/Q== diff --git a/packages/jest-core/src/runGlobalHook.ts b/packages/jest-core/src/runGlobalHook.ts index 32ba09112ec7..5abfa5101e8c 100644 --- a/packages/jest-core/src/runGlobalHook.ts +++ b/packages/jest-core/src/runGlobalHook.ts @@ -60,6 +60,7 @@ export default async ({ transformer.transformSource(filename, code, false).code || code, { exts: [extname(modulePath)], + ignoreNodeModules: false, matcher: transformer.shouldTransform.bind(transformer), }, );