-
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.
feat(testing): move jest config to .ts
move jest config and preset to ts files ISSUES CLOSED: #8344
- Loading branch information
1 parent
4f1c14c
commit abb0a0a
Showing
14 changed files
with
271 additions
and
21 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
2 changes: 1 addition & 1 deletion
2
...ject/files-angular/jest.config.js__tmpl__ → ...ject/files-angular/jest.config.ts__tmpl__
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
2 changes: 1 addition & 1 deletion
2
...jest-project/files/jest.config.js__tmpl__ → ...jest-project/files/jest.config.ts__tmpl__
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
37 changes: 37 additions & 0 deletions
37
packages/jest/src/migrations/update-14-0-0/__snapshots__/update-jest-config-ext.spec.ts.snap
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,37 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Jest Migration (v14.0.0) should rename project jest.config.js to jest.config.ts 1`] = ` | ||
"module.exports = { | ||
displayName: 'lib-one', | ||
preset: '../../jest.preset.ts', | ||
globals: { | ||
'ts-jest': { | ||
tsconfig: '<rootDir>/tsconfig.spec.json', | ||
} | ||
}, | ||
transform: { | ||
'^.+\\\\\\\\.[tj]s$': 'ts-jest' | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../coverage/libs/lib-one' | ||
}; | ||
" | ||
`; | ||
|
||
exports[`Jest Migration (v14.0.0) should update jest.config.ts preset to use the jest.preset.ts 1`] = ` | ||
"module.exports = { | ||
displayName: 'lib-one', | ||
preset: '../../jest.preset.ts', | ||
globals: { | ||
'ts-jest': { | ||
tsconfig: '<rootDir>/tsconfig.spec.json', | ||
} | ||
}, | ||
transform: { | ||
'^.+\\\\\\\\.[tj]s$': 'ts-jest' | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../coverage/libs/lib-one' | ||
}; | ||
" | ||
`; |
125 changes: 125 additions & 0 deletions
125
packages/jest/src/migrations/update-14-0-0/update-jest-config-ext.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,125 @@ | ||
import { | ||
readProjectConfiguration, | ||
Tree, | ||
updateProjectConfiguration, | ||
} from '@nrwl/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
import { libraryGenerator } from '@nrwl/js'; | ||
import { updateJestConfigExt } from './update-jest-config-ext'; | ||
|
||
describe('Jest Migration (v14.0.0)', () => { | ||
let tree: Tree; | ||
beforeEach(async () => { | ||
tree = createTreeWithEmptyWorkspace(2); | ||
tree.write( | ||
'jest.config.js', | ||
String.raw` | ||
const { getJestProjects } = require('@nrwl/jest'); | ||
module.exports = { | ||
projects: getJestProjects(), | ||
}; | ||
` | ||
); | ||
tree.write( | ||
'jest.preset.js', | ||
String.raw` | ||
const nxPreset = require('@nrwl/jest/preset'); | ||
module.exports = { ...nxPreset }; | ||
` | ||
); | ||
await libraryGenerator(tree, { name: 'lib-one' }); | ||
tree.rename('libs/lib-one/jest.config.ts', 'libs/lib-one/jest.config.js'); | ||
updateProjectConfiguration(tree, 'lib-one', { | ||
...readProjectConfiguration(tree, 'lib-one'), | ||
targets: { | ||
test: { | ||
executor: '@nrwl/jest:jest', | ||
options: { | ||
jestConfig: 'libs/lib-one/jest.config.js', | ||
passWithNoTests: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should rename project jest.config.js to jest.config.ts', async () => { | ||
await updateJestConfigExt(tree); | ||
expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy(); | ||
expect(tree.read('libs/lib-one/jest.config.ts', 'utf-8')).toMatchSnapshot(); | ||
}); | ||
|
||
it('should rename root jest files', async () => { | ||
await updateJestConfigExt(tree); | ||
expect(tree.exists('jest.config.ts')).toBeTruthy(); | ||
expect(tree.exists('jest.preset.ts')).toBeTruthy(); | ||
}); | ||
|
||
it('should update jest.config.ts preset to use the jest.preset.ts', async () => { | ||
tree.rename('libs/lib-one/jest.config.js', 'libs/lib-one/jest.config.ts'); | ||
const projectConfig = readProjectConfiguration(tree, 'lib-one'); | ||
updateProjectConfiguration(tree, 'lib-one', { | ||
...projectConfig, | ||
targets: { | ||
test: { | ||
...projectConfig.targets.test, | ||
options: { | ||
jestConfig: 'libs/lib-one/jest.config.ts', | ||
passWithNoTests: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy(); | ||
await updateJestConfigExt(tree); | ||
expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy(); | ||
expect(tree.read('libs/lib-one/jest.config.ts', 'utf-8')).toMatchSnapshot(); | ||
}); | ||
|
||
it('should only update js/ts files', async () => { | ||
tree.rename('libs/lib-one/jest.config.js', 'libs/lib-one/jest.config.ts'); | ||
updateProjectConfiguration(tree, 'lib-one', { | ||
...readProjectConfiguration(tree, 'lib-one'), | ||
targets: { | ||
test: { | ||
executor: '@nrwl/jest:jest', | ||
options: { | ||
jestConfig: 'libs/lib-one/jest.config.ts', | ||
passWithNoTests: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
await libraryGenerator(tree, { name: 'lib-two' }); | ||
tree.delete('libs/lib-two/jest.config.ts'); // lib generator creates a ts file | ||
tree.write('libs/lib-two/jest.config.json', '{}'); | ||
updateProjectConfiguration(tree, 'lib-two', { | ||
...readProjectConfiguration(tree, 'lib-two'), | ||
targets: { | ||
test: { | ||
executor: '@nrwl/jest:jest', | ||
options: { | ||
jestConfig: 'libs/lib-two/jest.config.json', | ||
passWithNoTests: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
await libraryGenerator(tree, { name: 'lib-three' }); | ||
|
||
expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy(); | ||
await updateJestConfigExt(tree); | ||
expect(tree.exists('libs/lib-one/jest.config.ts')).toBeTruthy(); | ||
expect(tree.exists('libs/lib-two/jest.config.ts')).toBeFalsy(); | ||
expect(tree.exists('libs/lib-two/jest.config.json')).toBeTruthy(); | ||
expect(tree.exists('libs/lib-three/jest.config.ts')).toBeTruthy(); | ||
}); | ||
|
||
it('should not throw error if file does not exit', async () => { | ||
tree.delete('libs/lib-one/jest.config.js'); | ||
await updateJestConfigExt(tree); | ||
expect(tree.exists('libs/lib-one/jest.config.ts')).toBeFalsy(); | ||
expect(tree.exists('libs/lib-one/jest.config.js')).toBeFalsy(); | ||
}); | ||
}); |
82 changes: 82 additions & 0 deletions
82
packages/jest/src/migrations/update-14-0-0/update-jest-config-ext.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,82 @@ | ||
import { | ||
formatFiles, | ||
logger, | ||
offsetFromRoot, | ||
readProjectConfiguration, | ||
Tree, | ||
updateProjectConfiguration, | ||
} from '@nrwl/devkit'; | ||
import { jestConfigObject } from '../../utils/config/functions'; | ||
import { dirname, extname, join } from 'path'; | ||
import { | ||
removePropertyFromJestConfig, | ||
addPropertyToJestConfig, | ||
} from '../../utils/config/update-config'; | ||
import { JestExecutorOptions } from '../../executors/jest/schema'; | ||
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils'; | ||
|
||
const allowedExt = ['.ts', '.js']; | ||
|
||
export async function updateJestConfigExt(tree: Tree) { | ||
let isRootPresetUpdated = false; | ||
if (tree.exists('jest.config.js')) { | ||
tree.rename('jest.config.js', 'jest.config.ts'); | ||
} else { | ||
logger.debug(`Did not find root jest.config.js to rename, skipping`); | ||
} | ||
|
||
if (tree.exists('jest.preset.js')) { | ||
isRootPresetUpdated = true; | ||
tree.rename('jest.preset.js', 'jest.preset.ts'); | ||
} else { | ||
logger.debug(`Did not find jest.preset.js to rename, skipping`); | ||
} | ||
|
||
forEachExecutorOptions<JestExecutorOptions>( | ||
tree, | ||
'@nrwl/jest:jest', | ||
(options, projectName, target, configuration) => { | ||
const configExt = extname(options.jestConfig); | ||
console.log(`configExt: ${configExt}`); | ||
if (!tree.exists(options.jestConfig) || !allowedExt.includes(configExt)) { | ||
logger.debug( | ||
`unable to update file because it doesn't exist or is not a js or ts file. Config: ${ | ||
options.jestConfig | ||
}. Exists?: ${tree.exists(options.jestConfig)}` | ||
); | ||
return; | ||
} | ||
|
||
const oldConfig = jestConfigObject(tree, options.jestConfig); | ||
|
||
// if using the root preset and the root preset was updated to ts file. | ||
// then update the jest config | ||
if (isRootPresetUpdated && oldConfig.preset.endsWith('jest.preset.js')) { | ||
removePropertyFromJestConfig(tree, options.jestConfig, 'preset'); | ||
addPropertyToJestConfig( | ||
tree, | ||
options.jestConfig, | ||
'preset', | ||
join(offsetFromRoot(dirname(options.jestConfig)), 'jest.preset.ts'), | ||
{ valueAsString: false } | ||
); | ||
} else { | ||
logger.debug( | ||
isRootPresetUpdated | ||
? `Existing preset for project ${projectName} did not end with 'jest.preset.js' there for not updating it. Actual preset: ${oldConfig.preset}` | ||
: `Did not update root preset; therefore, not updating ${projectName} jest config preset @ ${options.jestConfig}` | ||
); | ||
} | ||
const newJestConfigPath = options.jestConfig.replace('.js', '.ts'); | ||
|
||
tree.rename(options.jestConfig, newJestConfigPath); | ||
const projectConfig = readProjectConfiguration(tree, projectName); | ||
|
||
projectConfig.targets[target].options.jestConfig = newJestConfigPath; | ||
updateProjectConfiguration(tree, projectName, projectConfig); | ||
} | ||
); | ||
await formatFiles(tree); | ||
} | ||
|
||
export default updateJestConfigExt; |
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
Oops, something went wrong.