-
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): add projects into jest config (nrwl#3766)
* feat(testing): add projects into jest config * chore(testing): update unit tests and fix presets with spreading a default * chore(testing): fix node e2e * chore(testing): review comment changes
- Loading branch information
Showing
21 changed files
with
378 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export = require('./jest-preset'); |
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,9 @@ | ||
export = { | ||
testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], | ||
resolver: '@nrwl/jest/plugins/resolver', | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageReporters: ['html'], | ||
transform: { | ||
'^.+\\.(ts|js|html)$': 'ts-jest', | ||
}, | ||
}; |
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
162 changes: 162 additions & 0 deletions
162
packages/jest/src/migrations/update-10-3-0/update-projects-property.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 { tags } from '@angular-devkit/core'; | ||
import { Tree } from '@angular-devkit/schematics'; | ||
import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; | ||
import { serializeJson } from '@nrwl/workspace'; | ||
import { createEmptyWorkspace } from '@nrwl/workspace/testing'; | ||
import * as path from 'path'; | ||
|
||
describe('update projects property', () => { | ||
let initialTree: Tree; | ||
let schematicRunner: SchematicTestRunner; | ||
|
||
beforeEach(() => { | ||
initialTree = createEmptyWorkspace(Tree.empty()); | ||
|
||
initialTree.create( | ||
'jest.config.js', | ||
tags.stripIndents` | ||
module.exports = { | ||
testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], | ||
transform: { | ||
'^.+\\\\.(ts|js|html)$': 'ts-jest', | ||
}, | ||
maxWorkers: 2, | ||
}; | ||
` | ||
); | ||
|
||
initialTree.create( | ||
'apps/products/jest.config.js', | ||
tags.stripIndents` | ||
module.exports = { | ||
name: 'products', | ||
preset: '../../jest.config.js', | ||
coverageDirectory: '../../coverage/apps/products', | ||
snapshotSerializers: [ | ||
'jest-preset-angular/build/AngularSnapshotSerializer.js', | ||
'jest-preset-angular/build/HTMLCommentSerializer.js' | ||
], | ||
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'], | ||
globals: { | ||
'ts-jest': { | ||
tsConfig: '<rootDir>/tsconfig.spec.json', | ||
stringifyContentPathRegex: '\\.(html|svg)$', | ||
astTransformers: [ | ||
'jest-preset-angular/build/InlineFilesTransformer', | ||
'jest-preset-angular/build/StripStylesTransformer' | ||
] | ||
} | ||
} | ||
}; | ||
` | ||
); | ||
|
||
initialTree.overwrite( | ||
'workspace.json', | ||
serializeJson({ | ||
version: 1, | ||
projects: { | ||
products: { | ||
root: 'apps/products', | ||
sourceRoot: 'apps/products/src', | ||
architect: { | ||
build: { | ||
builder: '@angular-devkit/build-angular:browser', | ||
}, | ||
test: { | ||
builder: '@nrwl/jest:jest', | ||
options: { | ||
jestConfig: 'apps/products/jest.config.js', | ||
tsConfig: 'apps/products/tsconfig.spec.json', | ||
setupFile: 'apps/products/src/test-setup.ts', | ||
passWithNoTests: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
cart: { | ||
root: 'apps/cart', | ||
sourceRoot: 'apps/cart/src', | ||
architect: { | ||
build: { | ||
builder: '@nrwl/web:build', | ||
}, | ||
test: { | ||
builder: '@nrwl/jest:jest', | ||
options: { | ||
jestConfig: 'apps/cart/jest.config.js', | ||
passWithNoTests: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
basket: { | ||
root: 'apps/basket', | ||
sourceRoot: 'apps/basket/src', | ||
architect: { | ||
build: { | ||
builder: '@nrwl/web:build', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
); | ||
schematicRunner = new SchematicTestRunner( | ||
'@nrwl/jest', | ||
path.join(__dirname, '../../../migrations.json') | ||
); | ||
}); | ||
|
||
it('should remove setupFile and tsconfig in test architect from workspace.json', async (done) => { | ||
const result = await schematicRunner | ||
.runSchematicAsync('update-projects-property', {}, initialTree) | ||
.toPromise(); | ||
|
||
const updatedJestConfig = result.readContent('jest.config.js'); | ||
expect(tags.stripIndents`${updatedJestConfig}`).toEqual(tags.stripIndents` | ||
module.exports = { | ||
projects: ['<rootDir>/apps/products', '<rootDir>/apps/cart'], | ||
}; | ||
`); | ||
|
||
const jestPreset = result.readContent('jest.preset.js'); | ||
expect(tags.stripIndents`${jestPreset}`).toEqual(tags.stripIndents` | ||
const nxPreset = require('@nrwl/jest/preset'); | ||
module.exports = { | ||
...nxPreset, | ||
testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], | ||
transform: { | ||
'^.+\\\\.(ts|js|html)$': 'ts-jest', | ||
}, | ||
maxWorkers: 2, | ||
}; | ||
`); | ||
|
||
const projectConfig = result.readContent('apps/products/jest.config.js'); | ||
expect(tags.stripIndents`${projectConfig}`).toEqual(tags.stripIndents` | ||
module.exports = { | ||
preset: '../../jest.preset.js', | ||
coverageDirectory: '../../coverage/apps/products', | ||
snapshotSerializers: [ | ||
'jest-preset-angular/build/AngularSnapshotSerializer.js', | ||
'jest-preset-angular/build/HTMLCommentSerializer.js', | ||
], | ||
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'], | ||
globals: { | ||
'ts-jest': { | ||
tsConfig: '<rootDir>/tsconfig.spec.json', | ||
stringifyContentPathRegex: '\\.(html|svg)$', | ||
astTransformers: [ | ||
'jest-preset-angular/build/InlineFilesTransformer', | ||
'jest-preset-angular/build/StripStylesTransformer', | ||
], | ||
}, | ||
}, | ||
displayName: 'products', | ||
}; | ||
`); | ||
|
||
done(); | ||
}); | ||
}); |
107 changes: 107 additions & 0 deletions
107
packages/jest/src/migrations/update-10-3-0/update-projects-property.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,107 @@ | ||
import { | ||
chain, | ||
Rule, | ||
SchematicContext, | ||
Tree, | ||
} from '@angular-devkit/schematics'; | ||
import { | ||
formatFiles, | ||
getWorkspace, | ||
insert, | ||
InsertChange, | ||
offsetFromRoot, | ||
serializeJson, | ||
} from '@nrwl/workspace'; | ||
import { | ||
addPropertyToJestConfig, | ||
removePropertyFromJestConfig, | ||
} from '../../utils/config/update-config'; | ||
import { jestConfigObjectAst } from '../../utils/config/functions'; | ||
|
||
function updateRootJestConfig(): Rule { | ||
return async (host: Tree, context: SchematicContext) => { | ||
const workspace = await getWorkspace(host); | ||
|
||
const rootDirs = []; | ||
for (const [projectName, project] of workspace.projects) { | ||
for (const [, target] of project.targets) { | ||
if (target.builder !== '@nrwl/jest:jest') { | ||
continue; | ||
} | ||
|
||
rootDirs.push(`<rootDir>/${project.root}`); | ||
|
||
try { | ||
addPropertyToJestConfig( | ||
host, | ||
target.options.jestConfig as string, | ||
'preset', | ||
`${offsetFromRoot(project.root)}jest.preset.js` | ||
); | ||
addPropertyToJestConfig( | ||
host, | ||
target.options.jestConfig as string, | ||
'displayName', | ||
projectName | ||
); | ||
removePropertyFromJestConfig( | ||
host, | ||
target.options.jestConfig as string, | ||
'name' | ||
); | ||
} catch { | ||
context.logger.error( | ||
`Unable to update the jest preset for project ${projectName}. Please manually add "@nrwl/jest/preset" as the preset.` | ||
); | ||
} | ||
} | ||
} | ||
|
||
if (rootDirs.length == 0) { | ||
return; | ||
} else { | ||
try { | ||
context.logger.info(` | ||
The root jest.config.js file will be updated to include all references to each individual project's jest config. | ||
A new jest.preset.js file will be created that would have your existing configuration. All projects will now have this preset. | ||
`); | ||
|
||
let existingRootConfig = host.read('jest.config.js').toString('utf-8'); | ||
|
||
existingRootConfig = | ||
"const nxPreset = require('@nrwl/jest/preset'); \n" + | ||
existingRootConfig; | ||
|
||
const presetPath = 'jest.preset.js'; | ||
|
||
host.create(presetPath, existingRootConfig); | ||
const configObject = jestConfigObjectAst(host, presetPath); | ||
insert(host, presetPath, [ | ||
new InsertChange( | ||
presetPath, | ||
configObject.getStart() + 1, | ||
'\n...nxPreset,' | ||
), | ||
]); | ||
|
||
host.overwrite( | ||
'jest.config.js', | ||
` | ||
module.exports = { | ||
projects: ${serializeJson(rootDirs)} | ||
} | ||
` | ||
); | ||
} catch { | ||
context.logger.error(` | ||
Unable to update the root jest.config.js with projects. Please add the "projects" property to the exported jest config with the following: | ||
${serializeJson(rootDirs)} | ||
`); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
export default function update(): Rule { | ||
return chain([updateRootJestConfig(), formatFiles()]); | ||
} |
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.