-
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
4af1179
commit ea496f3
Showing
13 changed files
with
191 additions
and
20 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
10 changes: 10 additions & 0 deletions
10
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,10 @@ | ||
// 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 = { | ||
transform: { | ||
'^.+\\\\\\\\\\\\\\\\.[tj]sx?$': 'babel-jest', | ||
},\\"preset\\": \\"../../../jest.preset.ts\\" | ||
}" | ||
`; |
111 changes: 111 additions & 0 deletions
111
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,111 @@ | ||
import { addProjectConfiguration, Tree } from '@nrwl/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
import { updateJestConfigExt } from './update-jest-config-ext'; | ||
|
||
describe('Jest Migration (v14.0.0)', () => { | ||
let tree: Tree; | ||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
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 }; | ||
` | ||
); | ||
addProjectConfiguration(tree, 'lib-one', { | ||
root: 'libs/lib-one', | ||
sourceRoot: 'libs/lib-one/src', | ||
targets: { | ||
test: { | ||
executor: '@nrwl/jest:jest', | ||
options: { | ||
jestConfig: 'libs/lib-one/jest.config.js', | ||
passWithNoTests: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
tree.write( | ||
'libs/lib-one/jest.config.js', | ||
String.raw`module.exports = { | ||
preset: '../../jest.preset.js', | ||
transform: { | ||
'^.+\\\\.[tj]sx?$': 'babel-jest', | ||
} | ||
}` | ||
); | ||
|
||
addProjectConfiguration(tree, 'lib-two', { | ||
root: 'libs/lib-two', | ||
sourceRoot: 'libs/lib-two/src', | ||
targets: { | ||
test: { | ||
executor: '@nrwl/jest:jest', | ||
options: { | ||
jestConfig: 'libs/lib-two/jest.config.ts', | ||
passWithNoTests: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
tree.write( | ||
'libs/lib-two/jest.config.ts', | ||
String.raw`module.exports = { | ||
preset: '../../jest.preset.ts', | ||
transform: { | ||
'^.+\\\\.[tj]sx?$': 'babel-jest', | ||
} | ||
}` | ||
); | ||
|
||
addProjectConfiguration(tree, 'lib-three', { | ||
root: 'libs/lib-three', | ||
sourceRoot: 'libs/lib-three/src', | ||
targets: { | ||
test: { | ||
executor: '@nrwl/jest:jest', | ||
options: { | ||
jestConfig: 'libs/lib-three/jest.config.ts', | ||
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 only rename files that end in .js', async () => { | ||
expect(tree.exists('libs/lib-two/jest.config.ts')).toBeTruthy(); | ||
await updateJestConfigExt(tree); | ||
expect(tree.exists('libs/lib-two/jest.config.ts')).toBeTruthy(); | ||
}); | ||
|
||
it('should not throw error if file does not exit', async () => { | ||
await updateJestConfigExt(tree); | ||
expect(tree.exists('libs/lib-three/jest.config.ts')).toBeFalsy(); | ||
expect(tree.exists('libs/lib-three/jest.config.js')).toBeFalsy(); | ||
}); | ||
}); |
44 changes: 44 additions & 0 deletions
44
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,44 @@ | ||
import { formatFiles, offsetFromRoot, Tree } from '@nrwl/devkit'; | ||
import { 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'; | ||
|
||
export async function updateJestConfigExt(tree: Tree) { | ||
if (tree.exists('jest.config.js')) { | ||
tree.rename('jest.config.js', 'jest.config.ts'); | ||
} | ||
|
||
if (tree.exists('jest.preset.js')) { | ||
tree.rename('jest.preset.js', 'jest.preset.ts'); | ||
} | ||
|
||
forEachExecutorOptions<JestExecutorOptions>( | ||
tree, | ||
'@nrwl/jest:jest', | ||
(options, projectName) => { | ||
if ( | ||
!tree.exists(options.jestConfig) || | ||
!options.jestConfig.endsWith('.js') | ||
) { | ||
return; | ||
} | ||
removePropertyFromJestConfig(tree, options.jestConfig, 'preset'); | ||
addPropertyToJestConfig( | ||
tree, | ||
options.jestConfig, | ||
'preset', | ||
join(offsetFromRoot(options.jestConfig), 'jest.preset.ts'), | ||
{ valueAsString: false } | ||
); | ||
|
||
tree.rename(options.jestConfig, options.jestConfig.replace('.js', '.ts')); | ||
} | ||
); | ||
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