forked from nrwl/nx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(testing): Using
getJestProjects()
out of the box (nrwl#5900)
* feat(testing): updating nx to use getJestProjects() itself * feat(testing): using getJestProjects() for new workspaces * feat(testing): accomodating for getJestConfig() when adding new project * feat(testing): migration for updating the base jest.config.js * testing... * fix(testing): fixing formatting in tests and bumping to next version * fix(testing): fixing broken tests * fix(testing): fixing test for jest init * fix(testing): removing unnecessary test in jest project * fix(testing): updating remove generator to work with jest utility fn * fix(testing): fixing line break on package.json * fix(testing): fixing import statement * fix(testing): using AST to update the jest config contents * fix(testing): fixing snapshot tests * fix(testing): fixing describe to 12.6 * fix(testing): adding back in import statement to jest.config.js * fix(testing): updating generated docs
- Loading branch information
1 parent
0335797
commit 2524fdb
Showing
12 changed files
with
168 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,5 @@ | ||
const { getJestProjects } = require('@nrwl/jest'); | ||
|
||
module.exports = { | ||
projects: [ | ||
'<rootDir>/packages/tao', | ||
'<rootDir>/packages/workspace', | ||
'<rootDir>/packages/web', | ||
'<rootDir>/packages/cypress', | ||
'<rootDir>/packages/jest', | ||
'<rootDir>/packages/storybook', | ||
'<rootDir>/packages/react', | ||
'<rootDir>/packages/nx-plugin', | ||
'<rootDir>/packages/node', | ||
'<rootDir>/packages/next', | ||
'<rootDir>/packages/nest', | ||
'<rootDir>/packages/linter', | ||
'<rootDir>/packages/express', | ||
'<rootDir>/packages/eslint-plugin-nx', | ||
'<rootDir>/packages/create-nx-workspace', | ||
'<rootDir>/packages/create-nx-plugin', | ||
'<rootDir>/packages/cli', | ||
'<rootDir>/packages/angular', | ||
'<rootDir>/packages/gatsby', | ||
'<rootDir>/dep-graph/dep-graph', | ||
'<rootDir>/nx-dev/nx-dev', | ||
'<rootDir>/nx-dev/ui/common', | ||
'<rootDir>/nx-dev/feature-doc-viewer', | ||
'<rootDir>/nx-dev/data-access-documents', | ||
'<rootDir>/nx-dev/data-access-menu', | ||
'<rootDir>/nx-dev/feature-search', | ||
'<rootDir>/nx-dev/feature-analytics', | ||
'<rootDir>/typedoc-theme', | ||
], | ||
projects: getJestProjects(), | ||
}; |
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
53 changes: 53 additions & 0 deletions
53
packages/jest/src/migrations/update-12-6-0/update-base-jest-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,53 @@ | ||
const mockGetJestProjects = jest.fn(() => []); | ||
jest.mock('../../utils/config/get-jest-projects', () => ({ | ||
getJestProjects: mockGetJestProjects, | ||
})); | ||
const mockResolveConfig = jest.fn(() => | ||
Promise.resolve({ singleQuote: true, endOfLine: 'lf' }) | ||
); | ||
|
||
import { Tree } from '@nrwl/devkit'; | ||
import { createTreeWithEmptyWorkspace } from 'packages/devkit/src/tests/create-tree-with-empty-workspace'; | ||
import update from './update-base-jest-config'; | ||
|
||
describe('update 12.6.0', () => { | ||
let tree: Tree; | ||
beforeEach(() => { | ||
tree = createTreeWithEmptyWorkspace(); | ||
tree.write( | ||
'jest.config.js', | ||
`module.exports = { | ||
projects: ['<rootDir>/test-1'] | ||
}` | ||
); | ||
}); | ||
|
||
beforeEach(async () => { | ||
const prettier = await import('prettier'); | ||
prettier.resolveConfig = mockResolveConfig as any; | ||
}); | ||
|
||
test('all jest projects covered', async () => { | ||
mockGetJestProjects.mockImplementation(() => ['<rootDir>/test-1']); | ||
await update(tree); | ||
const result = tree.read('jest.config.js').toString(); | ||
expect(result).toMatchInlineSnapshot(` | ||
"const { getJestProjects } = require('@nrwl/jest'); | ||
module.exports = { projects: getJestProjects() }; | ||
" | ||
`); | ||
}); | ||
|
||
test('some jest projects uncovered', async () => { | ||
mockGetJestProjects.mockImplementation(() => ['<rootDir>/test-2']); | ||
await update(tree); | ||
const result = tree.read('jest.config.js').toString(); | ||
expect(result).toMatchInlineSnapshot(` | ||
"const { getJestProjects } = require('@nrwl/jest'); | ||
module.exports = { projects: [...getJestProjects(), '<rootDir>/test-1'] }; | ||
" | ||
`); | ||
}); | ||
}); |
57 changes: 57 additions & 0 deletions
57
packages/jest/src/migrations/update-12-6-0/update-base-jest-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,57 @@ | ||
import { formatFiles, Tree } from '@nrwl/devkit'; | ||
import { jestConfigObject } from '../../utils/config/functions'; | ||
import { getJestProjects } from '../../utils/config/get-jest-projects'; | ||
import { | ||
addImportStatementToJestConfig, | ||
addPropertyToJestConfig, | ||
removePropertyFromJestConfig, | ||
} from '../../utils/config/update-config'; | ||
|
||
function determineUncoveredJestProjects(existingProjects: string[]) { | ||
const coveredJestProjects = (getJestProjects() as string[]).reduce( | ||
(acc, key) => { | ||
acc[key] = true; | ||
return acc; | ||
}, | ||
{} | ||
); | ||
return existingProjects.filter((project) => !coveredJestProjects[project]); | ||
} | ||
|
||
function determineProjectsValue(uncoveredJestProjects: string[]): string { | ||
if (!uncoveredJestProjects.length) { | ||
return `getJestProjects()`; | ||
} | ||
return `[...getJestProjects(), ${uncoveredJestProjects.map( | ||
(projectName) => `'${projectName}', ` | ||
)}]`; | ||
} | ||
|
||
function updateBaseJestConfig( | ||
tree: Tree, | ||
baseJestConfigPath = 'jest.config.js' | ||
) { | ||
const currentConfig = jestConfigObject(tree, baseJestConfigPath); | ||
const uncoveredJestProjects = determineUncoveredJestProjects( | ||
currentConfig.projects as string[] | ||
); | ||
removePropertyFromJestConfig(tree, baseJestConfigPath, 'projects'); | ||
addPropertyToJestConfig( | ||
tree, | ||
baseJestConfigPath, | ||
'projects', | ||
determineProjectsValue(uncoveredJestProjects), | ||
{ valueAsString: true } | ||
); | ||
addImportStatementToJestConfig( | ||
tree, | ||
baseJestConfigPath, | ||
`const { getJestProjects } = require('@nrwl/jest');` | ||
); | ||
return; | ||
} | ||
|
||
export default async function update(tree: Tree) { | ||
updateBaseJestConfig(tree); | ||
await formatFiles(tree); | ||
} |
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