chore: Add Node testing option in ng-schematics (#9266)

**What kind of change does this PR introduce?**

It lets Users create an schematic for Puppeteer in Angular project using
Node's new test runner.

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

Yes. Unit test for module.

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

No

**Summary**

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

No

**Other information**
This commit is contained in:
Nikolay Vitkov 2022-11-14 12:57:42 +01:00 committed by GitHub
parent e003513c0c
commit 57f7366eb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 8 deletions

View File

@ -1,5 +1,14 @@
import * as puppeteer from 'puppeteer'; import * as puppeteer from 'puppeteer';
<% if(testingFramework == 'mocha') { %> <% if(testingFramework == 'node') { %>
import {
describe,
it,
before,
beforeEach,
after,
afterEach,
} from 'node:test';
<% } %><% if(testingFramework == 'mocha' || testingFramework == 'node') { %>
import * as assert from 'assert'; import * as assert from 'assert';
<% } %> <% } %>
@ -11,7 +20,7 @@ describe('App test', function () {
beforeAll(async () => { beforeAll(async () => {
browser = await puppeteer.launch(); browser = await puppeteer.launch();
}); });
<% } %><% if(testingFramework == 'mocha') { %> <% } %><% if(testingFramework == 'mocha' || testingFramework == 'node') { %>
before(async () => { before(async () => {
browser = await puppeteer.launch(); browser = await puppeteer.launch();
}); });
@ -26,11 +35,11 @@ describe('App test', function () {
await page.close(); await page.close();
}); });
<% if(testingFramework == 'jest') { %> <% if(testingFramework == 'jasmine' || testingFramework == 'jest') { %>
afterAll(async () => { afterAll(async () => {
await browser.close(); await browser.close();
}); });
<% } %><% if(testingFramework == 'mocha') { %> <% } %><% if(testingFramework == 'mocha' || testingFramework == 'node') { %>
after(async () => { after(async () => {
await browser.close(); await browser.close();
}); });
@ -43,7 +52,7 @@ describe('App test', function () {
<% if(testingFramework == 'jasmine' || testingFramework == 'jest') { %> <% if(testingFramework == 'jasmine' || testingFramework == 'jest') { %>
expect(element).not.toBeNull(); expect(element).not.toBeNull();
<% } %><% if(testingFramework == 'mocha') { %> <% } %><% if(testingFramework == 'mocha' || testingFramework == 'node') { %>
assert.ok(element); assert.ok(element);
<% } %> <% } %>
}); });

View File

@ -2,9 +2,13 @@
{ {
"extends": "../tsconfig.json", "extends": "../tsconfig.json",
"compilerOptions": { "compilerOptions": {
<% if(testingFramework == 'jest') { %> <% if(testingFramework == 'jest') { %>
"esModuleInterop": true, "esModuleInterop": true,
<% } %> <% } %><% if(testingFramework == 'node') { %>
"module": "CommonJS",
"rootDir": "tests/",
"outDir": "out-tsc/",
<% } %>
"types": ["<%= testingFramework %>"] "types": ["<%= testingFramework %>"]
}, },
"include": ["tests/**/*.e2e.ts"] "include": ["tests/**/*.e2e.ts"]

View File

@ -0,0 +1,3 @@
# Compiled e2e tests output
/out-tsc

View File

@ -13,7 +13,7 @@
"testingFramework": { "testingFramework": {
"description": "", "description": "",
"type": "string", "type": "string",
"enum": ["jasmine", "jest", "mocha"], "enum": ["jasmine", "jest", "mocha", "node"],
"default": "jasmine", "default": "jasmine",
"x-prompt": { "x-prompt": {
"message": "With what Testing Library do you wish to integrate?", "message": "With what Testing Library do you wish to integrate?",
@ -30,6 +30,10 @@
{ {
"value": "mocha", "value": "mocha",
"label": "Use Mocha [https://mochajs.org/]" "label": "Use Mocha [https://mochajs.org/]"
},
{
"value": "node",
"label": "Use Node Test Runner (Experimental: Node v18) [https://nodejs.org/api/test.html]"
} }
] ]
} }

View File

@ -129,6 +129,8 @@ export function getScriptFromOptions(options: SchematicsOptions): string {
return 'jest -c e2e/jest.config.js'; return 'jest -c e2e/jest.config.js';
case TestingFramework.Mocha: case TestingFramework.Mocha:
return 'mocha --config=./e2e/.mocharc.js'; return 'mocha --config=./e2e/.mocharc.js';
case TestingFramework.Node:
return 'tsc -p e2e/tsconfig.json && node --test e2e/out-tsc/**.js';
default: default:
throw new SchematicsException('Testing framework not supported.'); throw new SchematicsException('Testing framework not supported.');
} }

View File

@ -125,6 +125,9 @@ export function getDependenciesFromOptions(
case TestingFramework.Mocha: case TestingFramework.Mocha:
dependencies.push('mocha', '@types/mocha', ...babelPackages); dependencies.push('mocha', '@types/mocha', ...babelPackages);
break; break;
case TestingFramework.Node:
dependencies.push('@types/node');
break;
default: default:
throw new SchematicsException(`Testing framework not supported.`); throw new SchematicsException(`Testing framework not supported.`);
} }

View File

@ -153,4 +153,16 @@ describe('@puppeteer/ng-schematics: ng-add', () => {
expect(devDependencies).toContain('@babel/register'); expect(devDependencies).toContain('@babel/register');
expect(devDependencies).toContain('@babel/preset-typescript'); expect(devDependencies).toContain('@babel/preset-typescript');
}); });
it('should create Node files"', async () => {
const tree = await buildTestingTree({
testingFramework: 'node',
});
const {scripts} = getPackageJson(tree);
expect(tree.files).toContain(getProjectFile('e2e/.gitignore'));
expect(scripts['e2e']).toBe(
'tsc -p e2e/tsconfig.json && node --test e2e/out-tsc/**.js'
);
});
}); });