Skip to content

Commit

Permalink
chore: Add Jest as a ng-schematics option (#9257)
Browse files Browse the repository at this point in the history
**What kind of change does this PR introduce?**

Adds Jest in `ng-schematics`. So you can scaffold test with Jest instead
of Jasmine

**Did you add tests for your changes?**

Yes

**If relevant, did you update the documentation?**

No

**Summary**

Users can more easily integrate test with Puppeteer and their library of
choose.

**Does this PR introduce a breaking change?**

No

**Other information**
  • Loading branch information
Lightning00Blade committed Nov 11, 2022
1 parent 9545691 commit 1bbecb3
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/ng-schematics/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore File that will be copied to Angular
/files/
8 changes: 6 additions & 2 deletions packages/ng-schematics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"scripts": {
"copy": "node copySchemaFiles.js",
"clean": "tsc -b --clean && rimraf lib",
"dev": "npm run copy && tsc -p tsconfig.json --watch",
"dev": "run-s clean copy && tsc -p tsconfig.json --watch",
"build": "run-s build:*",
"build:schematics": "npm run copy && tsc -p tsconfig.json",
"build:test": "tsc -p tsconfig.spec.json",
"test": "run-s build && mocha"
"test": "run-s clean build && mocha"
},
"keywords": [
"angular",
Expand All @@ -29,6 +29,10 @@
"@types/node": "^14.15.0",
"@schematics/angular": "^14.2.8"
},
"files": [
"lib",
"!*.tsbuildinfo"
],
"ng-add": {
"save": "devDependencies"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ describe('App test', function () {
await page.close();
});

<% if(testingFramework == 'jest') { %>
afterAll(async () => {
await browser.close();
});
<% } %>

it('is running', async function () {
const element = await page.waitForSelector(
'text/<%= project %> app is running!'
);

<% if(testingFramework == 'jasmine') { %>
<% if(testingFramework == 'jasmine' || testingFramework == 'jest') { %>
expect(element).not.toBeNull();
<% } %>
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
<% if(testingFramework == 'jest') { %>
"esModuleInterop": true,
<% } %>
"types": ["<%= testingFramework %>"]
},
"include": ["tests/**/*.e2e.ts"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/configuration
*/

/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
testMatch: ['<rootDir>/tests/**/?(*.)+(e2e).[tj]s?(x)'],
preset: 'ts-jest',
testEnvironment: 'node',
};
6 changes: 5 additions & 1 deletion packages/ng-schematics/src/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"testingFramework": {
"description": "",
"type": "string",
"enum": ["jasmine"],
"enum": ["jasmine", "jest"],
"default": "jasmine",
"x-prompt": {
"message": "With what Testing Library do you wish to integrate?",
Expand All @@ -22,6 +22,10 @@
{
"value": "jasmine",
"label": "Use Jasmine [https://jasmine.github.io/]"
},
{
"value": "jest",
"label": "Use Jest [https://jestjs.io/]"
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions packages/ng-schematics/src/schematics/utils/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export function getScriptFromOptions(options: SchematicsOptions): string {
switch (options.testingFramework) {
case TestingFramework.Jasmine:
return 'jasmine --config=./e2e/support/jasmine.json';
case TestingFramework.Jest:
return 'jest -c e2e/jest.config.js';
default:
throw new SchematicsException('Testing framework not supported.');
}
Expand Down
3 changes: 3 additions & 0 deletions packages/ng-schematics/src/schematics/utils/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ export function getDependenciesFromOptions(
'@babel/preset-typescript'
);
break;
case TestingFramework.Jest:
dependencies.push('jest', '@types/jest', 'ts-jest');
break;
default:
throw new SchematicsException(`Testing framework not supported.`);
}
Expand Down
13 changes: 13 additions & 0 deletions packages/ng-schematics/test/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,17 @@ describe('@puppeteer/ng-schematics: ng-add', () => {
expect(devDependencies).toContain('@babel/register');
expect(devDependencies).toContain('@babel/register');
});

it('should create Jest files and update "package.json"', async () => {
const tree = await buildTestingTree({
testingFramework: 'jest',
});
const {scripts, devDependencies} = getPackageJson(tree);

expect(tree.files).toContain(getProjectFile('e2e/jest.config.js'));
expect(scripts['e2e']).toBe('jest -c e2e/jest.config.js');
expect(devDependencies).toContain('jest');
expect(devDependencies).toContain('@types/jest');
expect(devDependencies).toContain('ts-jest');
});
});

0 comments on commit 1bbecb3

Please sign in to comment.