Skip to content

Commit a2e0147

Browse files
committed
feat(nx-angular-mf): E2E test
E2E test fir check serve return full HTML from ssr
1 parent 35f4bc9 commit a2e0147

File tree

14 files changed

+1984
-828
lines changed

14 files changed

+1984
-828
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ jobs:
2626
- name: Setup Node.js
2727
uses: ./.github/actions/nodejs
2828

29-
- name: Test
30-
run: npx nx affected --target=test --configuration=ci --parallel --base=remotes/origin/${{ inputs.base }} --head=${{ inputs.head }}
29+
# - name: Test
30+
# run: npx nx affected --target=test --configuration=ci --parallel --base=remotes/origin/${{ inputs.base }} --head=${{ inputs.head }}
31+
32+
- name: E2E Test
33+
run: npm nx run nx-angular-mf-e2e:e2e

apps/host-application/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"esPlugins": ["tools/esbuild-plugin/test-external-plugin.ts"],
1919
"remoteEntry": {
2020
"firstRemote": "http://localhost:4201/"
21-
}
21+
},
22+
"deployUrlEnvName": "DEPLOY_URL"
2223
},
2324
"deployUrl": "http://localhost:4200/",
2425
"outputPath": "dist/apps/host-application",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const baseConfig = require('../../eslint.config.cjs');
2+
3+
module.exports = [...baseConfig];

apps/nx-angular-mf-e2e/jest.config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
export default {
3+
displayName: 'nx-angular-mf-e2e',
4+
preset: '../../jest.preset.js',
5+
transform: {
6+
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
7+
},
8+
moduleFileExtensions: ['ts', 'js', 'html'],
9+
coverageDirectory: '../../coverage/apps/nx-angular-mf-e2e',
10+
globalSetup: './tools/start-servers.ts',
11+
globalTeardown: './tools/stop-servers.ts',
12+
};

apps/nx-angular-mf-e2e/project.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "nx-angular-mf-e2e",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"projectType": "application",
5+
"sourceRoot": "apps/nx-angular-mf-e2e/src",
6+
"implicitDependencies": ["nx-angular-mf"],
7+
"targets": {
8+
"e2e": {
9+
"executor": "@nx/jest:jest",
10+
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
11+
"options": {
12+
"jestConfig": "apps/nx-angular-mf-e2e/jest.config.ts",
13+
"runInBand": true
14+
},
15+
"dependsOn": ["^build"]
16+
}
17+
}
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import axios from 'axios';
2+
import {parse} from 'parse5'
3+
import {findAllScripts, findContentWithinLibrary} from "../tools/utils";
4+
5+
describe('check result after run', () => {
6+
it('should html in response', async () => {
7+
8+
const response = await axios.get<string>('http://localhost:4200/').then(res => res.data)
9+
const document = parse(response)
10+
const result = findContentWithinLibrary(document, 'p', 'ngh')
11+
expect(result).not.toBe(null);
12+
expect(Array.isArray(result)).toBe(true)
13+
expect(result.length).toBe(1)
14+
expect(result[0]).toBe('TestSharedLibrary works!')
15+
16+
const result1 = findAllScripts(document)
17+
expect(result1).not.toBe(null);
18+
expect(Array.isArray(result1)).toBe(true)
19+
let hasModuleBefore = false
20+
21+
for(const item of result1) {
22+
if(item.attrs.type === 'module') {
23+
hasModuleBefore = true
24+
}
25+
if(item.attrs.name === 'importmap') break
26+
}
27+
expect(hasModuleBefore).toBe(false)
28+
})
29+
})
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { spawn } from 'child_process';
2+
3+
const controller = new AbortController();
4+
const { signal } = controller;
5+
global.abortCcontrollerInst = controller;
6+
7+
const runServe = async (app: string) => {
8+
return new Promise((resolve, reject) => {
9+
let isStarted = false;
10+
const nxProcess = spawn('npx', ['nx', 'serve', app], {
11+
stdio: ['pipe', 'pipe', 'inherit'],
12+
signal,
13+
});
14+
15+
nxProcess.stdout.on('data', (data) => {
16+
const output = data.toString();
17+
process.stdout.write(output);
18+
if (output.includes('generation complete')) {
19+
isStarted = true;
20+
resolve(nxProcess);
21+
}
22+
});
23+
24+
nxProcess.on('error', (error) => {
25+
reject(error);
26+
});
27+
28+
nxProcess.on('close', (code) => {
29+
if (!isStarted) {
30+
reject(new Error(`Process exited before starting (code ${code})`));
31+
}
32+
});
33+
});
34+
};
35+
36+
export default async () => {
37+
await runServe('mf1-application');
38+
await runServe('host-application');
39+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
export default async () => {
3+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
4+
global.abortCcontrollerInst.abort()
5+
}

apps/nx-angular-mf-e2e/tools/utils.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {serialize} from 'parse5'
2+
3+
export function findContentWithinLibrary(node, targetTagName, targetAttr) {
4+
if (!node || !node['childNodes']) return null;
5+
6+
if (
7+
node['tagName'] === 'lib-test-shared-library' &&
8+
(node['attrs'] || []).some(attr => attr.name === 'ngh' && attr.value === '0')
9+
) {
10+
return node['childNodes']
11+
.filter(child => child.tagName === targetTagName)
12+
.map(child => serialize(child));
13+
}
14+
15+
for (const child of node['childNodes']) {
16+
const result = findContentWithinLibrary(child, targetTagName, targetAttr);
17+
if (result) return result;
18+
}
19+
20+
return null;
21+
}
22+
23+
24+
export function findAllScripts(node) {
25+
let scripts = [];
26+
27+
if (!node || !node.childNodes) return scripts;
28+
29+
// Проверяем, является ли текущий узел <script>
30+
if (node.tagName === 'script') {
31+
scripts.push(node);
32+
}
33+
34+
// Рекурсивно обходим дочерние узлы
35+
for (const child of node.childNodes) {
36+
scripts = scripts.concat(findAllScripts(child));
37+
}
38+
39+
return scripts;
40+
}

apps/nx-angular-mf-e2e/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"files": [],
4+
"include": [],
5+
"references": [
6+
{
7+
"path": "./tsconfig.spec.json"
8+
}
9+
]
10+
}

0 commit comments

Comments
 (0)