-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(testing): Create root babel config if one isn't present (#7816)
* fix(testing): create root babel config for babel-jest option when creating a jest project with babel-jest a root babel.config.json file will be create if one doesn't exist and babel deps install for allowing jest tests to run successfully ISSUES CLOSED: #6782 * feat(testing): add migration for missing root babel config
- Loading branch information
1 parent
b3fb9ee
commit baf44ad
Showing
7 changed files
with
220 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
packages/jest/src/migrations/update-13-4-4/add-missing-root-babel-config.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { addProjectConfiguration, readJson, Tree } from '@nrwl/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
import update from './add-missing-root-babel-config'; | ||
|
||
describe('Jest Migration (v13.4.4)', () => { | ||
let tree: Tree; | ||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
tree.write( | ||
'package.json', | ||
JSON.stringify({ | ||
name: 'test', | ||
version: '', | ||
description: '', | ||
devDependencies: {}, | ||
}) | ||
); | ||
tree.write( | ||
'libs/lib-one/jest.config.js', | ||
String.raw`module.exports = { | ||
transform: { | ||
'^.+\\\\.[tj]sx?$': 'babel-jest', | ||
} | ||
}` | ||
); | ||
|
||
addProjectConfiguration(tree, 'lib-one', { | ||
root: 'libs/lib-one', | ||
sourceRoot: 'libs/lib-one/src', | ||
projectType: 'library', | ||
targets: { | ||
build: { | ||
executor: '@nrwl/web:build', | ||
}, | ||
test: { | ||
executor: '@nrwl/jest:jest', | ||
options: { | ||
jestConfig: 'libs/lib-one/jest.config.js', | ||
passWithNoTests: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
addProjectConfiguration(tree, 'lib-two', { | ||
root: 'libs/lib-two', | ||
sourceRoot: 'libs/lib-two/src', | ||
projectType: 'library', | ||
targets: { | ||
build: { | ||
executor: '@nrwl/web:build', | ||
}, | ||
test: { | ||
executor: '@nrwl/jest:jest', | ||
options: { | ||
jestConfig: 'libs/lib-two/jest.config.js', | ||
passWithNoTests: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should create root babel.config.json and install @nrwl/web', async () => { | ||
await update(tree); | ||
expect(tree.exists('babel.config.json')).toBeTruthy(); | ||
const packageJson = readJson(tree, 'package.json'); | ||
expect(packageJson.devDependencies['@nrwl/web']).toBeTruthy(); | ||
}); | ||
|
||
it('should not change anything if root babel.config.json is found', async () => { | ||
tree.write('babel.config.json', '{"babelrcRoots": ["*"]}'); | ||
await update(tree); | ||
const packageJson = readJson(tree, 'package.json'); | ||
expect(packageJson.devDependencies['@nrwl/web']).toBeFalsy(); | ||
}); | ||
|
||
it('should update w/ Array value for babel-jest transformer', async () => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
|
||
tree.write( | ||
'libs/lib-three/jest.config.js', | ||
String.raw`module.exports = { | ||
transform: { | ||
'^.+\\\\.[tj]sx?$': ['babel-jest', {someOptionsThatDontMatter: false}], | ||
} | ||
}` | ||
); | ||
|
||
addProjectConfiguration(tree, 'lib-three', { | ||
root: 'libs/lib-three', | ||
sourceRoot: 'libs/lib-three/src', | ||
projectType: 'library', | ||
targets: { | ||
build: { | ||
executor: '@nrwl/web:build', | ||
}, | ||
test: { | ||
executor: '@nrwl/jest:jest', | ||
options: { | ||
jestConfig: 'libs/lib-three/jest.config.js', | ||
passWithNoTests: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
await update(tree); | ||
expect(tree.exists('babel.config.json')).toBeTruthy(); | ||
const packageJson = readJson(tree, 'package.json'); | ||
expect(packageJson.devDependencies['@nrwl/web']).toBeTruthy(); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
packages/jest/src/migrations/update-13-4-4/add-missing-root-babel-config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
addDependenciesToPackageJson, | ||
formatFiles, | ||
joinPathFragments, | ||
readProjectConfiguration, | ||
Tree, | ||
} from '@nrwl/devkit'; | ||
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils'; | ||
import { JestExecutorOptions } from '@nrwl/jest/src/executors/jest/schema'; | ||
import { jestConfigObject } from '../../utils/config/functions'; | ||
import { nxVersion } from '../../utils/versions'; | ||
|
||
function checkIfProjectNeedsUpdate(tree: Tree): boolean { | ||
if (tree.exists('babel.config.json')) { | ||
// the project is already running on babel and good to go | ||
return false; | ||
} | ||
|
||
let shouldUpdate = false; | ||
forEachExecutorOptions<JestExecutorOptions>( | ||
tree, | ||
'@nrwl/jest:jest', | ||
(jestOptions, projectName) => { | ||
const projectConfig = readProjectConfiguration(tree, projectName); | ||
const jestConfigPath = joinPathFragments( | ||
projectConfig.root, | ||
'jest.config.js' | ||
); | ||
|
||
if (!tree.exists(jestConfigPath)) { | ||
return; | ||
} | ||
|
||
const config = jestConfigObject(tree, jestConfigPath); | ||
|
||
for (const transformer of Object.values(config.transform)) { | ||
if ( | ||
(typeof transformer === 'string' && transformer === 'babel-jest') || | ||
(Array.isArray(transformer) && transformer[0] === 'babel-jest') | ||
) { | ||
shouldUpdate = true; | ||
} | ||
} | ||
} | ||
); | ||
|
||
return shouldUpdate; | ||
} | ||
|
||
export default async function update(tree: Tree) { | ||
const shouldUpdateConfigs = checkIfProjectNeedsUpdate(tree); | ||
|
||
if (shouldUpdateConfigs) { | ||
addDependenciesToPackageJson(tree, {}, { '@nrwl/web': nxVersion }); | ||
|
||
tree.write('babel.config.json', '{"babelrcRoots": ["*"]}'); | ||
|
||
await formatFiles(tree); | ||
} | ||
} |