Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(angular): ensure apps/libs layout is created when migrating from …
…angular cli to nx monorepo layout (nrwl#13577)
- Loading branch information
1 parent
63820a9
commit 18e6ad2
Showing
19 changed files
with
785 additions
and
15 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
101 changes: 101 additions & 0 deletions
101
packages/angular/src/generators/karma-project/__snapshots__/karma-project.v14.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,101 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`karmaProject support angular v14 --root-project should generate the right karma.conf.js file for a nested project in a workspace with a project at the root 1`] = ` | ||
"// Karma configuration file, see link for more information | ||
// https://karma-runner.github.io/6.4/config/configuration-file.html | ||
const { join } = require('path'); | ||
const setBaseKarmaConfig = require('../../karma.conf'); | ||
module.exports = function (config) { | ||
setBaseKarmaConfig(config); | ||
config.set({ | ||
coverageReporter: { | ||
dir: join(__dirname, '../../coverage/libs/nested-lib') | ||
} | ||
}); | ||
}; | ||
" | ||
`; | ||
|
||
exports[`karmaProject support angular v14 --root-project should support a project located at the root 1`] = ` | ||
"// Karma configuration file, see link for more information | ||
// https://karma-runner.github.io/6.4/config/configuration-file.html | ||
const { constants } = require('karma'); | ||
const { join } = require('path'); | ||
module.exports = (config) => { | ||
config.set({ | ||
basePath: '', | ||
frameworks: ['jasmine', '@angular-devkit/build-angular'], | ||
plugins: [ | ||
require('karma-jasmine'), | ||
require('karma-chrome-launcher'), | ||
require('karma-jasmine-html-reporter'), | ||
require('karma-coverage'), | ||
require('@angular-devkit/build-angular/plugins/karma'), | ||
], | ||
client: { | ||
jasmine: { | ||
// you can add configuration options for Jasmine here | ||
// the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html | ||
// for example, you can disable the random execution with \`random: false\` | ||
// or set a specific seed with \`seed: 4321\` | ||
}, | ||
clearContext: false, // leave Jasmine Spec Runner output visible in browser | ||
}, | ||
jasmineHtmlReporter: { | ||
suppressAll: true, // removes the duplicated traces | ||
}, | ||
coverageReporter: { | ||
dir: join(__dirname, 'coverage/root-app'), | ||
subdir: '.', | ||
reporters: [{ type: 'html' }, { type: 'text-summary' }], | ||
}, | ||
reporters: ['progress', 'kjhtml'], | ||
port: 9876, | ||
colors: true, | ||
logLevel: constants.LOG_INFO, | ||
autoWatch: true, | ||
browsers: ['Chrome'], | ||
singleRun: true, | ||
restartOnFileChange: true, | ||
}); | ||
}; | ||
" | ||
`; | ||
|
||
exports[`karmaProject support angular v14 should create a karma.conf.js 1`] = ` | ||
"// Karma configuration file, see link for more information | ||
// https://karma-runner.github.io/6.4/config/configuration-file.html | ||
const { join } = require('path'); | ||
const getBaseKarmaConfig = require('../../karma.conf'); | ||
module.exports = function(config) { | ||
const baseConfig = getBaseKarmaConfig(); | ||
config.set({ | ||
...baseConfig, | ||
coverageReporter: { | ||
...baseConfig.coverageReporter, | ||
dir: join(__dirname, '../../coverage/libs/lib1') | ||
} | ||
}); | ||
}; | ||
" | ||
`; | ||
|
||
exports[`karmaProject support angular v14 should generate files 1`] = ` | ||
"{ | ||
\\"extends\\": \\"./tsconfig.json\\", | ||
\\"compilerOptions\\": { | ||
\\"outDir\\": \\"../../dist/out-tsc\\", | ||
\\"types\\": [\\"jasmine\\", \\"node\\"] | ||
}, | ||
\\"files\\": [\\"src/test.ts\\"], | ||
\\"include\\": [\\"**/*.spec.ts\\", \\"**/*.test.ts\\", \\"**/*.d.ts\\"] | ||
} | ||
" | ||
`; |
16 changes: 16 additions & 0 deletions
16
packages/angular/src/generators/karma-project/angular-v14/files/common/karma.conf.js__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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Karma configuration file, see link for more information | ||
// https://karma-runner.github.io/6.4/config/configuration-file.html | ||
|
||
const { join } = require('path'); | ||
const getBaseKarmaConfig = require('<%= offsetFromRoot %>karma.conf'); | ||
|
||
module.exports = function(config) { | ||
const baseConfig = getBaseKarmaConfig(); | ||
config.set({ | ||
...baseConfig, | ||
coverageReporter: { | ||
...baseConfig.coverageReporter, | ||
dir: join(__dirname, '<%= offsetFromRoot %>coverage/<%= projectRoot %>') | ||
} | ||
}); | ||
}; |
22 changes: 22 additions & 0 deletions
22
packages/angular/src/generators/karma-project/angular-v14/files/common/src/test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// This file is required by karma.conf.js and loads recursively all the .spec and framework files | ||
<% if (isLibrary) { %> | ||
import 'zone.js/dist/zone'; | ||
<% } %> | ||
import 'zone.js/dist/zone-testing'; | ||
import { getTestBed } from '@angular/core/testing'; | ||
import { | ||
BrowserDynamicTestingModule, | ||
platformBrowserDynamicTesting | ||
} from '@angular/platform-browser-dynamic/testing'; | ||
|
||
declare const require: any; | ||
|
||
// First, initialize the Angular testing environment. | ||
getTestBed().initTestEnvironment( | ||
BrowserDynamicTestingModule, | ||
platformBrowserDynamicTesting() | ||
); | ||
// Then we find all the tests. | ||
const context = require.context('./', true, /\.spec\.ts$/); | ||
// And load the modules. | ||
context.keys().map(context); |
9 changes: 9 additions & 0 deletions
9
packages/angular/src/generators/karma-project/angular-v14/files/common/tsconfig.spec.json
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 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "<%= offsetFromRoot %>dist/out-tsc", | ||
"types": ["jasmine", "node"] | ||
}, | ||
"files": ["src/test.ts"], | ||
"include": ["**/*.spec.ts", "**/*.test.ts", "**/*.d.ts"] | ||
} |
44 changes: 44 additions & 0 deletions
44
...angular/src/generators/karma-project/angular-v14/files/root-project/karma.conf.js__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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Karma configuration file, see link for more information | ||
// https://karma-runner.github.io/6.4/config/configuration-file.html | ||
|
||
const { constants } = require('karma'); | ||
const { join } = require('path'); | ||
|
||
module.exports = (config) => { | ||
config.set({ | ||
basePath: '', | ||
frameworks: ['jasmine', '@angular-devkit/build-angular'], | ||
plugins: [ | ||
require('karma-jasmine'), | ||
require('karma-chrome-launcher'), | ||
require('karma-jasmine-html-reporter'), | ||
require('karma-coverage'), | ||
require('@angular-devkit/build-angular/plugins/karma'), | ||
], | ||
client: { | ||
jasmine: { | ||
// you can add configuration options for Jasmine here | ||
// the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html | ||
// for example, you can disable the random execution with `random: false` | ||
// or set a specific seed with `seed: 4321` | ||
}, | ||
clearContext: false, // leave Jasmine Spec Runner output visible in browser | ||
}, | ||
jasmineHtmlReporter: { | ||
suppressAll: true, // removes the duplicated traces | ||
}, | ||
coverageReporter: { | ||
dir: join(__dirname, 'coverage/<%= projectName %>'), | ||
subdir: '.', | ||
reporters: [{ type: 'html' }, { type: 'text-summary' }], | ||
}, | ||
reporters: ['progress', 'kjhtml'], | ||
port: 9876, | ||
colors: true, | ||
logLevel: constants.LOG_INFO, | ||
autoWatch: true, | ||
browsers: ['Chrome'], | ||
singleRun: true, | ||
restartOnFileChange: true, | ||
}); | ||
}; |
15 changes: 15 additions & 0 deletions
15
...erators/karma-project/angular-v14/files/workspace-with-root-project/karma.conf.js__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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Karma configuration file, see link for more information | ||
// https://karma-runner.github.io/6.4/config/configuration-file.html | ||
|
||
const { join } = require('path'); | ||
const setBaseKarmaConfig = require('<%= offsetFromRoot %>karma.conf'); | ||
|
||
module.exports = function (config) { | ||
setBaseKarmaConfig(config); | ||
|
||
config.set({ | ||
coverageReporter: { | ||
dir: join(__dirname, '<%= offsetFromRoot %>coverage/<%= projectRoot %>') | ||
} | ||
}); | ||
}; |
27 changes: 27 additions & 0 deletions
27
packages/angular/src/generators/karma-project/angular-v14/karma-project.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,27 @@ | ||
import type { GeneratorCallback, Tree } from '@nrwl/devkit'; | ||
import { formatFiles } from '@nrwl/devkit'; | ||
import { checkProjectTestTarget } from './lib/check-test-target'; | ||
import { generateKarmaProjectFiles } from './lib/generate-karma-project-files'; | ||
import { updateTsConfigs } from './lib/update-tsconfig'; | ||
import { updateWorkspaceConfig } from './lib/update-workspace-config'; | ||
import type { KarmaProjectOptions } from './schema'; | ||
import { karmaGenerator } from '../../karma/karma'; | ||
|
||
export async function karmaProjectGenerator( | ||
tree: Tree, | ||
options: KarmaProjectOptions | ||
): Promise<GeneratorCallback> { | ||
const installTask = await karmaGenerator(tree, options); | ||
checkProjectTestTarget(tree, options.project); | ||
generateKarmaProjectFiles(tree, options.project); | ||
updateTsConfigs(tree, options.project); | ||
updateWorkspaceConfig(tree, options.project); | ||
|
||
if (!options.skipFormat) { | ||
await formatFiles(tree); | ||
} | ||
|
||
return installTask; | ||
} | ||
|
||
export default karmaProjectGenerator; |
9 changes: 9 additions & 0 deletions
9
packages/angular/src/generators/karma-project/angular-v14/lib/check-test-target.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,9 @@ | ||
import type { Tree } from '@nrwl/devkit'; | ||
import { readProjectConfiguration } from '@nrwl/devkit'; | ||
|
||
export function checkProjectTestTarget(tree: Tree, project: string): void { | ||
const projectConfig = readProjectConfiguration(tree, project); | ||
if (projectConfig.targets.test) { | ||
throw new Error(`"${project}" already has a test target.`); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...ages/angular/src/generators/karma-project/angular-v14/lib/generate-karma-project-files.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,62 @@ | ||
import type { Tree } from '@nrwl/devkit'; | ||
import { | ||
generateFiles, | ||
getProjects, | ||
joinPathFragments, | ||
offsetFromRoot, | ||
readProjectConfiguration, | ||
} from '@nrwl/devkit'; | ||
|
||
export function generateKarmaProjectFiles(tree: Tree, project: string): void { | ||
const projectConfig = readProjectConfiguration(tree, project); | ||
generateFiles( | ||
tree, | ||
joinPathFragments(__dirname, '..', 'files', 'common'), | ||
projectConfig.root, | ||
{ | ||
tmpl: '', | ||
isLibrary: projectConfig.projectType === 'library', | ||
projectRoot: projectConfig.root, | ||
offsetFromRoot: offsetFromRoot(projectConfig.root), | ||
} | ||
); | ||
|
||
if (projectConfig.root === '' || projectConfig.root === '.') { | ||
generateFiles( | ||
tree, | ||
joinPathFragments(__dirname, '..', 'files', 'root-project'), | ||
projectConfig.root, | ||
{ | ||
tmpl: '', | ||
projectName: project, | ||
} | ||
); | ||
} else if (isWorkspaceWithProjectAtRoot(tree)) { | ||
generateFiles( | ||
tree, | ||
joinPathFragments( | ||
__dirname, | ||
'..', | ||
'files', | ||
'workspace-with-root-project' | ||
), | ||
projectConfig.root, | ||
{ | ||
tmpl: '', | ||
projectRoot: projectConfig.root, | ||
offsetFromRoot: offsetFromRoot(projectConfig.root), | ||
} | ||
); | ||
} | ||
} | ||
|
||
function isWorkspaceWithProjectAtRoot(tree: Tree): boolean { | ||
const projects = getProjects(tree); | ||
for (const [, project] of projects) { | ||
if (project.root === '.' || project.root === '') { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} |
Oops, something went wrong.