-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add migration to add DTL as devDependency (#458)
- Loading branch information
1 parent
67adc5c
commit ef10fbf
Showing
8 changed files
with
82 additions
and
6 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
44 changes: 44 additions & 0 deletions
44
projects/testing-library/schematics/migrations/dtl-as-dev-dependency/index.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,44 @@ | ||
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
import * as path from 'path'; | ||
import { EmptyTree } from '@angular-devkit/schematics'; | ||
|
||
test('adds DTL to devDependencies', async () => { | ||
const tree = await setup({}); | ||
const pkg = tree.readContent('package.json'); | ||
|
||
expect(pkg).toMatchInlineSnapshot(` | ||
"{ | ||
\\"devDependencies\\": { | ||
\\"@testing-library/dom\\": \\"^10.0.0\\" | ||
} | ||
}" | ||
`); | ||
}); | ||
|
||
test('ignores if DTL is already listed as a dev dependency', async () => { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const tree = await setup({ devDependencies: { '@testing-library/dom': '^9.0.0' } }); | ||
const pkg = tree.readContent('package.json'); | ||
|
||
expect(pkg).toMatchInlineSnapshot(`"{\\"devDependencies\\":{\\"@testing-library/dom\\":\\"^9.0.0\\"}}"`); | ||
}); | ||
|
||
test('ignores if DTL is already listed as a dependency', async () => { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const tree = await setup({ dependencies: { '@testing-library/dom': '^11.0.0' } }); | ||
const pkg = tree.readContent('package.json'); | ||
|
||
expect(pkg).toMatchInlineSnapshot(`"{\\"dependencies\\":{\\"@testing-library/dom\\":\\"^11.0.0\\"}}"`); | ||
}); | ||
|
||
async function setup(packageJson: object) { | ||
const collectionPath = path.join(__dirname, '../migrations.json'); | ||
const schematicRunner = new SchematicTestRunner('schematics', collectionPath); | ||
|
||
const tree = new UnitTestTree(new EmptyTree()); | ||
tree.create('package.json', JSON.stringify(packageJson)); | ||
|
||
await schematicRunner.runSchematic(`atl-add-dtl-as-dev-dependency`, {}, tree); | ||
|
||
return tree; | ||
} |
20 changes: 20 additions & 0 deletions
20
projects/testing-library/schematics/migrations/dtl-as-dev-dependency/index.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,20 @@ | ||
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; | ||
import { | ||
addPackageJsonDependency, | ||
getPackageJsonDependency, | ||
NodeDependencyType, | ||
} from '@schematics/angular/utility/dependencies'; | ||
|
||
const dtl = '@testing-library/dom'; | ||
|
||
export default function (): Rule { | ||
return async (tree: Tree, context: SchematicContext) => { | ||
const dtlDep = getPackageJsonDependency(tree, dtl); | ||
if (dtlDep) { | ||
context.logger.info(`Skipping installation of '@testing-library/dom' because it's already installed.`); | ||
} else { | ||
context.logger.info(`Adding '@testing-library/dom' as a peer dependency.`); | ||
addPackageJsonDependency(tree, { name: dtl, type: NodeDependencyType.Dev, overwrite: false, version: '^10.0.0' }); | ||
} | ||
}; | ||
} |
3 changes: 0 additions & 3 deletions
3
projects/testing-library/schematics/migrations/migration.json
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
projects/testing-library/schematics/migrations/migrations.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,10 @@ | ||
{ | ||
"$schema": "../../../../node_modules/@angular-devkit/schematics/collection-schema.json", | ||
"schematics": { | ||
"atl-add-dtl-as-dev-dependency": { | ||
"description": "Add @testing-library/dom as a dev dependency", | ||
"version": "17.0.0-beta.3", | ||
"factory": "./dtl-as-dev-dependency/index" | ||
} | ||
} | ||
} |
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,2 +1,6 @@ | ||
import 'jest-preset-angular/setup-jest'; | ||
import '@testing-library/jest-dom'; | ||
import { TextEncoder, TextDecoder } from 'util'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
Object.assign(global, { TextDecoder, TextEncoder }); |
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