-
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
4ca470c
commit 903ef65
Showing
33 changed files
with
543 additions
and
43 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
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]sx?$': 'ts-jest' | ||
}, | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], | ||
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]sx?$': 'ts-jest' | ||
}, | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], | ||
coverageDirectory: '../../coverage/libs/lib-one' | ||
}; | ||
" | ||
`; |
162 changes: 162 additions & 0 deletions
162
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,162 @@ | ||
import { | ||
readJson, | ||
readProjectConfiguration, | ||
Tree, | ||
updateJson, | ||
updateProjectConfiguration, | ||
} from '@nrwl/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
import { updateJestConfigExt } from './update-jest-config-ext'; | ||
import { libraryGenerator as workspaceLib } from '@nrwl/workspace'; | ||
|
||
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 workspaceLib(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 workspaceLib(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 workspaceLib(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(); | ||
}); | ||
|
||
it('should update correct tsconfigs', async () => { | ||
updateJson(tree, 'libs/lib-one/tsconfig.lib.json', (json) => { | ||
json.exclude = ['**/*.spec.ts']; | ||
return json; | ||
}); | ||
|
||
await updateJestConfigExt(tree); | ||
|
||
const tsconfig = readJson(tree, 'libs/lib-one/tsconfig.json'); | ||
const libTsConfig = readJson(tree, 'libs/lib-one/tsconfig.lib.json'); | ||
const specTsConfig = readJson(tree, 'libs/lib-one/tsconfig.spec.json'); | ||
|
||
expect(tsconfig.exclude).toBeFalsy(); | ||
expect(libTsConfig.exclude).toEqual(['**/*.spec.ts', 'jest.config.ts']); | ||
expect(specTsConfig.exclude).toBeFalsy(); | ||
}); | ||
|
||
it('should add exclude to root tsconfig with no references', async () => { | ||
tree.delete('libs/lib-one/tsconfig.spec.json'); | ||
tree.delete('libs/lib-one/tsconfig.lib.json'); | ||
|
||
updateJson(tree, 'libs/lib-one/tsconfig.json', (json) => { | ||
delete json.references; | ||
return json; | ||
}); | ||
|
||
await updateJestConfigExt(tree); | ||
|
||
const tsconfig = readJson(tree, 'libs/lib-one/tsconfig.json'); | ||
|
||
expect(tsconfig.exclude).toEqual(['jest.config.ts']); | ||
expect(tree.exists('libs/lib-one/tsconfig.spec.json')).toBeFalsy(); | ||
expect(tree.exists('libs/lib-one/tsconfig.lib.json')).toBeFalsy(); | ||
}); | ||
}); |
Oops, something went wrong.