diff --git a/packages/ng-schematics/src/schematics/ng-add/files/base/e2e/tests/app.e2e.ts.template b/packages/ng-schematics/src/schematics/ng-add/files/base/e2e/tests/app.e2e.ts.template index 0778872f..2f98ef7d 100644 --- a/packages/ng-schematics/src/schematics/ng-add/files/base/e2e/tests/app.e2e.ts.template +++ b/packages/ng-schematics/src/schematics/ng-add/files/base/e2e/tests/app.e2e.ts.template @@ -1,5 +1,14 @@ 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'; <% } %> @@ -11,7 +20,7 @@ describe('App test', function () { beforeAll(async () => { browser = await puppeteer.launch(); }); -<% } %><% if(testingFramework == 'mocha') { %> +<% } %><% if(testingFramework == 'mocha' || testingFramework == 'node') { %> before(async () => { browser = await puppeteer.launch(); }); @@ -26,11 +35,11 @@ describe('App test', function () { await page.close(); }); -<% if(testingFramework == 'jest') { %> +<% if(testingFramework == 'jasmine' || testingFramework == 'jest') { %> afterAll(async () => { await browser.close(); }); -<% } %><% if(testingFramework == 'mocha') { %> +<% } %><% if(testingFramework == 'mocha' || testingFramework == 'node') { %> after(async () => { await browser.close(); }); @@ -43,7 +52,7 @@ describe('App test', function () { <% if(testingFramework == 'jasmine' || testingFramework == 'jest') { %> expect(element).not.toBeNull(); -<% } %><% if(testingFramework == 'mocha') { %> +<% } %><% if(testingFramework == 'mocha' || testingFramework == 'node') { %> assert.ok(element); <% } %> }); diff --git a/packages/ng-schematics/src/schematics/ng-add/files/base/e2e/tsconfig.json.template b/packages/ng-schematics/src/schematics/ng-add/files/base/e2e/tsconfig.json.template index 50294276..cc5097ed 100644 --- a/packages/ng-schematics/src/schematics/ng-add/files/base/e2e/tsconfig.json.template +++ b/packages/ng-schematics/src/schematics/ng-add/files/base/e2e/tsconfig.json.template @@ -2,9 +2,13 @@ { "extends": "../tsconfig.json", "compilerOptions": { - <% if(testingFramework == 'jest') { %> +<% if(testingFramework == 'jest') { %> "esModuleInterop": true, - <% } %> +<% } %><% if(testingFramework == 'node') { %> + "module": "CommonJS", + "rootDir": "tests/", + "outDir": "out-tsc/", +<% } %> "types": ["<%= testingFramework %>"] }, "include": ["tests/**/*.e2e.ts"] diff --git a/packages/ng-schematics/src/schematics/ng-add/files/node/e2e/.gitignore.template b/packages/ng-schematics/src/schematics/ng-add/files/node/e2e/.gitignore.template new file mode 100644 index 00000000..b35208fd --- /dev/null +++ b/packages/ng-schematics/src/schematics/ng-add/files/node/e2e/.gitignore.template @@ -0,0 +1,3 @@ +# Compiled e2e tests output + +/out-tsc \ No newline at end of file diff --git a/packages/ng-schematics/src/schematics/ng-add/schema.json b/packages/ng-schematics/src/schematics/ng-add/schema.json index 6fa5114d..0d7c1a4d 100644 --- a/packages/ng-schematics/src/schematics/ng-add/schema.json +++ b/packages/ng-schematics/src/schematics/ng-add/schema.json @@ -13,7 +13,7 @@ "testingFramework": { "description": "", "type": "string", - "enum": ["jasmine", "jest", "mocha"], + "enum": ["jasmine", "jest", "mocha", "node"], "default": "jasmine", "x-prompt": { "message": "With what Testing Library do you wish to integrate?", @@ -30,6 +30,10 @@ { "value": "mocha", "label": "Use Mocha [https://mochajs.org/]" + }, + { + "value": "node", + "label": "Use Node Test Runner (Experimental: Node v18) [https://nodejs.org/api/test.html]" } ] } diff --git a/packages/ng-schematics/src/schematics/utils/files.ts b/packages/ng-schematics/src/schematics/utils/files.ts index bd1da17f..54f25071 100644 --- a/packages/ng-schematics/src/schematics/utils/files.ts +++ b/packages/ng-schematics/src/schematics/utils/files.ts @@ -129,6 +129,8 @@ export function getScriptFromOptions(options: SchematicsOptions): string { return 'jest -c e2e/jest.config.js'; case TestingFramework.Mocha: return 'mocha --config=./e2e/.mocharc.js'; + case TestingFramework.Node: + return 'tsc -p e2e/tsconfig.json && node --test e2e/out-tsc/**.js'; default: throw new SchematicsException('Testing framework not supported.'); } diff --git a/packages/ng-schematics/src/schematics/utils/packages.ts b/packages/ng-schematics/src/schematics/utils/packages.ts index 1a483cc9..f7d9f40b 100644 --- a/packages/ng-schematics/src/schematics/utils/packages.ts +++ b/packages/ng-schematics/src/schematics/utils/packages.ts @@ -125,6 +125,9 @@ export function getDependenciesFromOptions( case TestingFramework.Mocha: dependencies.push('mocha', '@types/mocha', ...babelPackages); break; + case TestingFramework.Node: + dependencies.push('@types/node'); + break; default: throw new SchematicsException(`Testing framework not supported.`); } diff --git a/packages/ng-schematics/test/src/index.spec.ts b/packages/ng-schematics/test/src/index.spec.ts index f1e94811..d676537d 100644 --- a/packages/ng-schematics/test/src/index.spec.ts +++ b/packages/ng-schematics/test/src/index.spec.ts @@ -153,4 +153,16 @@ describe('@puppeteer/ng-schematics: ng-add', () => { expect(devDependencies).toContain('@babel/register'); 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' + ); + }); });